Daniel, teach you hand in hand! Spring MVC - Crazy God notes

  1. Write web XML, register DispatcherServlet

  2. Writing spring MVC configuration files

  3. The next step is to create the corresponding control class, controller

  4. Finally, improve the correspondence between the front-end view and the controller (the parameters of Model type are declared in the method to bring the data in the Action to the view)

  5. Test run commissioning

Note: DispatcherServlet is the unified entry of spring MVC, and all requests pass through it. Dispatcher Servlet is a front-end Controller configured on the web In the XML file, the Servlet intercepts matching requests according to specific rules defined by itself and distributes them to the target Controller for processing.

Three pieces (annotation or xml) that must be configured to use spring MVC:

Processor mapper, processor adapter, view parser

ModelAndView: model view class (the result returned by the method is the view name hello, and the prefix in the configuration file (view parser) becomes WEB-INF/jsp/hello.jsp.)

 //ModelAndView models and views

       ModelAndView mv = new ModelAndView();



       //Encapsulate the object and place it in ModelAndView. Model

       mv.addObject("msg","HelloSpringMVC!");

       //Encapsulate the view to jump to and put it in ModelAndView

       mv.setViewName("hello"); //: /WEB-INF/jsp/hello.jsp

       return mv;

Section III:

  • POST, DELETE, PUT, GET: add, DELETE, modify, query

RESTful style:

  • In Spring MVC, you can use the @ PathVariable annotation to bind the value of the method parameter to a URI template variable.
@Controller

public class RestFulController {

   //Map access path

   @RequestMapping("/commit/{p1}/{p2}")

   public String index(@PathVariable int p1, @PathVariable int p2, Model model){

       

       int result = p1+p2;

       //Spring MVC will automatically instantiate a Model object to pass values to the view

       model.addAttribute("msg", "result:"+result);

       //Return to the view location (the view parser parses and finds the corresponding file)

       return "test";

       

  }  

}

Use the method property to specify the request type

It is used to constrain the type of request and narrow the request range. Specify the type of request predicate, such as GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE, etc

//The mapped access path must be a POST request

@RequestMapping(value = "/hello",method = {RequestMethod.POST})

public String index2(Model model){

   model.addAttribute("msg", "hello!");

   return "test";

}

```![wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==](data:image/gif;base64,R0lGODlhAQABAPABAP///Waaaach5baekaaaalaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa



**All address bar requests will be by default HTTP GET Type.**



* * *



### Section IV:



**Result jump mode**



*   ModelAndView: page : {View parser prefix} + viewName +{View parser suffix}

*   ServletAPI: adopt HttpServletResponse Output, redirect and forward



@RequestMapping("/result/t1")

public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {

   rsp.getWriter().println("Hello,Spring BY servlet API");

}

@RequestMapping("/result/t2")

public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {

   rsp.sendRedirect("/index.jsp");

}

@RequestMapping("/result/t3")

public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {

   //forward

   req.setAttribute("msg","/result/t3");

   req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);

}



*   adopt SpringMVC To achieve forwarding and redirection - No view parser is required;



@RequestMapping("/rsm/t1")

public String test1(){

   //forward

   return "/index.jsp";

}



**Data submission:**



@RequestMapping("/hello")

summary

Ant interview pays more attention to the foundation, so those basic skills of Java must be solid. The working environment of ants is still very good, because I meet the stability guarantee department, and there are many separate groups. What is class 1 in three years, I feel very young. The basic level of the interviewers is quite high, basically above P7. In addition to the foundation, they also asked a lot of questions about architecture design, and the harvest is still very big.

Data collection method: stamp here

After this interview, I also found the need for real interviews with large factories through some channels, mainly including ant financial, pinduoduo, Alibaba cloud, Baidu, vipshop, Ctrip, Fengchao technology, Lexin, softcom power, OPPO, Yinsheng payment, China Ping An and other early, intermediate and advanced Java interview questions, with super detailed answers. I hope I can help you.

The feeling of. The basic level of the interviewers is quite high, basically above P7. In addition to the foundation, they also asked a lot of questions about architecture design, and the harvest is still very big.

Data collection method: stamp here

After this interview, I also found the need for real interviews with large factories through some channels, mainly including ant financial, pinduoduo, Alibaba cloud, Baidu, vipshop, Ctrip, Fengchao technology, Lexin, softcom power, OPPO, Yinsheng payment, China Ping An and other early, intermediate and advanced Java interview questions, with super detailed answers. I hope I can help you.

Keywords: Java Back-end Interview Programmer

Added by hawkeyes on Wed, 29 Dec 2021 21:12:48 +0200