Java SpringMVC数据响应实例分析(java,springmvc,开发技术)

时间:2024-05-10 09:08:58 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

1)页面跳转

直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转。

Java SpringMVC数据响应实例分析

返回带有前缀的字符串:

转发: forward:/WEB-INF/views/index.jsp

重定向: redirect:/index.jsp

通过ModelAndView对象返回

@RequestMapping("/quick2")publicModelAndViewquickMethod2(){ModelAndViewmodelAndView=newModelAndView();modelAndView.setViewName("redirect:index.jsp");returnmodelAndView;}@RequestMapping("/quick3")publicModelAndViewquickMethod3(){ModelAndViewmodelAndView=newModelAndView();modelAndView.setViewName("forward:/WEB-INF/views/index.jsp");returnmodelAndView;}

在进行转发时,往往要向request域中存储数据,在jsp页面中显示,那么Controller中怎样向request 域中存储数据呢?

① 通过SpringMVC框架注入的request对象setAttribute()方法设置。

@RequestMapping("/quick")publicStringquickMethod(HttpServletRequestrequest){request.setAttribute("name","zhangsan");return"index";}

② 通过ModelAndView的addObject()方法设置。

@RequestMapping("/quick3")publicModelAndViewquickMethod3(){ModelAndViewmodelAndView=newModelAndView();modelAndView.setViewName("forward:/WEB-INF/views/index.jsp");modelAndView.addObject("name","lisi");returnmodelAndView;}

2)回写数据

直接返回字符串:Web基础阶段,客户端访问服务器端,如果想直接回写字符串作为响应体返回的话,只需要使用response.getWriter().print(“hello world”) 即可,那么在Controller中想直接回写字符串该怎样呢?

① 通过SpringMVC框架注入的response对象,使用response.getWriter().print(“hello world”) 回写数据,此时不需要视图跳转,业务方法返回值为void。

@RequestMapping("/quick4")publicvoidquickMethod4(HttpServletResponseresponse)throwsIOException{response.getWriter().print("helloworld");}

② 将需要回写的字符串直接返回,但此时需要通过@ResponseBody注解告知SpringMVC框架,方法 返回的字符串不是跳转是直接在http响应体中返回。

@RequestMapping("/quick5")@ResponseBodypublicStringquickMethod5()throwsIOException{return"hellospringMVC!!!";}

开发中往往要将复杂的java对象转换成json格式的字符串,我们可以使用web阶段学习过的json转换工具jackson进行转换,

1.在pom.xml中导入jackson坐标。

<!--jackson--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.9.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.9.0</version></dependency>

2.通过jackson转换json格式字符串,回写字符串。

@RequestMapping("/quick7")@ResponseBodypublicStringquickMethod7()throwsIOException{Useruser=newUser();user.setUsername("zhangsan");user.setAge(18);ObjectMapperobjectMapper=newObjectMapper();Strings=objectMapper.writeValueAsString(user);returns;}

返回对象或集合

通过SpringMVC帮助我们对对象或集合进行json字符串的转换并回写,为处理器适配器配置消息转换参数, 指定使用jackson进行对象或集合的转换,因此需要在spring-mvc.xml中进行如下配置:

<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"><propertyname="messageConverters"><list><beanclass="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean></list></property></bean>

直接在方法中返回对象或集合

@RequestMapping("/quick8")@ResponseBodypublicUserquickMethod8()throwsIOException{Useruser=newUser();user.setUsername("zhangsan");user.setAge(18);returnuser;}

3)配置注解驱动

在方法上添加 @ResponseBody就可以返回json格式的字符串,但是这样配置比较麻烦,配置的代码比较多, 因此,我们可以使用mvc的注解驱动代替上述配置。

在 SpringMVC 的各个组件中, 处理器映射器、 处理器适配器、 视图解析器称为 SpringMVC 的三大组件。

使用<mvc:annotation-driven>自动加载 RequestMappingHandlerMapping(处理映射器)和 RequestMappingHandlerAdapter(处理适配器)可用在Spring-xml.xml配置文件中使用 <mvc:annotation-driven>替代注解处理器和适配器的配置。

同时使用<mvc:annotation-driven>默认底层就会集成jackson进行对象或集合的json格式字符串的转换。

<!--在spring-mvc.xml中配置mvc的注解驱动--><mvc:annotation-driven/>

4)知识要点

SpringMVC的数据响应方式

1) 页面跳转

  • 直接返回字符串

  • 通过ModelAndView对象返回

2) 回写数据

  • 直接返回字符串

  • 返回对象或集合

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Java SpringMVC数据响应实例分析的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:ASP.NET Core中间件如何使用下一篇:

12 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18