srping搭建步驟:javascript
配置web項目中的web.xml文件:html
(必須配置)前端控制器(DispatcherServlet),DispatcherServlet是spring的核心部件前端
亂碼過濾器(CharacterEncodingFilter),通常也都會配置上用於解決post提交是的亂碼問題;亂碼問題總結見:http://my.oschina.net/u/1757476/blog/411158java
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 若是這裏不配置contextConfigLocation,會自動找"servlet名稱+-servlet.xml"--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- post提交解決亂碼過濾器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
配置springmvc-servlet.xml,spring重要組件:映射器Mapping、適配器Adapter以及視圖解析器ViewResolverweb
Mapping配置:spring
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/
Adapter配置,消息轉換器下一篇再述。json
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <!-- 定義消息轉換器 --> <property name="messageConverters"> <ref bean="jsonHttpMessageConverter"/> </property> </bean>
ViewResolver配置:其中prefix和suffic是其spring-mvc
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前綴 --> <property name="prefix" value="/WEB-INF/jsp/"></property> <!-- 後綴 --> <property name="suffix" value=".jsp"></property> </bean>
總體springmvc-servlet.xml:session
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-- 組件掃描 --> <context:component-scan base-package="springmvc.action" /> <!-- 可使用註解驅動代替下面配置的映射器和適配器 --> <!-- <mvc:annotation-driven /> --> <!-- Annotation Mapping--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!-- Annotation Adapter--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <!-- 定義消息轉換器 --> <property name="messageConverters"> <ref bean="jsonHttpMessageConverter"/> </property> </bean> <!-- 消息轉換器 --> <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <!-- <property name="supportedMediaTypes"> <list> <value>text/javascript;charset=UTF-8</value> </list> </property> --> </bean> <!-- View Resolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- hello Controller --> <!-- <bean class="springmvc_annotation.action.HelloWorldController"/> --> <!-- 攔截器 --> <!-- 多個攔截器,順序執行 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="springmvc.filter.LoginInterceptor"></bean> </mvc:interceptor> <!-- <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="springmvc.filter.HandlerInterceptor1"></bean> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="springmvc.filter.HandlerInterceptor2"></bean> </mvc:interceptor> --> </mvc:interceptors> </beans>
若是須要配置的controller比較多,能夠採用組件掃描的方式:在springmvc-servlet.xml中添加mvc
<!-- 組件掃描 --> <context:component-scan base-package="springmvc.action" />
上述的Mapping和Adapter能夠直接寫成註解驅動代替以上配置:
<!-- 可使用註解驅動代替下面配置的映射器和適配器 --> <mvc:annotation-driven />
Controller代碼部分:
在Controller類上須要標註@Controller註解,這樣組建掃描纔可以掃描的到。
Controller方法上須要標註@RequestMapping(value = "/userAddSubmit", method = { RequestMethod.POST, RequestMethod.GET }),method是限定請求方法,能夠不寫。
@Controller @RequestMapping("/user") //模塊路徑 public class UserAction { /** * 用戶列表 * groupId是用戶類型,進入查詢頁面默認顯示那個用戶類型 * @return */ @RequestMapping(value="/userList") public String userList(HttpServletRequest request, HttpServletResponse response, H ttpSession session, Model model, Map map, ModelMap modelMap, @RequestParam (defaultValue="2", value="group", required=true) String groupId) { return "user/userList"; //返回頁面 }
詳細見附件文檔 springmvc.docx。