Usage 1: Multiple URL paths map to the same Handler (same method)
- The index. The JSP page
<fieldset>
<h4>Usage 1: Multiple URL paths map to the same Handler (same method)</h4>
<a href="http://localhost:8080/gotoResultURL1.do">Test Path 1</a>
<a href="${pageContext.request.contextPath}/gotoResultURL2.do">Test Path 2</a>
</fieldset>
Copy the code
- DefaultController controller
public class DefaultController {
// Usage 1: Multiple URL paths map to the same Handler (same method)
@RequestMapping(value={"gotoResultURL1","gotoResultURL2"})
public ModelAndView gotoResultURL(ModelAndView modelAndView){
// Encapsulate data
modelAndView.addObject("nowDate".new Date() +"====Default===gotoResultURL");
// Specify the page
modelAndView.setViewName("result");
returnmodelAndView; }}Copy the code
Usage 2:RequestMapping annotations apply to classes to classify request paths and define the prefix used to access methods in a class
- The index. The JSP page
<fieldset>
<h4>Usage 2:RequestMapping annotations apply to classes to classify request paths and define the prefix used to access methods in a class</h4>
<a href="http://localhost:8080/default/gotoResultURL1.do">Testing the Default path</a>
<a href="${pageContext.request.contextPath}/user/gotoResultURL1.do">Testing the User path</a>
</fieldset>
Copy the code
- DefaultController controller
@Controller
// Usage 2:RequestMapping annotations apply to classes to categorize request paths and define the prefix used to access methods in a class
@RequestMapping("default")
public class DefaultController {
// Usage 1: Multiple URL paths map to the same Handler (same method)
@RequestMapping(value={"gotoResultURL1","gotoResultURL2"})
public ModelAndView gotoResultURL(ModelAndView modelAndView){
// Encapsulate data
modelAndView.addObject("nowDate".new Date() +"====Default===gotoResultURL");
// Specify the page
modelAndView.setViewName("result");
returnmodelAndView; }}}Copy the code
- UserController controller
@Controller
@RequestMapping("user")
public class UserController {
// Usage 1: Multiple URL paths map to the same Handler (same method)
@RequestMapping(value={"gotoResultURL1","gotoResultURL2"})
public ModelAndView gotoResultURL(ModelAndView modelAndView){
// Encapsulate data
modelAndView.addObject("nowDate".new Date() +"===User===gotoResultURL");
// Specify the page
modelAndView.setViewName("result");
returnmodelAndView; }}Copy the code
Usage 3 Method Attribute specifies the request method: The request handler is the same, and different request methods are used to process the request
The method attribute defines the request method. The request handler is the same, the request method is different, the access url is the same (handler name is the same), but THE request method (get/ POST) is different, the request URL is the same. But the request is different (get/post)
- index.jsp
<fieldset>
<h4>Usage 3 Method Attribute specifies the request method: The request handler is the same, and different request methods are used to process the request</h4>
<a href="http://localhost:8080/default/gotoResultMethod.do">Get test</a>
<form action="http://localhost:8080/default/gotoResultMethod.do" method="post">
<input type="submit" value="Post test">
</form>
</fieldset>
Copy the code
- DefaultController controller
/** * the same url, but the request method is GET *@RequestMappingThe annotation attribute method */
@RequestMapping(value = "gotoResultMethod",method = RequestMethod.GET)
public ModelAndView gotoResultMethodGet(ModelAndView modelAndView){
modelAndView.addObject("nowDate".new Date()+"Method=GET");
modelAndView.setViewName("index");
return modelAndView;
}
/** * the same url, but the request is POST *@RequestMappingThe annotation attribute method */
@RequestMapping(value = "gotoResultMethod",method = RequestMethod.POST)
public ModelAndView gotoResultMethodPost(ModelAndView modelAndView){
modelAndView.addObject("nowDate".new Date()+"Method=POST");
modelAndView.setViewName("index");
return modelAndView;
}
Copy the code
Usage 4 Params attribute qualifiers for request parameters: Supports simple expression syntax, like URLS, which enter different methods based on the parameters
The params attribute defines request parameters and supports simple expression syntax. The same as url, different hanlder methods are used for processing according to different parameters. The same URL, the same request mode, different request parameters are processed by different Hanlder methods
- The index. The JSP page
<fieldset>
<h4>Usage 4 Params attribute qualifiers for request parameters: Supports simple expression syntax, like URLS, which enter different methods based on the parameters</h4>
<a href="http://localhost:8080/user/login.do? type=user">The average user</a>
<a href="http://localhost:8080/user/login.do? type=admin">The administrator</a>
<a href="http://localhost:8080/user/login.do? type=vip">VIP</a>
</fieldset>
Copy the code
- UserController controller
@Controller
@RequestMapping("user")
public class UserController {
// Common user
@RequestMapping(value={"login"}, params = {"type=user"})
public ModelAndView loginUser(ModelAndView modelAndView){
// Encapsulate data
modelAndView.addObject("nowDate".new Date() +"===user");
// Specify the page
modelAndView.setViewName("result");
return modelAndView;
}
/ / administrator
@RequestMapping(value={"login"}, params = {"type=admin"})
public ModelAndView loginAdmin(ModelAndView modelAndView){
// Encapsulate data
modelAndView.addObject("nowDate".new Date() +"===admin");
// Specify the page
modelAndView.setViewName("result");
return modelAndView;
}
//VIP
@RequestMapping(value={"login"}, params = {"type=vip"})
public ModelAndView loginVIP(ModelAndView modelAndView){
// Encapsulate data
modelAndView.addObject("nowDate".new Date() +"===VIP");
// Specify the page
modelAndView.setViewName("result");
return modelAndView;
}
* @requestMapping attribute params * id: indicates that the request must contain a request parameter named ID * demo: http://localhost:8080/user/gotoResultParamsURL.do? id=11 * ! Id: according to the request cannot contain request parameter called id * presentation: http://localhost:8080/user/gotoResultParamsURL? Id = 123 * id = 100: said request contains request parameter called param1, but its value can't for 100 * presentation: http://localhost:8080/user/gotoResultParamsURL.do? Id = 100 * {" id! = 100 ", "name"}, the request must contain named id and the name of two request parameters, * http://localhost:8080/user/gotoResultParamsURL.do? id=123 */
@RequestMapping(value={"gotoResultParamsURL"}, params = {"id! =100","name"})
public ModelAndView gotoResultParamsURL (ModelAndView modelAndView){
// Encapsulate data
modelAndView.addObject("nowDate".new Date() +"===gotoResultParamsURL");
// Specify the page
modelAndView.setViewName("result");
returnmodelAndView; }}Copy the code