문제

A Spring controller has the following method :

@RequestMapping(value = "reportsOverall.html")
    public/* @ResponseBody */
    ModelAndView reportsOverall(SearchParameters searchParameters) {
        searchParameters = new SearchParameters(840, "SCH_01", "PRM_07");
        System.out.println("Inside controller");
        String result = overallDetailsService
                .getOverallDetails(searchParameters);

        System.out.println("result is " + result);

        Map<String, String> model = new HashMap<String, String>();
        model.put("data", result);

        ModelAndView modelAndView = new ModelAndView("reports/reportsOverall",
                model);

        return modelAndView;

    }

I get the expected output on the server-side console :

result is { "_id" : { "SpId" : 840 , "Scheduler_id" : "SCH_01"} , "positive_count" : 64 , "neutral_count" : 78 , "negative_count" : 2}

But in the reportsOverall.jsp, when I retrieve the result object as :

function drawSentimentPieChart() {

        var data = '<c:out value="${data}"/>';
        alert(data);
            var dataJson = JSON.parse(data);

......
}

in the alert I get :

{ &#034;_id&#034; : { &#034;SpId&#034; : 840 , &#034;Scheduler_id&#034; : &#034;SCH_01&#034;} , &#034;positive_count&#034; : 64 , &#034;neutral_count&#034; : 78 , &#034;negative_count&#034; : 2}

The space and "" quotes are encoded here, thereby, causing the parse to fail.

The spring-servlet.xml is :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <mvc:annotation-driven />
    <context:component-scan base-package="com.lnt" />

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"></bean>

    <!-- <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <array>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
                </bean>
            </array>
        </property>
    </bean> -->

    <bean id="jacksonMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

        <property name="basenames"
            value="classpath:userProfile,classpath:default,classpath:messages,classpath:customerAdmin,classpath:reports,classpath:superAdmin" />

        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <bean id="dateFormat" class="java.text.SimpleDateFormat">
        <constructor-arg value="yyyy-MM-dd hh:mm:ss" />
    </bean>


</beans>

I tried several things in the Spring controller :

Using @ResponseBody; in this case, I got a dump on the page like this but the page wasn't rendered properly :

{"view":null,"model":{"data":"{ \"_id\" : { \"SpId\" : 840 , \"Scheduler_id\" : \"SCH_01\"} , \"positive_count\" : 64 , \"neutral_count\" : 78 , \"negative_count\" : 2}"},"empty":false,"viewName":"reports/reportsOverall","reference":true,"modelMap":{"data":"{ \"_id\" : { \"SpId\" : 840 , \"Scheduler_id\" : \"SCH_01\"} , \"positive_count\" : 64 , \"neutral_count\" : 78 , \"negative_count\" : 2}"}}

Added encoding filter to the web.xml :

<filter>
        <filter-name>encoding-filter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>

    </filter>

    <filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Attempted using MappingJacksonJsonView in vain !

What is that I'm overlooking?

도움이 되었습니까?

해결책

I had the same problem, but this additional attribute saved me:

<c:out value="${data}" escapeXml="false"/>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top