Data processing for spring mvc in java

Keywords: PHP Spring JSP

1. How to handle data submission from pages to Controller

a) If a custom Controller is the interface that implements spring's Controller, data can be obtained through HttpServletRequest.

b) If the custom Controller does not implement spring's Controller interface, you can automatically inject the HttpServletRequest into the parameter by adding a parameter of type HttpServletRequest to the processing method when the request is processed by the method.

Note: Form data can be retrieved in both ways, but in the same way as the servlet, the values in the form field are automatically injected directly into the parameters when the data is submitted.

c) When you submit data, the values in the form field are automatically injected directly into the parameters if the parameters are declared directly, and the parameter names are consistent with the names of the fields in the form.

Jsp Page

<form action="login.do" method="post">
    username:<input type="text" name="username"/><br>
    password:<input type="password" name="password"/><br>
    <input type="submit" value="Sign in"/>
 </form>

Controller class

@RequestMapping("/login.do")
    public ModelAndView login(String username,String password){
        ModelAndView mv = new ModelAndView();
        System.out.println("username="+username+"  \tpassword="+password);
        if("siggy".equals(username)&&"1111".equals(password)){
            mv.addObject("msg", "Login successful!!!");
            mv.setViewName("index");
        }else{
            mv.setViewName("login");
        }
        return mv;
    }

If the name and parameter names in the form field are inconsistent, you can declare them using @RequestParam("username")

@RequestMapping("/login.do")
    public ModelAndView login(@RequestParam("username")String user,String password){
        ModelAndView mv = new ModelAndView();
        System.out.println("username="+user+"  \tpassword="+password);
        if("siggy".equals(user)&&"1111".equals(password)){
            mv.addObject("msg", "Login successful!!!");
            mv.setViewName("index");
        }else{
            mv.setViewName("login");
        }
        return mv;
    }

d) Processing of submitted objects:

If you submit an object in a form field, the name of the field is the property name of the vo class, the get/set method is provided in the vo class, and the parameter is declared as an object of vo type directly on the processing method in the Controller class.

vo type

public class User {
    private String name;
    private String pwd;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

Jsp Page

<body>
    <form action="login.do" method="post">
    username:<input type="text" name="name"/><br>
    password:<input type="password" name="pwd"/><br>
    <input type="submit" value="Sign in"/>
    </form>
</body>

Controller class

@Controller
public class UserController {
    @RequestMapping("/toLogin.do")
    public ModelAndView toLogin(){
        return new ModelAndView("login");
    }
    @RequestMapping("/login.do")
    public ModelAndView login(User user){
        ModelAndView mv = new ModelAndView();
        System.out.println("username="+user.getName()+"  \tpassword="+user.getPwd());
        if("siggy".equals(user.getName())&&"1111".equals(user.getPwd())){
            mv.addObject("msg", "Login successful!!!");
            mv.setViewName("index");
        }else{
            mv.setViewName("login");
        }
        return mv;
    }
}

 

2. How the data responds from the background to the page

a) Data can be brought to the foreground through the Servlet API, which is consistent with servlet processing.

@RequestMapping("/login.do")
    public ModelAndView login(@RequestParam("username")String user,String password,HttpServletRequest req){
        ModelAndView mv = new ModelAndView();
        System.out.println("username="+user+"  \tpassword="+password);
        req.setAttribute("hello", "world");
        if("siggy".equals(user)&&"1111".equals(password)){
            mv.addObject("msg", "Login successful!!!");
            mv.setViewName("index");
        }else{
            mv.setViewName("login");
        }
        return mv;
    }

b) Carry data to the foreground through ModelAndView, which uses el and jstl to get data

@RequestMapping("/login.do")
    public ModelAndView login(User user){
        ModelAndView mv = new ModelAndView();
        System.out.println("username="+user.getName()+"  \tpassword="+user.getPwd());
        if("siggy".equals(user.getName())&&"1111".equals(user.getPwd())){
            mv.addObject("msg", "Login successful!!!");
            mv.setViewName("index");
        }else{
            mv.setViewName("login");
        }
        return mv;
    }

c) You can also carry data through ModelMap, which is a type of data that needs to be declared in the parameters of the processing method:

@RequestMapping("/login.do")
    public ModelAndView login(User user,ModelMap map){
        ModelAndView mv = new ModelAndView();
        System.out.println("username="+user.getName()+"  \tpassword="+user.getPwd());
        if("siggy".equals(user.getName())&&"1111".equals(user.getPwd())){
            map.addAttribute("msg", "Login successful!!!");
            mv.setViewName("index");
        }else{
            mv.setViewName("login");
        }
        return mv;
    }

d) You can also carry data through Model, which is a type of data that needs to be declared in the parameters of the processing method

@RequestMapping("/login.do")
    public ModelAndView login(User user,Model model){
        ModelAndView mv = new ModelAndView();
        System.out.println("username="+user.getName()+"  \tpassword="+user.getPwd());
        if("siggy".equals(user.getName())&&"1111".equals(user.getPwd())){
            model.addAttribute("msg", "Login successful!!!");
            mv.setViewName("index");
        }else{
            mv.setViewName("login");
        }
        return mv;
    }

Posted by the7soft.com on Tue, 30 Jul 2019 10:20:03 -0700