@ModelAttribute两大作用:
- 接收客户端请求参数
- 会把接收到的同时会把它存到request作用域当中
dispatcher.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"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
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 http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--设置注解扫描的路径 -->
<context:component-scan base-package="com.baizhiedu"/>
<!--引入springMVC的核心功能-->
<mvc:annotation-driven />
<!--视图解析器配置: 控制器方法返回的结果的前缀和后缀-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
View4Controller:
package com.baizhiedu;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/view4")
public class View4Controller {
@RequestMapping("/view1")
public String view1(@ModelAttribute("name") String naem){ //使用ModelAttribute注解,接收客户端的请求参数,我们就不需要在创建Model了,会自动的把数据存储到request作用域当中
System.out.println("View4Controller.view1");
return "result3";
}
}
result3.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/6
Time: 11:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<h1>${requestScope.name}</h1>
</body>
</html>
使用场景:
User:
package com.baizhiedu;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
View4Controller:
package com.baizhiedu;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/view4")
public class View4Controller {
@RequestMapping("/view1")
public String view1(@ModelAttribute("name") String name){ //使用ModelAttribute注解,接收客户端的请求参数,我们就不需要在创建Model了,会自动的把数据存储到request作用域当中
//@ModelAttribute("name") value name要和超链接或这表单中的key要一致
System.out.println("View4Controller.view1");
return "result3";
}
@RequestMapping("/view2")
public String view2(@ModelAttribute("u") User user){ //使用ModelAttribute注解,接收客户端的请求参数,会自动的把数据存储到request作用域当中
//@ModelAttribute("u") 中的value是随便写的,但是要记住在EL表达式需要它来获取数据,只要POJO类型中的属性名和key对应即可
System.out.println("User="+user);
return "result3";
}
}
result3.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/6
Time: 11:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<h1>${requestScope.name}</h1>
<h1>${requestScope.u.name}</h1>
<h1>${requestScope.u.password}</h1>
</body>
</html>