springmvc中@ModelAttribute和@SessionAttributes标签的使用

来源:互联网 发布:闲鱼淘宝介入操作过程 编辑:程序博客网 时间:2024/06/15 07:30
@ModelAttribute
首先用 @ModelAttribute注释过的方法,会在该controller执行相对应的请求前执行。同个方法和其他方法一样都可以通过springmvc获取到前端相对应的* 字段的数据。 不过一般是获取id;然后去数据库查找该对象。(若果没有对应的id那么会报  NullPointerException)找出来了之后将他放入到ModelAttribute* 中。
如下:
@ModelAttribute public User getUser(int  id) {   User user =userService.getUser(id);   System.out.println(user);   return user;}
@ModelAttribute如果有这个注释修饰  那么直接到ModelAttribute里面去找user对象,如果有那么就用前端数据将该对象更新,并返回更新后的user对象 * 如果没有该对象那么将会创建一个对象,并将前端的值赋给该对象。 * 如果参数没有被@ModelAttribute修饰过 那么该对象只是被springmvc包装成User的数据而已
@RequestMapping("/update")public String updateUser(@ModelAttribute("muser") User user) {   System.out.println(user);    userService.update(user);    return "home";}
上面参数被@ModelAttribute("muser")修饰过,由于Model里面不存在muser对象,所以执行的时候找不到就会创建一个对象所以此时model中就存在user和muser两个对象了。以下是验证代码:
package com.lxj.cn.controller;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import com.lxj.cn.pojo.User;import com.lxj.cn.service.IUserService;@Controller@RequestMapping("/user")public class UserController {      @Autowired   private IUserService userService;   /**@ModelAttribute执行更新    * 首先用 @ModelAttribute注释过的方法,会在该controller执行相对应的请求前执行。同个方法和其他方法一样都可以通过springmvc获取到前端相对应的    * 字段的数据。 不过一般是获取id;然后去数据库查找该对象。(若果没有对应的id那么会报  NullPointerException)找出来了之后将他放入到ModelAttribute    * 中。    * @param id    * @return    */    @ModelAttribute    public User getUser(int  id) {      User user =userService.getUser(id);      System.out.println(user);      return user;   }    /**@ModelAttribute如果有这个注释修饰  那么直接到ModelAttribute里面去找user对象,如果有那么就用前端数据将该对象更新,并返回更新后的user对象     * 如果没有该对象那么将会创建一个对象,并将前端的值赋给该对象。     * 如果参数没有被@ModelAttribute修饰过 那么该对象只是被springmvc包装成User的数据而已     * @param user     * @return     */   @RequestMapping("/update")   public String updateUser(@ModelAttribute("muser") User user) {      System.out.println(user);       userService.update(user);       return "home";   }   @SuppressWarnings("unused")   @RequestMapping("/regist")   public void cheacnum(HttpServletRequest rs,HttpServletResponse httpServletResponse) {      rs.getParameter("tel");      rs.getParameter("psw");      PrintWriter pw=null;      try {          pw=httpServletResponse.getWriter();      } catch (IOException e) {         e.printStackTrace();      }      if(true) {         pw.print("<span style='color:green'>注册成功!<span>");      }else {         pw.print("<span style='color:red'>注册失败!<span>");      }      pw.flush();      pw.close();   }      @SuppressWarnings("unused")   @RequestMapping("/checktel")   public void checktel(HttpServletRequest rs,HttpServletResponse httpServletResponse) {      rs.getParameter("tel");      PrintWriter pw=null;      try {          pw=httpServletResponse.getWriter();      } catch (IOException e) {         e.printStackTrace();      }      if(true) {         pw.print("可以使用该号码!");      }else {         pw.print("改号码以被注册");      }      pw.flush();      pw.close();   }}
@SessionAttributes的使用 
package com.lxj.cn.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.SessionAttributes;import com.lxj.cn.pojo.User;import com.lxj.cn.service.IUserService;import org.springframework.web.bind.support.SessionStatus;import javax.servlet.http.HttpSession;@Controller@RequestMapping("/user2")@SessionAttributes(names="user",types= {User.class,String.class})public class UserController2 {   /**@SessionAttributes 用来注释类的注解。那么将会把@ModelAttribute内对应的字段放入SessionAttributes中如果后面有进行该字段的更新操作,那么    * 在此更新方法执行完毕后,会将更新后的字段属性覆盖原属性。以下为猜想验证:    * 1@SessionAttributes 如果不制定typenames那么会不会将所有用到的字段都保存一份?   验证猜想1证明不会。不会有任何字段会保存进sessionAttribute    * 2@SessionAttributes如果指定了属性的话那么,如果ModelAttribute中存在该属性字段,那么都会保存一份进@SessionAttributes    * 3@SessionAttributes 如果指定了类型的话,那么只要是指定类型的话,都会保存一份进@SessionAttributes    * 注意:1、保存进@SessionAttributes的属性和值  最后都会传到 httpsession中去.    *     2、在使用@SessionAttributes的时候可以不适用@ModelAttribute。如果不适用@ModelAttribute的话,那么当@SessionAttributes    *     没有的话,就会新建一个该对象存入@SessionAttributes    */   @Autowired    private IUserService userService;// @ModelAttribute   public void model(int id,Model model) {      User user=userService.getUser(id);      model.addAttribute("user", user);      model.addAttribute("haha", "hahahhaha!");      model.addAttribute("hehe", "heheheheheh");      model.addAttribute("user2", new User(22, "小白", "heheh", "m", "sichuanqu"));   }   //验证猜想1   @RequestMapping("/update")   public String updateUser(User user) {      System.out.println("User--------------------------------");      //执行完下面这一步后,原@SessionAttributes中的user将被更新      user.setName("hahahahhah");      return "home2";   }   //验证猜想2//    @RequestMapping("/update")      public String updateUser2(User user) {         System.out.println("User--------------------------------");         return "home2";      }   /**通过@ModelAttribute@SessionAttributes实现登录登出    *     */   @RequestMapping("/login")   public String login(String name,String psw,Model model) {      System.out.println("-----------进来了---------------");      User user=userService.login(name, psw);      model.addAttribute("user", user);      model.addAttribute("haha", "登录成功!");      if(user!=null) {         model.addAttribute("haha", "登录成功!");      }else {         model.addAttribute("haha", "登录失败!");      }      return "home2";   }   /**    * 登录是通过session来保持的,那么要如何消除掉session呢? 如果只消除Httpsession那么能成功登出吗?    * 验证:失败    * 原因:由于将HttpSession中的user清楚了  session中没有user属性,但是在方法执行完毕后由于但是在@SessionAttributes    * 内还存在user属性。所有@SessionAttributes又把user设置进了HttpSession    * @return    */// @RequestMapping("loginout")   public String loginout(HttpSession session) {      session.removeAttribute("user");      return "home2";   }   /**    * 登出成功!  将HttpSessionSessionStatus用时清楚    * @param session    * @param ss    * @return    */   @RequestMapping("loginout")   public String loginout2(HttpSession session, SessionStatus ss) {      session.removeAttribute("user");      ss.setComplete();      return "home2";   }   }




原创粉丝点击