Several points to pay attention to when using Spring MVC

  Question 1: How to specify a request method to request? Such as get, post

  Spring MVC obtains the request using @RequestMapping annotation.
  There are two commonly used forms of this annotation. The second method means that the method is limited to the specified request method.

@RequestMapping("/testE")
public ModelAndView testE(ModelAndView model){
    
    ...}
@RequestMapping(value = "/testE",method = RequestMethod.POST)
public ModelAndView testE(ModelAndView model){
    
    ...}

  Question 2: Because Spring MVC will automatically perform the process of obtaining data and encapsulating objects, if the Web side passes a name parameter, when the Java side accepts the method parameters, there is both a String name and an object of the Test1Model class, and the Test1Model class There is an attribute name in the name, what will happen?

  Both will get the value of the name parameter passed from the Web terminal.

@RequestMapping("/testE")
public ModelAndView testE(ModelAndView model, String name){
    
    ...}

  Question 3: What if there is more than one value passed for a certain parameter on the Web side? Such as checkbox

  Use an array to get

//http://127.0.0.1:8080/spring-mvc/test1/testD.do?hobby=qwer&hobby=asdf&aa=bb
@RequestMapping("/testD")
public String doD(String aa,String[] hobby){
    
    ...}

  Question 4: In Spring MVC, can I not use traditional JavaWeb project objects? For example, HttpServletRequest class object request, HttpServletResponse class object response, HttpSessio class object session.

  Can be used, if you want to use these variables, you only need to define the corresponding type of formal parameter variables in the method

@RequestMapping("/testF")
public ModelAndView testF(HttpServletRequest request,String aa){
    
    ...}

  Other HttpServletResponse classes, HttpSession classes, etc. are also used in the same way, but pay attention to introducing the Serlvet API dependency in the Maven configuration file pom.xml.

  Question 5: By default, the returned result will perform request forwarding and go to the view parser for prefix and suffix splicing. What should I do if I don't want splicing?

  Use forward:/web/page/index.jspit will not be before the suffix stitching, forward: the back is the path you want to forward, and can be forwarded to a page, it can also be forwarded to the controller.

  Question 6: How to perform request redirection?

  Use redirect:/web/page/index.jsprequest redirection, redirect: the latter is required to redirect the path. But be careful! In traditional JavaWeb projects, the request redirection path needs to write the project name, but in Spring MVC, there is no need to write the project name.

  Restful request path : It is a development method that is suitable for the complete separation of front and back ends.

Guess you like

Origin blog.csdn.net/qq_40395874/article/details/114438766