Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of Java SpringMVC data response

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Java SpringMVC data response case analysis". In daily operation, I believe many people have doubts about Java SpringMVC data response case analysis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "Java SpringMVC data response case analysis". Next, please follow the editor to study!

1) Page jump

Return the string directly: this way jumps the returned string after concatenating the suffix of the view parser.

Returns a string with a prefix:

Forward: forward:/WEB-INF/views/index.jsp

Redirect: redirect:/index.jsp

Returned through the ModelAndView object

@ RequestMapping ("/ quick2") public ModelAndView quickMethod2 () {ModelAndView modelAndView = new ModelAndView (); modelAndView.setViewName ("redirect:index.jsp"); return modelAndView;} @ RequestMapping ("/ quick3") public ModelAndView quickMethod3 () {ModelAndView modelAndView = new ModelAndView (); modelAndView.setViewName ("forward:/WEB-INF/views/index.jsp"); return modelAndView;}

When forwarding, you often store data in the request field and display it in the jsp page, so how do you store data in the Controller domain into the request domain?

① is set through the setAttribute () method of the request object injected by the SpringMVC framework.

@ RequestMapping ("/ quick") public String quickMethod (HttpServletRequest request) {request.setAttribute ("name", "zhangsan"); return "index";}

② is set through the addObject () method of ModelAndView.

@ RequestMapping ("/ quick3") public ModelAndView quickMethod3 () {ModelAndView modelAndView = new ModelAndView (); modelAndView.setViewName ("forward:/WEB-INF/views/index.jsp"); modelAndView.addObject ("name", "lisi"); return modelAndView;} 2) write back data

Directly return the string: Web basic phase, the client accesses the server side, if you want to directly write back the string as the response body, you only need to use response.getWriter (). Print ("hello world"), so what if you want to write back the string directly in Controller?

The response object injected by ① through the SpringMVC framework writes back the data using response.getWriter (). Print ("hello world"), which does not require a view jump, and the business method returns a value of void.

@ RequestMapping ("/ quick4") public void quickMethod4 (HttpServletResponse response) throws IOException {response.getWriter () .print ("hello world");}

② returns the string that needs to be written back directly, but at this point you need to tell the SpringMVC framework through the @ ResponseBody annotation that the string returned by the method is either jumped or returned directly in the http response body.

@ RequestMapping ("/ quick5") @ ResponseBodypublic String quickMethod5 () throws IOException {return "hello springMVC programs!";}

In development, we often have to convert complex java objects into strings in json format. We can use jackson, a json conversion tool learned in the web stage, to do the conversion.

1. Import jackson coordinates in pom.xml.

Com.fasterxml.jackson.core jackson-core 2.9.0 com.fasterxml.jackson.core jackson-databind 2.9.0 com.fasterxml.jackson.core jackson-annotations 2.9.0

two。 Convert the json format string through jackson and write back the string.

@ RequestMapping ("/ quick7") @ ResponseBodypublic String quickMethod7 () throws IOException {User user = new User (); user.setUsername ("zhangsan"); user.setAge (18); ObjectMapper objectMapper = new ObjectMapper (); String s = objectMapper.writeValueAsString (user); return s;}

Returns an object or collection

Use SpringMVC to help us convert and write back json strings of objects or collections, configure message conversion parameters for processor adapters, and specify to use jackson to convert objects or collections, so you need to configure them in spring-mvc.xml as follows:

Returns an object or collection directly in a method

@ RequestMapping ("/ quick8") @ ResponseBodypublic User quickMethod8 () throws IOException {User user = new User (); user.setUsername ("zhangsan"); user.setAge (18); return user;} 3) configure the annotation driver

Adding @ ResponseBody to the method can return a string in json format, but it is troublesome to configure and there is a lot of code to configure, so we can use mvc's annotation driver instead of the above configuration.

Among the components of SpringMVC, the processor mapper, the processor adapter, and the view parser are called the three components of SpringMVC.

Using autoload RequestMappingHandlerMapping (processing mapper) and RequestMappingHandlerAdapter (processing adapter) can be used to replace the configuration of annotation processors and adapters in Spring-xml.xml configuration files.

At the same time, using the default underlying layer will integrate jackson to convert the json format string of the object or collection.

4) key points of knowledge

The data response mode of SpringMVC

1) Page jump

Return the string directly

Returned through the ModelAndView object

2) write back data

Return the string directly

Returns an object or collection

At this point, the study on "Java SpringMVC data response case analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report