SpringMVC @ModelAttribute应用

该篇博客主要阐述@ModelAttribute注解的应用

一、场景

修改用户个人基本信息(用户名和邮箱)

注意:不修改用户密码

二、解决方案

利用@ModelAttribute从数据库中找到用户信息,并初始其信息,当修改了用户名和邮箱之后,因为利用了@ModelAttribute,使得不会重置密码,否则将重置密码为NULL

用户实体类User.java

package linjie.springmvc.test;
/**
* @author 浅然    [email protected]
* @version 创建时间:2018年5月21日 下午12:28:14
* User POJO
*/
public class User {
    //用户个人信息成员变量
    private Integer id;     //用户id
    private String username;//用户姓名
    private String password;//用户密码
    private String email;   //用户email

    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }
    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }
    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }
    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }
    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + "]";
    }

    public User() {
        super();
    }

}

修改表单页面

<h1>修改用户名和邮箱<h1>
<form action="springmvc/testmodelattribute">
    <input type="text" name="username" value="浅然"><br><br>
    <input type="text" name="email" value="[email protected]"><br><br>
    <input type="submit" value="submit"> 
</form>
请求处理方法类(controller)
package linjie.springmvc.test;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
* @author 浅然    [email protected]
* @version 创建时间:2018年5月21日 下午12:33:06
* 请求处理的控制器
*/
@Controller
@RequestMapping("/springmvc")
public class Springmvc_controller {
    /*
     * 利用@ModelAttribute注解的方法,会在此controller每个方法执行
     * 因此对于一个controller映射多个URL的用法需要谨慎使用
     * getInitUser()初始化用户信息(模拟)
     */
    @ModelAttribute
    public void getInitUser(@RequestParam(value="id",required=false) Integer id
            ,Map<String, Object> map) {
        System.out.println("执行@ModelAttribute注解的getInitUser方法");
        User user = new User();//创建User对象并设置其相关域
        user.setId(1);
        user.setUsername("浅然");
        user.setPassword("1234");
        user.setEmail("[email protected]");
        System.out.println("User 初始化完成:"+user);
        //放入map集合中
        map.put("user", user);
    }

    /*
     * 请求处理方法
     * 将表单修改后的信息交给testmodelattribute()方法处理
     */
    @RequestMapping("testmodelattribute")
    public String testmodelattribute(User user,Model model) {
        System.out.println("用户名和邮箱修改完成:"+user);
        //将信息存储到model中
        //key(键)要与POJO成员变量名相等
        model.addAttribute("id", user.getId());
        model.addAttribute("username", user.getUsername());
        model.addAttribute("password", user.getPassword());
        model.addAttribute("email", user.getEmail() );
        return "success";
    }
}

注意:利用@ModelAttribute注解的方法,会在此controller每个方法执行,因此对于一个controller映射多个URL的用法需要谨慎使用

结果

修改信息
这里写图片描述

最终回显修改后结果

这里写图片描述


@ModelAttribute还有一种使用——作用在请求处理方法的参数中

**作用:这样的注解,首先springmvc,会先去model找有没有user_new这个key,如果有,注入到string这个参数中

package linjie.springmvc.test;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
* @author 浅然    [email protected]
* @version 创建时间:2018年5月21日 下午12:33:06
* 请求处理的控制器
*/
@Controller
@RequestMapping("/springmvc")
public class Springmvc_controller { 
    /*
     * 这样的注解,首先springmvc,会先去model找有没有user_new这个key,
     * 如果有,注入到string这个参数中
     */
    @ModelAttribute
    public void getInitUser2(@RequestParam(value="id",required=false) Integer id
            ,Model model) {
        System.out.println("执行@ModelAttribute注解的getInitUser方法");
        User user = new User();//创建User对象并设置其相关域
        user.setUsername("浅然");
        System.out.println("User 初始化完成:"+user);
        //放入model集合中
        model.addAttribute("user_new", user.getUsername());
    }

    @RequestMapping("testmodelattribute2W")
    public String testmodelattribute(@ModelAttribute("user_new") String username) {
        System.out.println("username : "+username);
        return "success";
    }
}
访问:http://localhost:8080/Springmvc_ModelAttribute/springmvc/testmodelattribute2W

然后看到控制台打印String类型的username,成功获取

这里写图片描述

工程下载


发布了254 篇原创文章 · 获赞 695 · 访问量 117万+

猜你喜欢

转载自blog.csdn.net/w_linux/article/details/80425805