SpringMVC回顾总结
1、开发流程
- 导入SpringMVC的依赖包
- 配置SpringMVC的核心控制器DispatcherServlet
- 创建Controller类和视图页面
- 配置Controller中业务方法的映射地址
- 编写SpringMVC的核心配置文件spring-mvc.xml
pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.14.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>
web.xml
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加载SpringMVC核心配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.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>
DemoController.java
@Controller @RequestMapping("/demo") public class DemoController { // 当前请求资源路径:http://localhost:8080/demo/test @RequestMapping("/test") public String test() { System.out.println("SpringMVC执行……"); // 表示从当前资源所在路径下(http://localhost:8080/demo/)寻找视图 // return "success.jsp"; // 表示从项目根路径下(http://localhost:8080/)寻找视图 // return "forward:/page/forward.jsp";// 页面转发,相当于request.getRequestDispatcher().forward(); return "redirect:/redirect.jsp";// 重定向,相当于response.sendRedirect(); 无法访问WEB-INF下的资源 } }
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>success</title> </head> <body> <h1>success!</h1> </body> </html>
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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.silence.controller"/> </beans>
2、SpringMVC执行流程
3、SpringMVC数据响应方式
页面跳转:
- 直接返回字符串:将字符串与视图解析器的前后缀(如果配置了)进行拼接
- 通过ModelAndView对象返回
<!--视图解析器(选择配置)--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--试图前缀--> <property name="prefix" value="/WEB-INF/page/"/> <!--试图后缀--> <property name="suffix" value=".jsp"/> </bean>
返回字符串:
注意:一旦配置了视图解析器,那么页面转发时,SpringMVC会将当前返回的字符串拼接上配置的前后缀作为完整的视图名称去寻找资源。也就是说,此时能再写“forward:”
@RequestMapping("/test1") public String test1(HttpServletRequest request) { request.setAttribute("username","abc"); // 默认页面转发,若配置了上面的示例的视图解析器的前后缀,则此时将返回/WEB-INF/page/forward.jsp这个页面 return "forward"; } @RequestMapping("/test2") public String test2() { // 重定向,要注意此时无法访问WEB-INF下的资源 return "redirect:/page/redirect.jsp"; }
返回ModelAndView对象
@RequestMapping("/test3") public ModelAndView test3() { ModelAndView mav = new ModelAndView(); mav.setViewName("success"); mav.addObject("username", "tom"); return mav; } @RequestMapping("/test4") public ModelAndView test4(ModelAndView mav) { mav.setViewName("success"); mav.addObject("username", "jerry"); return mav; }
jsp页面样例:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>success</title> </head> <body> <h1>success! ${username}</h1> </body> </html>
回写数据:
只需要在方法上添加注解@ResponseBody
即可
普通字符串:
@RequestMapping("/test5") @ResponseBody public String test5() { return "ResponseBody"; }
以json方式返回对象或者集合:
<!--导入必要的jar--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.11.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.11.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.4</version> </dependency>
SpringMVC配置文件中:
<!--开启注解驱动,此驱动底层集成了Jackson,默认会将对象和集合转化为json字符串--> <mvc:annotation-driven/>
@RequestMapping("/test6") @ResponseBody public Map test6() { Map map = new HashMap(); map.put(1, "abc"); map.put(2, "dkd"); map.put(3, "asdd"); map.put(4, "1111"); return map; }