zoukankan      html  css  js  c++  java
  • SpringMVC(十七-二十) ModelAttribute 注解

    有点难理解。

    修饰方法是表示在该控制器的所有目标方法执行前都执行该modelattribute注解的方法。

    修饰参数是表示什么?修饰参数时@modelattributes(value="xxxx") User user  中的value值需要和@modelattribute修改的方法加入的键的值一致。

    Controller Code:

    复制代码
    package com.tiekui.springmvc.handlers;
    
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import com.tiekui.springmvc.pojo.Address;
    import com.tiekui.springmvc.pojo.User;
    import com.tiekui.springmvc.pojo.UserNew;
    
    @Controller
    public class ModelAttributes {
    
        @RequestMapping("testModelAttribute")
        public String targetMethod(UserNew user){
            System.out.println("Target method" + user);
            return "afterModelAttributes";
        }
        
        
        @ModelAttribute
        public void getuser(@RequestParam(value="id") String id,Map<String, UserNew> map) {
            
            if ((id != null)) {
                UserNew user = new UserNew("tiekui","1234","zhoutiekui@huawei.com",30,1, new Address("guangzhou", "zhenzhen"));
                System.out.println("Get an object" + user);
                map.put("userNew", user);
            }
        }
    }
    复制代码

    Response View:

    复制代码
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    
        userAge: ${requestScope.userNew.age}
    
    </body>
    </html>
    复制代码

    View:

    复制代码
        <form action="testModelAttribute">
            <br>
            password : <input type="text" name="username" value="tiekui">
            <br>
            password : <input type="password" name="password" value="1234">
            <br>
            email : <input type="text" name="email" value="zhoutiekui@huawei.com">
            <br>
            age : <input type="text" name="age" value="18">
            <br>
            <input type="hidden" name="id" value="1">
            <br>
            Address Province: <input type="text" name="address.province" value="guangzhou">
            <br>
            Address City: <input type="text" name="address.city" value="shenzhen">
            <br>
            <input type="submit" value="Test ModelAttributes">
        </form>
    复制代码

    POJO userNew:

     View Code

     POJO Address:

    复制代码
    package com.tiekui.springmvc.pojo;
    
    public class Address {
        private String province;
        private String city;
        
        
        public Address() {
            super();
        }
        public Address(String province, String city) {
            super();
            this.province = province;
            this.city = city;
        }
        @Override
        public String toString() {
            return "Address [province=" + province + ", city=" + city + "]";
        }
        public String getProvince() {
            return province;
        }
        public void setProvince(String province) {
            this.province = province;
        }
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
    }
    复制代码
    https://github.com/godmaybelieve
  • 相关阅读:
    SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)
    SpringCloud教程 | 第五篇: 路由网关(zuul)(Finchley版本)
    SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)
    SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)
    SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)
    SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)
    linux查看日志文件内容命令tail、cat、tac、head、echo详解
    codevs 1462 素数和
    Codevs 1313 质因数分解
    Open Judge 1.4 09
  • 原文地址:https://www.cnblogs.com/yuyu666/p/10136104.html
Copyright © 2011-2022 走看看