企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # 清晰的角色划分 * 前端控制器(DispatcherServlet) * 请求到处理器映射(HandlerMapping) * 处理器适配器(HandlerAdapter) * 视图解析器(ViewResolver) * 处理器或页面控制器(Controller) * 验证器( Validator) * 命令对象(Command 请求参数绑定到的对象就叫命令对象) * 表单对象(Form Object 提供给表单展示和提交到的对象就叫表单对象) # 和struct2区别 共同点: * 它们都是表现层框架,都是基于 MVC 模型编写的。 * 它们的底层都离不开原始 ServletAPI。 它们处理请求的机制都是一个核心控制器。 区别: * Spring MVC 的入口是 Servlet, 而 Struts2 是 Filter * Spring MVC 是基于方法设计的,而 Struts2 是基于类,Struts2 每次执行都会创建一个动作类。所 以 Spring MVC 会稍微比 Struts2 快些。 * Spring MVC 使用更加简洁,同时还支持 JSR303, 处理 ajax 的请求更方便 * (JSR303 是一套 JavaBean 参数校验的标准,它定义了很多常用的校验注解,我们可以直接将这些注解加在我们 JavaBean 的属性上面,就可以在需要校验的时候进行校验了。) * Struts2 的 OGNL 表达式使页面的开发效率相比 Spring MVC 更高些,但执行效率并没有比 JSTL 提升,尤其是 struts2 的表单标签,远没有 html 执行效率高。 # 入门程序 ## 代码 1. 启动服务器,加载一些配置文件 * DispatcherServlet对象创建 * springmvc.xml被加载了 * HelloController创建成对象 2. 发送对象,后台处理请求 ![](https://img.kancloud.cn/00/cb/00cbf948cefac9e0cfa2a91757296080_1447x302.png) **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> <display-name>Archetype Created Web Application</display-name> <!--配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ~~~ **springmvc.xml** ~~~ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置创建 spring 容器要扫描的包 --> <context:component-scan base-package="com.jdxia.controller"/> <!-- 视图解析器对象,帮我们跳转页面的 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 以后会找这个目录下的文件 --> <property name="prefix" value="/WEB-INF/pages/"/> <!-- 文件的后缀名是 --> <property name="suffix" value=".jsp"/> </bean> <!-- 开启SpringMVC框架注解的支持 --> <mvc:annotation-driven/> </beans> ~~~ **控制器** ~~~ package com.jdxia.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping(path = "/user") public class HelloController { @RequestMapping(path = "/hello", method = {RequestMethod.GET}) // public String sayHello() { System.out.println("hello sayHello"); //返回的会自动找对应的jsp文件 webapp/WEB-INF/pages/success.jsp return "success"; } } ~~~ ## 执行过程 ![](https://img.kancloud.cn/b2/a9/b2a9475fc55136cc6fd14ba70eb418b3_1160x647.png) # 防止中文乱码 web.xml中 webapp下 ~~~ <!--配置解决中文乱码的过滤器--> <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> ~~~ 在 springmvc 的配置文件中可以配置,静态资源不过滤: ~~~ <!-- location表示路径,mapping表示文件,**表示该目录下的文件以及子目录的文件 --> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/images/" mapping="/images/**"/> <mvc:resources location="/scripts/" mapping="/javascript/**"/> ~~~ get 请求方式: tomacat 对 GET 和 POST 请求处理方式是不同的,GET 请求的编码问题,要改 tomcat 的 server.xml 配置文件,如下: ~~~ <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/> ~~~ 改为: ~~~ <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"/> ~~~ 如果遇到 ajax 请求仍然乱码,修改 tomcat/conf/server.xml请把: `useBodyEncodingForURI="true"`改为 `URIEncoding="UTF-8"` 即可。 ~~~ <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8543" URIEncoding="UTF-8" /> ~~~ # 获取原生servlet对象 SpringMVC 还支持使用原始 ServletAPI 对象作为控制器方法的参数。 支持原始 ServletAPI 对象有: * HttpServletRequest * HttpServletResponse * HttpSession * java.security.Principal * Locale * InputStream * OutputStream * Reader * Writer 我们可以把上述对象 ,直接写在控制的方法参数中使用 ~~~ @RequestMapping(path = "/abc") public String sayAbc(HttpServletRequest request, HttpServletResponse response) { System.out.println("request: " + request); System.out.println("response: " + response); System.out.println("session: " + request.getSession()); System.out.println("servletContext: " + request.getSession().getServletContext()); return "success"; } ~~~ # 配置哪些资源不拦截 springmvc.xml下 ~~~ <!--前端控制器,哪些静态资源不拦截--> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/images/" mapping="/images/**"/> <mvc:resources location="/js/" mapping="/js/**"/> ~~~