之前用过struts2,学习了spring mvc之后,发现还是比struts2方便一些。
首先,web.xml配置
1. <</span>filter>
2. <</span>filter-name>springUtf8Encoding</</span>filter-name>
3. <</span>filter-class>org.springframework.web.filter.CharacterEncodingFilter</</span>filter-class>
4. <</span>init-param>
5. <</span>param-name>encoding</</span>param-name>
6. <</span>param-value>UTF-8</</span>param-value>
7. </</span>init-param>
8. </</span>filter>
9. <</span>filter-mapping>
10. <</span>filter-name>springUtf8Encoding</</span>filter-name>
11. <</span>url-pattern>/*</</span>url-pattern>
12. </</span>filter-mapping>
13.
14. <</span>servlet>
15. <</span>servlet-name>dispatcher</</span>servlet-name>
16. <</span>servlet-class>org.springframework.web.servlet.DispatcherServlet</</span>servlet-class>
17. <</span>load-on-startup>1</</span>load-on-startup>
18. </</span>servlet>
19.
20. <</span>servlet-mapping>
21. <</span>servlet-name>dispatcher</</span>servlet-name>
22. <</span>url-pattern>*.do</</span>url-pattern>
23. </</span>servlet-mapping>
24.
25. <</span>listener>
26. <</span>listener-class>org.springframework.web.context.ContextLoaderListener</</span>listener-class>
27. </</span>listener>
28. <</span>context-param>
29. <</span>param-name>contextConfigLocation</</span>param-name>
30. <</span>param-value>/WEB-INF/applicationContext.xml</</span>param-value>
31. </</span>context-param>
也可以配置url-pattern为 / ,这需要在spring配置中添加,这个据说只在3.05版本之后支持。
然后是spring mvc配置文件:dispatcher-servlet.xml , 这个名称中的 dispatcher要和web.xml中DispatcherServlet的名称相同