Processing springmvc (5) data

1. processing data submitted

The domain a) submitted by the same name and parameters can be submitted:

http://localhost:8080/hello?name=zhangsan

   @RequestMapping("/hello")
    public String hello(String name){
        System.out.println(name);
        return "hello";
    }

b) If the domain name and parameter names submitted by inconsistency:

http://localhost:8080/hello?uname=zhangsan

  @RequestMapping("/hello")
    public String hello(@RequestParam("uname") String name){
        System.out.println(name);
        return "hello";
    }

If uname not turn around, will report an error

c) If the submission is an object:

The same property name and domain name of the object submitted, the parameters of the processor using the corresponding object can be:

http://localhost:8080/user?name=zhangsan&pws=123

 @RequestMapping("/user")
    public String user(User user){
        System.out.println(user);
        return "hello";
    } 

Entity classes:
public class User {
private String name;
private String pws;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPws() {
return pws;
}

public void setPws(String pws) {
this.pws = pws;
}
}
 

2. The display data to the ui layer:

a) by ModelAndView, there is no view resolvers are the same, it is necessary to return ModelAndView.

  @RequestMapping("/hello")
    public ModelAndView hello(HttpServletRequest request,HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        //相当于request.setAttribute("msg","modelAndView");
        modelAndView.addObject("msg","modelAndView");
        modelAndView.setViewName("hello");
        return modelAndView;
    }

b) By ModelMap, there is no view resolvers are the same, but the parameters need to be as ModelMap processor.

@RequestMapping("/hello")
public String hello(ModelMap modelMap){
//相当于request.setAttribute("msg","modelMap");
modelMap.addAttribute("msg","modelMap");
return "index.jsp";
}

 

Guess you like

Origin www.cnblogs.com/yuby/p/11019847.html