24

SpringMVC---请求与响应的处理(实战二)

 3 years ago
source link: https://segmentfault.com/a/1190000025190908
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本篇文章讲述响应的处理,关于请求的处理可以看我第一篇文章

链接地址: https://segmentfault.com/a/11...

前期准备

使用环境

  • JDK:1.8
  • Tomcat:9.0.3
  • Spring:5.2.8
  • Maven:3.6.3
  • 编译器:IntelliJ IDEA 2019

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 version="4.0">
     
     <servlet> 
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param> 
            <param-name>contextConfigLocation</param-name>
        <param-value>classpath:ApplicationContext.xml</param-value>
        </init-param> 
    </servlet>
    <servlet-mapping> 
        <servlet-name>mvc</servlet-name>
        <url-pattern>*.do</url-pattern>
     </servlet-mapping>
</web-app>

ApplicationContext.xml配置(Spring核心配置文件)

<?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"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 开启spring注解驱动-->
     <context:component-scan base-package="com.cjh"/>
    <!-- 开启mvc注解驱动-->
     <mvc:annotation-driven></mvc:annotation-driven>
</beans>

响应处理

原生的Servlet处理有直接采用response获取输出流直接响应会给浏览器,通过request转发或者重定向三种方式,现在使用spring-mvc进行响应的处理

返回String

  • "forward:资源路径名":转发到冒号后面的资源路径
  • "redirect:资源路径名":重定向到冒号后面的资源路径名
  • "":如果返回的是空串,则会找index.jsp资源
  • 示例代码如下:
@Controller
public class UserController {
    
    //方法中传入实体对象:对象里面有list集合
    @RequestMapping("test.do")
    public String testFive(User user){
        System.out.println(user);
        return "redirect:welcome.jsp";
    }
}

ModelAndView

  • Model、ModelMap和ModelAndView的区别

    • Model和ModelMap只能存储返回的参数(key-value)
    • ModelAndView既能存储返回的参数,也能存储资源路径
  • ModelAndView中有两个重要的属性:

    • Object view:存储资源路径
    • ModelMap model:存储返回的参数,mvc框架会将存储的Key-value数据存入request作用域
  • 代码如下:

    • index.jsp:
    <%@ page contentType="text/html; charset=UTF-8" language="java" %>
    <html>
    <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>cai jin hong</title>
    </head>
    <body>
     <form action="test.do" method="post">
         account:<input type="text" name="account" value=""><br>
         password:<input type="text" name="password" value=""><br>
         balance:<input type="text" name="balance" value=""><br>
         address1:<input type="text" name="addressList[0].address" value=""><br>
         address2:<input type="text" name="addressList[1].address" value=""><br>
         <input type="submit" value="submit">
     </form></body>
    </html>
    • welcome.jsp:(这时候,Jsp就能从requestScope作用域里面提取到我们存进去的account)
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
     <title>Title</title>
    </head>
    <body>
     param:welcome,${param.account}<br>
     requestScope:welcome,${requestScope.account}<br>
    </body>
    </html>
    • Java:
    @Controller
    public class UserController {
         //方法中传入实体对象:对象里面有list集合
         @RequestMapping("test.do")
         public ModelAndView testFive(User user){
             //创建ModelAndView对象
             ModelAndView mv = new ModelAndView();
             //设置返回的参数
             mv.addObject("account", user.getAccount());
             mv.addObject("password", user.getBalance());
             //设置返回路径
             mv.setViewName("welcome.jsp");
             return mv;
         }
    }

存入Session作用域

  • 首先,需要将参数(Key-value)存入request作用域
  • 然后通过@SessionAttributes({"key",""})注解存入Session作用域
  • 代码如下:(此时,welcome.jsp就能从SessionScope作用域里获取到account的值)
@Controller
@SessionAttributes("account")
public class UserController {
    //方法中传入实体对象:对象里面有list集合
     @RequestMapping("test.do")
     public ModelAndView testFive(User user){
         //创建ModelAndView对象
         ModelAndView mv = new ModelAndView();
         //设置返回的参数
         mv.addObject("account", user.getAccount());
         mv.addObject("password", user.getBalance());
         //设置返回路径
         mv.setViewName("welcome.jsp");
         return mv;
     }
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK