SpringMVC优势
性能比struts2好
简单 便捷 易学
和Spring无缝集成(使用spring ioc aop)
约定优于配置
能够简单进行Junit测试
支持Restful风格
本地化、国际化、数据验证、类型转换
拦截器
1、导入包
2、配置springMVC
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--1.配置springMVC前端控制器(是总的控制中心 被拦截的url会汇聚到该servlet) --> <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:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <!-- tomcat启动后立即加载 --> </servlet> <!--配置Spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </context-param> <!--2. 配置spring拦截的url模板 以.do结尾的url--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--3. 注册配置文件读取器,监听spring配置文件的变化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </context-param> <!--5. 编码字符集统一为UTF-8(过滤器) --> <filter> <display-name>encodingFilter</display-name> <filter-name>encodingFilter</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> </web-app>
springmvc-servlet.xml
package wym.controler; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class FirstSpringMvc implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { // TODO Auto-generated method stub ModelAndView mv=new ModelAndView(); mv.addObject("msg","你好。。 springmvc");///WEB-INF/jsp/hello.jsp mv.setViewName("hellow"); return mv; } }