controller access mode based on spring mvc request

  • 2021-11-10 09:29:33
  • OfStack

Directory spring mvc Request controller Access 1.1 Controller contains different requests url 2. Adopt one url Access 3. RequestMapping on Class 4. Commonly used annotations in SpringMVC springmvc Request once, access multiple controller method example conclusion

spring mvc Request controller Access

1.1 Controller contains different requests url


@Controller  // Similar Struts Adj. Action
public class TestController {
    @RequestMapping("test/login.do")  //  Request url Address mapping, similar to Struts Adj. action-mapping
    public String testLogin(@RequestParam(value="username")String username, String password, HttpServletRequest request) {
        // @RequestParam Refers to the request url Parameters that must be included in the address map ( Unless the attribute required=false)
        // @RequestParam Can be abbreviated as: @RequestParam("username")
        if (!"admin".equals(username) || !"admin".equals(password)) {
            return "loginError"; //  Jump page path (default is forwarding), which does not need to contain spring-servlet Prefixes and suffixes configured in configuration files 
        }
        return "loginSuccess";
    }
}

2. Use an url access

Distinguish access to different methods through url parameters


@Controller
@RequestMapping("/test2/login.do")  //  Specify only 11 A *.do Request is associated with the Controller
public class TestController2 {
        @RequestMapping
    public String testLogin(String username, String password, int age) {
        //  If no parameters are added, the request is made in the /test2/login.do The method is executed by default 
        
        if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
            return "loginError";
        }
        return "loginSuccess";
    }
    @RequestMapping(params = "method=1", method=RequestMethod.POST)
    public String testLogin2(String username, String password) {
        //  Basis params Parameters of method To distinguish between different calling methods 
        //  You can specify the type of page request method, and the default is get Request 
        
        if (!"admin".equals(username) || !"admin".equals(password)) {
            return "loginError";
        }
        return "loginSuccess";
    }
    
    @RequestMapping(params = "method=2")
    public String testLogin3(String username, String password, int age) {
        if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
            return "loginError";
        }
        return "loginSuccess";
    }
}

3. RequestMapping on Class

It can be seen that the parent Request requests url, and the method of RequestMapping can be seen as the child Request requests url, and the parent-child request url will eventually be put together to match the page request url


@Controller
@RequestMapping("/test3/*")  //  Father request Request url
public class TestController3 {
    @RequestMapping("login.do")  //  Sub request Request url After splicing, it is equivalent to /test3/login.do
    public String testLogin(String username, String password, int age) {
        if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
            return "loginError";
        }
        return "loginSuccess";
    }
}

4. Commonly used annotations in SpringMVC

Also @ PathVariable, @ RequestParam, @ PathVariable are marked on the parameters of the method, and the parameters marked with it can be passed by using the request path


@Controller  // Similar Struts Adj. Action
public class TestController {
 @RequestMapping(value="/comment/{blogId}", method=RequestMethod.POST)
 public void comment(Comment comment,@PathVariable int blogId) throws IOException {    
 }
}

springmvc request once, accessing multiple controller methods

There is 1 requirement: request once and access multiple methods in controller

For example: Execute the query operation first, and then update the query content (of course, you can also write the method to bo, and call the method of bo directly in controller, here is just an example)

Example

JSP page


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> In 1 A action Execute two methods in the </title>
</head>
<body>
1  Ha ha  7000
<a href="${pageContext.request.contextPath}/emp/find?id=1" rel="external nofollow"  style="text-decoration:none"> Edit </a>
</body>
</html>

Controller page


@Controller
@RequestMapping("/emp")
public class EmpAction { 
    @RequestMapping(value="/find")
    public String findEmpById(int id) throws Exception{
        System.out.println(" Query "+id+" Employee information ");
 
        // Forward to EmpAction Another of 1 Methods, that is, send the request again 
//        return "forward:/emp/update";
 
        // Redirect to EmpAction Another of 1 Methods, that is, send the request again 
        return "redirect:/emp/update.action?id=" + id;
    }
 
    @RequestMapping(value="/update")
    public String updateEmpById(int id,Model model) throws Exception{
        System.out.println(" Update " + id +" Employee information ");
        model.addAttribute("message"," Update Employee Information Successfully ");
        return "success";
    }
}

Conclusion

1. ModelAndView does not realize data transfer between the two methods;

2. It can be delivered through Session.

There are several ways to implement Session delivery

Method 1: HttpServletRequest is used as a method parameter through request. getSession (). addAttribute

Method 2: Take HttpSession as the method parameter

Method 3: Pass through @ SessionAttribute + @ ModelAttribute

Use HttpSession to access data, but this uses the contents of servlet in springmvc, which is not good

3. Use forwarding. In the forwarding case, sharing the request domain object will pass parameters from the first service control method to the second service control method


return"forward:/emp/update.action";

4. Redirection does not share parameters, so you need to take parameters


return "redirect:/emp/update.action?id="+ id;

Related articles: