SpringMVC的返回字符串(JSON)乱码问题,以及Date类型字段转换为json时的问题

原创
2014/04/09 10:44
阅读数 820

web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

     <display-name>extjs4</display-name>

    

     <context-param>

         <param-name>contextConfigLocation</param-name>

         <param-value>classpath:beans.xml</param-value>

     </context-param>

    

     <listener>

         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

     </listener>

    

     <filter>

         <filter-name>CharacterEncodingFilter</filter-name>

             <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

         <init-param>

         <param-name>encoding</param-name>

             <param-value>utf-8</param-value>

         </init-param>

     </filter>

    

     <filter-mapping>

         <filter-name>CharacterEncodingFilter</filter-name>

         <url-pattern>/*</url-pattern>

     </filter-mapping>

    

     <servlet>

         <servlet-name>springmvc</servlet-name>

         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

     </servlet>

    

     <servlet-mapping>

         <servlet-name>springmvc</servlet-name>

         <url-pattern>*.do</url-pattern>

     </servlet-mapping>

</web-app>

springmvc-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:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx.xsd ">


     <context:annotation-config />

    

     <context:component-scan base-package="com.iduanyingjie.extjs4.controller" />

    

     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    

     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

         <property name="messageConverters">

             <list>

                 <ref bean="stringHttpMessageConverter" />

                 <ref bean="mappingJackson2HttpMessageConverter" />

             </list>

         </property>

     </bean>

    

     <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">

         <property name="supportedMediaTypes">

             <list>

                 <value>text/plain;charset=utf-8</value>

             </list>

         </property>

     </bean>

    

     <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

         <property name="supportedMediaTypes">

             <list>

                 <value>application/json</value>

             </list>

         </property>

     </bean>

    

     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

         <property name="prefix" value="/jsp/" />

         <property name="suffix" value=".jsp" />

     </bean>


</beans>


UserController.java:

import java.util.Date;

import java.util.List;


import javax.annotation.Resource;


import net.sf.json.JSONArray;

import net.sf.json.JsonConfig;

import net.sf.json.processors.JsonDateValueProcessor;


import org.springframework.stereotype.Controller;

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 com.iduanyingjie.extjs4.domain.User;

import com.iduanyingjie.extjs4.service.IUserService;


@Controller

@RequestMapping(value = "/UserController/")

public class UserController {


     @Resource(name = "userService")

     private IUserService userService;


     @RequestMapping(value = "getUserList.do", produces = "text/plain;charset=UTF-8", method = {RequestMethod.GET, RequestMethod.POST })

     @ResponseBody

     public String getUserList() {

         System.out.println("UserController/getUserList");

         List<User> userList = userService.findAll();

    

         JsonConfig jsonConfig = new JsonConfig();

         jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());

    

         return "{\"total\":"

                      + userList.size()

                      + ", \"rows\":"

                      + JSONArray.fromObject(userList, jsonConfig).toString() + "}";

     }


     @RequestMapping(value = "save.do", method = { RequestMethod.GET, RequestMethod.POST })

     @ResponseBody

     public void save(@RequestBody User user) {

         System.out.println(user);

     }


}

JsonDateValueProcessor.java

package net.sf.json.processors;


import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;


import net.sf.json.JsonConfig;


public class JsonDateValueProcessor implements JsonValueProcessor {


     private String datePattern = "yyyy-MM-dd";


     private SimpleDateFormat sdf = new SimpleDateFormat(datePattern, Locale.UK);


     public JsonDateValueProcessor() {

         super();

     }


     public JsonDateValueProcessor(String format) {

         super();

         this.datePattern = format;

     }

    

     @Override

     public Object processArrayValue(Object value, JsonConfig jsonConfig) {

         return process(value);

     }

    

     @Override

     public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {

         return process(value);

     }

    

     private Object process(Object value) {

         try {

             if (value instanceof Date) {

                 return sdf.format((Date) value);

             }

             return value == null ? "" : value.toString();

         } catch (Exception e) {

             return "";

         }

    

     }

    

     public String getDatePattern() {

         return datePattern;

     }

    

     public void setDatePattern(String pDatePattern) {

         datePattern = pDatePattern;

     }


}


展开阅读全文
打赏
0
4 收藏
分享
加载中
更多评论
打赏
0 评论
4 收藏
0
分享
返回顶部
顶部