spring @ModelAttribute @SessionAttributes数据访问模型...

记录几个springMVC中容易混淆的注解合格几个常用的获取表单(json数据)的参数
@RequestMapping(value = "index1/{id}",method= RequestMethod.GET)
    public String index1(@PathVariable Integer id,Model model ){
        System.out.println("id:"+id);
//        ModelAndView modelAndView=new ModelAndView();
        List<Integer> list=new ArrayList<Integer>();
        for (int i=0;i<10;i++){
            list.add(i);
        }
        model.addAttribute("list",list);
        return "/test/test";
    }

    @RequestMapping(value = "index",method= RequestMethod.POST)
    @ResponseBody//返回的数据自动转化为json数据
    public Map<String, Object> index(HttpServletRequest request, @RequestParam(value = "username",required = false) String username ){
//        @PathVariable Integer mm       @RequestParam

        System.out.println("username:"+username);
//        System.out.println(request.getAttribute("password"));
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("msg", 1);

       return modelMap;
    }
    /**
     * 测试model
     * @return
     */
    @RequestMapping("index")
    public String index(Model model){
        User user=new User();
        user.setUsername("chenlei");
        user.setPassword("123456");
        model.addAttribute(user);
        return "test/index";
    }
    /**
     * 测试model
     * @return
     */
    @RequestMapping(value = "index/form",method = RequestMethod.POST)
    public ModelAndView index(@RequestBody User user,Model model){
        System.out.println("用户名:"+user.getUsername()+"密码:"+user.getPassword());
//        user.setUsername(user.getUsername());
//        model.addAttribute(user);    ok

        ModelAndView modelAndView=new ModelAndView();
//        addObject( java.lang.Object attributeValue);
//        addObject(java.lang.String attributeName, java.lang.Object attributeValue)
        modelAndView.addObject("msg","okoko");
        modelAndView.setViewName("test/index");
        return modelAndView;
    }

    //方法仅处理request Content-Type为“application/json”类型的请求。
    @RequestMapping(value = "index4",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> index4(HttpEntity<User> entity){
        User user =entity.getBody();
        System.out.println("用户名:"+user.getUsername()+"密码:"+user.getPassword());
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("msg", "json send ok!");
        return modelMap;
    }

 @Service 用于标注业务层组件,@Controller用于标注控制层组件(如struts中的action,@Repository用于标注数据访问组件,即DAO组件

@Controller
@RequestMapping("/user")
@SessionAttributes(“user”)
public class UserController {              
                   @ModelAttribute("user")//

该方法会往隐含模型中添加一个名为user的模型属性

public User getUser(){ User user = new User(); return user; } @RequestMapping(value = "/handle71") public String handle71(@ModelAttribute(“user”) User user){ ... } @RequestMapping(value = "/handle72") public String handle72(ModelMap modelMap,SessionStatus sessionStatus){ ... } }



使用一:

@RequestMapping(value = "/handle61")
public String  handle61(@ModelAttribute("user") User user){
	user.setUserId("1000");
	return "/user/createSuccess";
}

使用2:

访问UserController中任何一个请求处理方法前,Spring MVC先执行该方法,并将返回值以user为键添加到模型中

在此,模型数据会赋给User的入参,然后再根据HTTP请求消息进一步填充覆盖user对象

@ModelAttribute("user")
public User getUser(){
	User user = new User();
	user.setUserId("1001"); 
	return user;
}

@RequestMapping(value = "/handle62")
public String  handle62(@ModelAttribute("user") User user){
	user.setUserName("tom");
	return "/user/showUser";
} 





@Controller
@RequestMapping("/user")
@SessionAttributes(“user”)①
public class UserController {
	
	@RequestMapping(value = "/handle71")
	public String  handle71(@ModelAttribute(“user”) User user){②//自动保存在session
		user.setUserName("John");
		return "redirect:/user/handle72.html";
	}
	
	@RequestMapping(value = "/handle72")
	public String  handle72(ModelMap modelMap,SessionStatus sessionStatus){
		User user = (User)modelMap.get(“user”);③//读取模型数据
		if(user != null){
			user.setUserName("Jetty");
			sessionStatus.setComplete();④	//清除处理器对应的会话属性
		}
		return "/user/showUser";
	}		
}

你可能感兴趣的:(spring @ModelAttribute @SessionAttributes数据访问模型...)