情况是这样。 我要为一个POST请求生成一个JSON,于是就有了下面这个Controller和这个JSP
@RequiresRoles("ROLE_USER")
@RequestMapping(value = "/ajax/cost-pie", method = RequestMethod.POST)
public String costPie(
ModelMap modelMap
)
{
Map<String, Double> map = myService.getData(); // 经调试,读取数据业务逻辑完全正常
modelMap.put("map", map);
return "statistics/ajax/cost-pie";
}
有了数据以后我借助WEB-INF/jsp/statistics/ajax/cost-pie.jsp来帮我生成JSON
没有借助FastJSON 或 Jackson
<%@ page language="java" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.*" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%--
@see http://www.highcharts.com/demo/pie-basic
--%>
{
"chart": {
"plotBackgroundColor": null,
"plotBorderWidth": null,
"plotShadow": true
},
"title": {
"text": ""
},
"tooltip": {
"enabled": true,
"pointFormat": "{series.name}: <b>{point.percentage:.1f}%</b>"
},
"plotOptions": {
"pie": {
"allowPointSelect": false,
"cursor": "pointer",
"dataLabels": {
"enabled": true,
"color": "#000000",
"connectorColor": "#000000",
"format": "<b>{point.name}</b>: {point.percentage:.1f} %"
}
}
},
"series": [{
"type": "pie",
"name": "share",
"data": [
<%--
["Firefox", 45.0],
["IE", 26.8],
["Chrome", 12.8],
["Safari", 8.5],
["Opera", 6.2],
--%>
<c:if test="${sum != 0}">
["燃油", ${map['GAS'] / sum * 100}],
["停车", ${map['PARKING'] / sum * 100}],
["洗车", ${map['WASHING'] / sum * 100}],
["路桥", ${map['TOLL'] / sum * 100}],
["罚款", ${map['FINE'] / sum * 100}]
</c:if>
<c:if test="${sum == 0}">
["没有消费记录", 100]
</c:if>
]
}]
}
以下是我spring-mvc.xml配置
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" />
<property name="favorParameter" value="true" />
<property name="parameterName" value="format" />
<property name="ignoreAcceptHeader" value="true" /> <!-- 忽略HttpHeader:Accept -->
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
html=text/html
</value>
</property>
<property name="defaultContentType" value="text/html" />
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="0" />
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="defaultViews">
<list>
<bean class="com.alibaba.fastjson.support.spring.FastJsonJsonView" />
</list>
</property>
</bean>
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1" />
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="contentType" value="text/html" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
奇怪的现象是,即使我配置了ignoreAcceptHeader=true,当请求http://localhost:8080/statistics/ajax/cost-pie
(FireBug调试可知 Header.Accept=application/json)到达后最后mvc框架最终用FastJsonJsonView帮我展示的数据,
而不是我期望的JstlView!!!
我是用的软件版本如下
spring/spring-mvc 3.2.4.RELEASE
fastjson 1.1.36
有哪位大神遇到过和我一样的问题吗? 请多指教。