您好,欢迎访问一九零五行业门户网

Java SpringMVC数据响应实例分析

1)页面跳转  直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转。
返回带有前缀的字符串:
转发:   forward:/web-inf/views/index.jsp
重定向:   redirect:/index.jsp
通过modelandview对象返回
@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;}
在进行转发时,往往要向request域中存储数据,在jsp页面中显示,那么controller中怎样向request 域中存储数据呢?
① 通过springmvc框架注入的request对象setattribute()方法设置。
@requestmapping("/quick")public string quickmethod(httpservletrequest request){ request.setattribute("name","zhangsan"); return "index";}
② 通过modelandview的addobject()方法设置。
@requestmapping("/quick3")public modelandview quickmethod3(){ modelandview modelandview = new modelandview(); modelandview.setviewname("forward:/web-inf/views/index.jsp"); modelandview.addobject("name","lisi"); return modelandview;}
2)回写数据直接返回字符串:web基础阶段,客户端访问服务器端,如果想直接回写字符串作为响应体返回的话,只需要使用response.getwriter().print(“hello world”) 即可,那么在controller中想直接回写字符串该怎样呢?
① 通过springmvc框架注入的response对象,使用response.getwriter().print(“hello world”) 回写数据,此时不需要视图跳转,业务方法返回值为void。
@requestmapping("/quick4")public void quickmethod4(httpservletresponse response) throws ioexception { response.getwriter().print("hello world");}
② 将需要回写的字符串直接返回,但此时需要通过@responsebody注解告知springmvc框架,方法 返回的字符串不是跳转是直接在http响应体中返回。
@requestmapping("/quick5")@responsebodypublic string quickmethod5() throws ioexception { return "hello springmvc!!!"; }
开发中往往要将复杂的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")@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;}
返回对象或集合
通过springmvc帮助我们对对象或集合进行json字符串的转换并回写,为处理器适配器配置消息转换参数, 指定使用jackson进行对象或集合的转换,因此需要在spring-mvc.xml中进行如下配置:
<bean class="org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter"> <property name="messageconverters"> <list> <bean class="org.springframework.http.converter.json.mappingjackson2httpmessageconverter"></bean> </list> </property></bean>
直接在方法中返回对象或集合
@requestmapping("/quick8")@responsebodypublic user quickmethod8() throws ioexception { user user = new user(); user.setusername("zhangsan"); user.setage(18); return user;}
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) 回写数据          
直接返回字符串          
返回对象或集合
以上就是java springmvc数据响应实例分析的详细内容。
其它类似信息

推荐信息