发布时间:2023-06-01 11:00
接上一篇:SpringMVC接收请求参数
<project xmlns=\"http://maven.apache.org/POM/4.0.0\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>springMVC03artifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>javax.servlet.jsp.jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jsp-apiartifactId>
<version>2.0version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>*.xmlinclude>
<include>**/*.xmlinclude>
includes>
<filtering>truefiltering>
resource>
resources>
build>
project>
<beans xmlns=\"http://www.springframework.org/schema/beans\"
xmlns:context=\"http://www.springframework.org/schema/context\"
xmlns:mvc=\"http://www.springframework.org/schema/mvc\"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
\">
<context:component-scan base-package=\"com.lyx\"/>
<mvc:annotation-driven>mvc:annotation-driven>
<bean class=\"org.springframework.web.servlet.view.InternalResourceViewResolver\">
<property name=\"prefix\" value=\"/\"/>
<property name=\"suffix\" value=\".jsp\"/>
bean>
beans>
后端控制器,跳转:forword,重定向:redirect
package com.lyx.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(\"/jump\")
public class ForwardController {
@RequestMapping(\"/test1\")
public String test1(){
System.out.println(\"test1\");
return \"forward:/hello.jsp\";
}
@RequestMapping(\"/test2\")
public String test2(){
System.out.println(\"test2\");
//return \"forward:/hello/test1\";//绝对路径。跨类跳转到hello的test1方法
return \"forward:test1\";//相对路径,当前本类的test1
}
@RequestMapping(\"/test3\")
public String test3(){
System.out.println(\"test3\");
return \"redirect:/hello.jsp\";//重定向到hello.jsp
}
@RequestMapping(\"/test4\")
public String test4(){
System.out.println(\"test4\");
//return \"redirect:/hello/test1\";//绝对路径。跨类跳转到hello的test1方法
return \"redirect:test3\";//相对路径,重定向到当前本类的test1
}
}
@Controller
@RequestMapping(\"/data\")
public class DataController {
@RequestMapping(\"/test1\")
public String test1(HttpServletRequest request){
System.out.println(\"test1\");
request.setAttribute(\"name\",\"张三\");
request.setAttribute(\"age\",22);
return \"data\";
}
}
<body>
name:${name}<br/>
age:${age}
body>
@Controller
@RequestMapping(\"/data\")
public class DataController {
@RequestMapping(\"/test1\")
public String test1(HttpServletRequest request, HttpSession session){
session.setAttribute(\"birth\",new Date());
System.out.println(\"test1\");
request.setAttribute(\"name\",\"张三\");
request.setAttribute(\"age\",22);
return \"index\";
}
}
<body>
birth:<fmt:formatDate value=\"${sessionScope.birth}\" pattern=\"yyyy-MM-dd\"/><br/>
name : ${requestScope.name}<br/>
age : ${requestScope.age}<br/>
body>
当我们把数据存在model中的时候呢,model会把这个数据复制到request作用域
@Controller
@RequestMapping(\"/data\")
public class DataController {
@RequestMapping(\"/test2\")
public String test2(Model model){
model.addAttribute(\"gender\",true);
return \"data\";
}
}
<body>
gender:${requestScope.gender}
body>
通过注解设置key值,model向session作用域中存数据
@Controller
@RequestMapping(\"/data\")
@SessionAttributes(names = {\"city\",\"hobby\"})
public class DataController {
@RequestMapping(\"/sss\")
public String test(Model model){
model.addAttribute(\"city\",\"北京\");
model.addAttribute(\"hobby\",\"football\");
return \"index\";
}
}
<body>
city:${sessionScope.city}<br/>
hobby:${sessionScope.hobby}
body>
@Controller
@RequestMapping(\"/data\")
public class DataController {
@RequestMapping(\"ds\")
public String clean(SessionStatus sessionStatus){
sessionStatus.setComplete();
return \"index\";
}
}
ModelAndView是一个用来做数据整合的类
,Model就是数据,View就是视图(.jsp文件)
@Controller
@RequestMapping(\"/data\")
@SessionAttributes(names = {\"city\",\"hobby\"})
public class DataController {
@RequestMapping(\"/aaa\")
public ModelAndView test(){
//新建ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
//设置视图名,即跳转的路径
modelAndView.setViewName(\"forward:/index.jsp\");
//增加数据
modelAndView.addObject(\"Singer\",\"刘雨昕\");
return modelAndView;
}
<body>
Singer:${requestScope.Singer}
body>