基于上一篇文章的代码基础上
一、准备工作
1、 下载依赖库jar包
Jackson的jar all下载地址:http://wiki.fasterxml.com/JacksonDownload
jackson-core-asl.jar
jackson-mapper-as.jar
二、修改配置文件spmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc/spring-mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<!-- 自动扫描注解的Controller -->
<context:component-scan base-package="com.wy.controller.annotation" />
<!-- 处理在类级别上的@RequestMapping注解-->
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- 处理方法级别上的@RequestMapping注解-->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<util:list id="beanList">
<ref bean="mappingJacksonHttpMessageConverter"/>
</util:list>
</property>
</bean>
<!-- 视图解析器策略 和 视图解析器 -->
<!-- 对JSTL提供良好的支持 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 默认的viewClass,可以不用配置
<property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" />
-->
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 处理JSON数据转换的 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<!-- 为了处理返回的JSON数据的编码,默认是ISO-88859-1的,这里把它设置为UTF-8,解决有乱码的情况 -->
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!--
ResourceBundleViewResolver通过basename所指定的ResourceBundle解析视图名。
对每个待解析的视图,ResourceBundle里的[视图名].class所对应的值就是实现该视图的类。
同样,[视图名].url所对应的值是该视图所对应的URL。
可以指定一个parent view,其它的视图都可以从parent view扩展。
用这种方法,可以声明一个默认的视图。
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="welcome" />
</bean>
-->
</beans>
二、注解的Controller
package com.wy.controller.annotation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.wy.pojo.User;
/**
* SpringMVC 注解 内置支持
* @author Administrator
*
*/
@Controller
@RequestMapping("/entryOrJsonController")
public class EntryOrJsonController {
private Logger log = Logger.getLogger(EntryOrJsonController.class);
/**
* 输入为JSON格式的数据的方式
* 1、@RequestBody 将httpRequest的body绑定到方法参数上
* @param param
* @return
*/
@RequestMapping(value = "/annotationParam", method = RequestMethod.POST)
public ModelAndView annotationParam(@RequestBody User user){
ModelAndView mav = new ModelAndView();
log.info("============"+user);
mav.setViewName("welcome");
return mav;
}
/**
* 输出为JSON格式的数据的方式
* 1、@ResponseBody的作用是把返回值直接写到HTTP response body里
* @param session
* @return
* @throws ServletRequestBindingException
*/
@RequestMapping(value = "/commonReturnType" , method = RequestMethod.GET)
@ResponseBody
public ModelAndView commonReturnType(HttpSession session) throws ServletRequestBindingException{
ModelAndView mav = new ModelAndView();
session.setAttribute("userName", "使用ResponseBody输出!");
log.info("=================使用ResponseBody输出====================");
ModelMap modelMap = new ModelMap();
modelMap.put("mapKey", "mapValue");
modelMap.addAttribute("attributeKey", "attributeValue");
mav.addObject("model", modelMap);
mav.addObject("modelMap", modelMap);
mav.setViewName("welcome");
return mav;
}
/**
* 输出为JSON格式的数据的方式
* 2、ResponseEntity<?>
* @return
*/
@RequestMapping(value = "/annotationReturnType" , method = RequestMethod.GET)
public ResponseEntity<String> annotationReturnType(){
log.info("=================使用ResponseEntity<?>输出====================");
HttpHeaders httpHeaders = new HttpHeaders();
ResponseEntity<String> responseEntity = new ResponseEntity<String>("Hello WY!", httpHeaders, HttpStatus.CREATED);
return responseEntity;
}
/**
* 输入 和输出为JSON格式的数据的方式
* 3、HttpEntity<?> ResponseEntity<?>
* @param u
* @return
*/
@RequestMapping(value = "/annotationParamsReturn", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> annotationParamsReturn(HttpEntity<User> user){
String temp = user.getBody().getUsername();
ResponseEntity<String> responseResult = new ResponseEntity<String>(temp, HttpStatus.OK);
return responseResult;
}
/**
*
* @param request
* @param session
* @return
* @throws ServletRequestBindingException
*/
@RequestMapping(value = "/login" , method = RequestMethod.GET)
public ModelAndView login(HttpServletRequest request, HttpSession session) throws ServletRequestBindingException{
ModelAndView mav = new ModelAndView();
String userName = request.getParameter("userName");
String password = ServletRequestUtils.getStringParameter(request, "password");
log.info("============================\r\n接收到的参数: "+userName+" "+password);
session.setAttribute("userName", userName);
mav.setViewName("welcome");
return mav;
}
}
本文详细介绍了如何在SpringMVC中利用注解进行控制器的定义,并展示了如何通过请求体参数和响应体输出JSON数据,以及如何使用ResponseEntity进行更灵活的JSON数据返回方式。
136

被折叠的 条评论
为什么被折叠?



