[SpringMVC] Response-Jump/modify view page + key-value pairs in the operation field

1. Response


 

1 Data response method

1) Page jump-directly return to the string / return to the ModelAndView object

2) Write back data-directly return to the string / return to the object or collection

 

2.1 Page jump-return to the string directly

The direct returnstring will be spliced ​​with the prefix and suffix of the view parser configured in spring-mvc.xml.

@RequestMapping("/test0")
public String test0() {
    
    
    return "hello.jsp";
}

 

2.2 Page Jump-Return to ModelAndView Object

-------------------------------------------------------------------------------------------

形式1: 在方法内部`new`出一个ModelAndView对象,并设置其模型和视图
形式2: 方法的参数直接写ModelAndView,SpringMVC就会自动帮我们创建这个对象,我们在方法内部就可以直接使用
形式3: 方法的参数直接写HttpServletRequest,我们之前学习Web时就是利用该对象向request域设置数据的(不推荐这种原生方法)

-------------------------------------------------------------------------------------------

@RequestMapping("/test1")
public ModelAndView test1() {
    
    
    ModelAndView modelAndView = new ModelAndView();
    // 设置模型
    modelAndView.addObject("username", "Alice");
    // 设置视图
    modelAndView.setViewName("hello.jsp");
    return modelAndView;
}


@RequestMapping("/test2")
public ModelAndView test2(ModelAndView modelAndView) {
    
    
    // 设置模型
    modelAndView.addObject("username", "Alice");
    // 设置视图
    modelAndView.setViewName("hello.jsp");
    return modelAndView;
}

@RequestMapping("/test2")
public String test2(Model model) {
    
    
	// 这种写法不难理解
    model.addAttribute("username", "Alice");
    return "hello.jsp";
}


@RequestMapping("/test3")
public String test3(HttpServletRequest request) {
    
    
	// 之前学习Web时就是利用该对象向request域设置数据的(原生写法,并未使用框架)
    request.setAttribute("username", "Alice");
    return "hello.jsp";
}

 
 

3.1 Write back data-return a string directly

When learning the Web, the way to write back data is response.getWriter().print(“hello world”). In the SpringMVC framework, we can also (but not recommended) use this way of using native Request/Responce objects:

@RequestMapping("/test4")
public void test4(HttpServletResponse response) throws IOException {
    
    
    response.getWriter().print("萝莉suki");
}

We can even direct the returnstring-just use the @ResponseBody annotation to tell the framework not to confuse the view jump directly : return

@RequestMapping("/test5")
@ResponseBody
public String test5() {
    
    
    return "萝莉suki";
}

 

3.2 Write back data-directly return json format string

In actual development scenarios, all we return are json strings. Using jackson can directly convert objects into json strings, which is extremely convenient.

@RequestMapping("/test6")
@ResponseBody
public String  test6() throws IOException {
    
    
    User user = new User("Alice", 12);
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(user);
    return json;
}

 

3.3 Write back data-return object or collection

This "converting an object into a json string" operation is extremely frequent, and it seems troublesome to always use the above method. Fortunately, the SpringMVC framework can automatically achieve this conversion, and we only need to perform a simple configuration (spring-mvc.xml).

Also, don’t forget to write @ResponseBody annotations!

<!-- 配置处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
        </list>
    </property>
</bean>
@RequestMapping("/test7")
@ResponseBody
public User test7() throws IOException {
    
    
	// 直接返回对象/集合即可,框架帮我们做好了一切
    User user = new User("Alice", 12);
    return user;
}

It can even be simpler.

<mvc:annotation-driven />Can automatically load RequestMappingHandlerMapping (processor mapper) and

RequestMappingHandlerAdapter (processor adapter), and its default bottom layer has integrated Jackson's automatic conversion.

In other words, the extremely complicated configuration above becomes the following sentence:

<!-- 配置mvc注解驱动(自动加载处理器映射器+处理器适配器) -->
<mvc:annotation-driven />

 
 

4. Explanation of confusion

① Isn't the "Model" in the "return to ModelAndView object" in the page jump count as writing back data?

  These are two different things. The returned "Model" is indeed data, but these data are actually written in the Request field; and "write-back data" refers to writing some data directly to the displayed page through Responce .

② Sometimes I can’t figure it out or forget to write @ResponseBody annotation. How can I judge whether to write it or not?

  It's very simple. Regarding "page jump", the essence is to use the Request object, so the annotation is not written; regarding "write back data", the essence is to use the Responce object, so the annotation must be written. In other words, the meaning of adding the @ResponseBody annotation is to tell the SpringMVC framework not to jump .

③ Can the write-back data in response be understood as "both character string and object/collection can be written back"?

  What is written back is actually "normal string and json string".

④ I still can’t distinguish between page jump and write-back data?

Page jump is to jump to another page displaying brand new information. Writing back data is only a partial modification of the information displayed on the original page; as for the key-value pairs in the operation field, it is another matter.

Insert picture description here

 
 
 
 

 
 
 
 

 
 
 
 

More >_<

Guess you like

Origin blog.csdn.net/m0_46202073/article/details/114304113