SpringMVC:@ModelAttribute注解

@ModelAttribute两大作用:

  1. 接收客户端请求参数
  2. 会把接收到的同时会把它存到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>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值