SpringMVC和Struts2同樣,是先後臺的一個粘合劑,struts2用得比較熟悉了,如今來配置一下SpringMVC,看看其最基礎配置和基本使用。SpriingMVC不是太難,學習成本不高,如今不少人都喜歡使用它了。css
本次demo工程是一個maven工程,使用maven來對項目進行管理。html
1、首先須要創建一個maven的webapp工程。java
目錄結構以下:web
2、配置maven的pox.xmlspring
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.1.2</version> </dependency>
Maven配置界面以下圖。express
這裏咱們使用的spring版本是3.2.8.RELEASE;配置好後,maven會自動給咱們加載其餘依賴的jar包,以下圖:瀏覽器
具體依賴的包以下:spring-mvc
1) aopalliance-1.0.jartomcat
2) commons-logging-1.1.3.jarmvc
3) spring-aop-3.2.8.RELEASE.jar
4) spring-beans-3.2.8.RELEASE.jar
5) spring-context-3.2.8.RELEASE.jar
6) spring-core-3.2.8.RELEASE.jar
7) spring-expression-3.2.8.RELEASE.jar
8) spring-web-3.2.8.RELEASE.jar
9) spring-webmvc-3.2.8.RELEASE.jar
10) jstl-1.1.2.jar
3、工程配置
一、創建一個最基礎的springMVC測試工程目錄結構以下圖:
二、application_spring_mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 自動掃描的包名 --> <context:component-scan base-package="com.clj"/> <!-- 默認的註解映射的支持,自動註冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --> <mvc:annotation-driven /> <!-- 視圖解釋類 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> <!-- 對靜態資源文件的訪問--> <mvc:resources mapping="/images/**" location="/WEB-INF/images/" cache-period="31556926"/> <mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="31556926"/> <mvc:resources mapping="/css/**" location="/WEB-INF/css/" cache-period="31556926"/> </beans>
三、web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <!-- ================spring mvc 適配器================ --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/applicationContext/application_spring_mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- ================================================== --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
四、index.html
<html> <body> <h2>Hello World!</h2> <form action="./user/save" method="get"> <input id="name" name="name" value="張三"/><br/> <input id="password" name="password" value="123456"/><br/> <input type="submit" value="提交"/> </form> </body> </html>
五、UserAction.java
package com.clj.test.user.action; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; /** * <一句話功能簡述> * <功能詳細描述> * * @author Administrator * @version [版本號, 2014年3月4日] * @see [相關類/方法] * @since [產品/模塊版本] */ @Controller @Scope("prototype") @RequestMapping("/user") public class UserAction { @RequestMapping(value="/save",method=RequestMethod.GET) public ModelAndView save(String name,String password) { System.out.println("接收到的用戶信息:"+name); ModelAndView mov=new ModelAndView(); mov.setViewName("/test/saveUserSuccess"); mov.addObject("msg", "保存成功了"); return mov; } }
六、saveUserSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加用戶成功</title> </head> <body> <h1>操做成功了</h1> 後臺返回的信息:${msg} </body> </html>
4、發佈工程到tomcat進行測試
具體eclipse中如何使用tomcat進行maven webapp項目測試請參看:
http://blog.csdn.net/clj198606061111/article/details/20221133
1) 瀏覽器中輸入:http://localhost:8080/demoSpringMVC/
即可進入index.html頁面,以下圖:
2) 提交後
以上經過本人測試。轉載僅供學習,版權原版http://blog.csdn.net/clj198606061111/article/details/20743769