Introduction to the return value of the processor
- (1) Servlet returns the result to the browser
Forwarding: with data
Redirect: without data
Asynchronous: json data is sent to the js of the page, and the js organizes the label display
The code written in the Servlet to respond to the browser must call the response - (2) The method of springmvc has designed a variety of return value types
>ModelAndView
>void
"String: request forwarding, redirection, json data
The return value of the handler - ModelAndView
- (1) ModelAndView is the model view provided by SPringMVC
- (2) Function
Set data addObject(key,value)
Set the page setViewName (logical view) is the name of the file
@Controller public class Demo02ReturnController { //public return value type method name(parameter type parameter name){} @RequestMapping(path = "demo01.action",method = {RequestMethod.POST,RequestMethod.GET})//echo page public ModelAndView test01(){// Person p1 = new Person(1,"jack","1234"); Person p2 = new Person(2,"rose","1234"); List<Person> list = new ArrayList<Person>(); list.add(p1); list.add(p2); //Use ModelAndView to implement request forwarding ModelAndView mv = new ModelAndView(); mv.addObject("list",list); mv.setViewName("demo04_update_persons");//prefix + logical view + suffix //Physical view real file address /WEB-INF/jsp/demo04_update_persons.jsp //physical view = prefix + logical view + suffix return mv; } }
The return value of the handler - void
- (1) Write void on the return value type of the method
At this point, the parameter is written in the request, and the request is called with the set parameter and the request is forwarded or redirected - (2) Background code
- (3) Does not reflect the advantages of springmvc
Demo02ReturnController
@RequestMapping(path="demo02.action",method = {RequestMethod.GET,RequestMethod.POST}) public void test02(HttpServletRequest req, HttpServletResponse resp)throws Exception{ //request forwarding req.setAttribute("name","request forwarding"); req.getRequestDispatcher("demo05.jsp").forward(req,resp); }
The return value of the handler - String
- (1) Write String on the return value type of the method
The method returns the page name - (2) What if data is carried?
The method parameters are written on the Model, which is less View than ModelAndView
req.setAttriibute(key,val) req....forward(req,resp)
>Model is a model class provided by SpingMVC, this class does not need to create objects by itself, the system automatically creates
- (3) Model role
Model can be used to pass parameters to the page - (4) Background code
model.addAttribute("item", item);
@RequestMapping(path="demo03.action",method = {RequestMethod.GET,RequestMethod.POST}) public String test03(Model model){ //Model can set data, automatically brought to the page by the view resolver model.addAttribute("data","wHelloWord"); return "success"; //Logical View Filename }
The return value of the processor - String - forward and redirect
- (1) What is a logical view and a physical view
The page name is called the logical view, not the real page address
But it can be spliced by the view parser to get the real page address, that is, the physical view - (2) The return value can add instructions
"1: forward request forwarding
forward directive: physical view
"2: redirect redirection
redirect instruction: project access path + physical view
Features: The view parser does not splicing the prefix and suffix after the instruction - (3) Instructions can also access controller methods
@RequestMapping(path="demo04.action",method = {RequestMethod.GET,RequestMethod.POST}) public String test04(Model model){ //Model can set data, automatically brought to the page by the view resolver model.addAttribute("data","wHelloWord"); //return "forward:/WEB-INF/jsp/success.jsp"; // //return "redirect:http://www.baidu.com"; //External network //return "redirect:demo05.jsp"; // return "redirect:demo01.action"; // }
The return value of the processor - json data processing
- (1) When is json used?
ajax request - (2) javaBean object and json are converted to each other, such as Alibaba's fastjson
- (3) Convert the return value to json @ResponseBody
Annotation is added to the method, SpringMVC can automatically convert the return object of the method to json and send it to the page - (4) Parameter to json @RequestBody
Add the @RequestBody annotation to the front of the formal parameter. This annotation can automatically parse the json data sent by the page. After parsing, the data in the json will be automatically encapsulated into the formal parameter object.
pom.xml
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.74</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
TestPersnoToJson
public class TestPersnoToJson { @Test public void test01(){ Person p = new Person(1,"jack","1234"); String json = JSON.toJSONString(p);//Call the static method toJSONString, pass the parameter into the object, and convert the object into json System.out.println(json); } @Test public void test02(){ String json = "{\"id\":1,\"password\":\"1234\",\"username\":\"jack\"}"; Person p = JSON.parseObject(json,Person.class);//json to javaBean, refer to 1, json refer to class 2 System.out.println(p); } }