In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of a sample analysis of how to request controller access based on spring mvc. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Spring mvc requests controller to access 1. A Controller contains different request url@Controller / / Struts-like Actionpublic class TestController {@ RequestMapping ("test/login.do") / / request url address mapping Struts-like action-mapping public String testLogin (@ RequestParam (value= "username") String username, String password, HttpServletRequest request) {/ / @ RequestParam refers to the parameter that must be included in the request url address mapping (unless the attribute required=false) / @ RequestParam can be abbreviated to: @ RequestParam ("username") if (! "admin" .equals (username) | |! "admin" .equals (password)) {return "loginError" / / Redirect page path (default is forwarding), which does not need to contain the prefix and suffix} return "loginSuccess";}} 2 configured in the spring-servlet configuration file. Use one url access
Distinguish between different access methods through the url parameter
@ Controller@RequestMapping ("/ test2/login.do") / / specifies that the only * .do request is associated with the Controllerpublic class TestController2 {@ RequestMapping public String testLogin (String username, String password, int age) {/ / if no parameters are added, when requesting / test2/login.do The method if (! "admin" .equals (username)) is executed by default | |! "admin" .equals (password) | | age < 5) {return "loginError" } return "loginSuccess" } @ RequestMapping (params = "method=1", method=RequestMethod.POST) public String testLogin2 (String username, String password) {/ / distinguish different calling methods according to the value of the parameter method of params. / / you can specify the type of page request by default. 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 regarded as the parent Request request url, while the RequestMapping method can be regarded as the child Request request url, and the parent-son request url will eventually be put together to match the page request url.
@ Controller@RequestMapping ("/ test3/*") / / parent request request urlpublic class TestController3 {@ RequestMapping ("login.do") / / child request request url, which 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. Notes commonly used in SpringMVC
There is also @ PathVariable,@RequestParam,@PathVariable marked on the parameters of the method, and the parameters marked with it can be passed values using the request path
@ Controller / / Actionpublic class TestController {@ RequestMapping (value= "/ comment/ {blogId}", method=RequestMethod.POST) public void comment (Comment comment,@PathVariable int blogId) throws IOException {}} springmvc request similar to Struts once and access multiple controller methods
There is a requirement: one request, access to methods in multiple controller
For example, first execute the query operation, and then update the content of the query (of course, you can also write the method to bo and call the method of bo directly in controller. Here is just an example)
Give an example
JSP page
Execute two methods 1 7000 edits in one action
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 another method of EmpAction, that is, send the request / / return "forward:/emp/update" again / / redirect to another method of EmpAction, that is, send the request 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 successful") Return "success";}} conclusion
1. ModelAndView cannot realize the data transfer between the two methods.
two。 It can be passed through Session.
There are several ways to achieve Session delivery
Method 1: use HttpServletRequest as a method parameter through request.getSession (). AddAttribute
Method 2: use HttpSession as the method parameter
Method 3: pass through @ SessionAttribute+@ModelAttribute
Use HttpSession to access data, but this uses the content of servlet in springmvc, which is not good
3. Use forwarding. In the case of forwarding, the request domain object is shared, and parameters are passed from the first service control method to the second service control method.
Return "forward:/emp/update.action"
4. Redirection does not share parameters, so it is necessary to take parameters.
Return "redirect:/emp/update.action?id=" + id; Thank you for reading! This is the end of the article on "sample Analysis of controller access based on spring mvc requests". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.