我正在开发一个 spring mvc web 应用程序,其中我使用 Google Visualization API 来生成一些图表。我有一个模型类,其中包含 2 个数组列表,它们代表我发送给绘制图表的函数的数据(这是我想要转换为 JSON 的数据)。
模型类:
@Component
public class JsonResponse {
private List<Integer> percentages = new ArrayList<Integer>();
private List<String> topics = new ArrayList<String>();
public JsonResponse(){
}
public List<Integer> getPercentages() {
return percentages;
}
public void setPercentages(List<Integer> percentages) {
this.percentages = percentages;
}
public List<String> getTopics() {
return topics;
}
public void setTopics(List<String> topics) {
this.topics = topics;
}
}
然后我ve got a
@Component` 注释类,其中包含一个返回模型对象(我上面写的类)的方法,并填充了 2 个 arraylists 属性。
@Component
public class ChartUtils {
@Autowired
public JsonResponse response;
public JsonResponse listPieChartData( ModelAndView model ,int waveId ){
//arraylists for chart generation
List<Integer> percentages = new ArrayList<Integer>();
List<String> topics = new ArrayList<String>();
{... code for accessing the DB and processing some data and then populating the 2
arraylists ... }
response.setTopics(topics);
response.setPercentages(percentages);
return response;}
}
因此,Controller 类,它具有我正在调用以收集图表生成数据的操作的映射,并且我正在listPieChartData
从上面的类中调用方法,并且我也在其中使用@ResponseBody
注释是这个:
@Controller
public class ChartController {
@Autowired
public ChartUtils utils;
@Autowired
public JsonResponse response;
@RequestMapping(value = "/drawPieChart", method = RequestMethod.GET )
@ResponseBody
public JsonResponse drawPieChart( ModelAndView model,
@RequestParam(value = "id", defaultValue = "-1") int waveId ) {
return utils.listPieChartData(model,waveId ); }
绘制图表的 JavaScript 函数:
function drawColumnChart(percentages, topics , div,width,height) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Wave');
for (var i=0; i < topics.length; i++){
data.addColumn( 'number', topics[i] );
}
data.addRow( percentages );
var wave=percentages[0];
var options = {
'title':'Generated Chart For '+wave,
'backgroundColor': { fill: "none" },
'is3D': true,
'width':width,
'height':height,
};
var chart = new google.visualization.ColumnChart(document.getElementById(div));
chart.draw(data, options);
}
以及对控制器的映射方法(用于收集数据)的 AJAX 调用,最终调用上述 JS 函数来获取图表(我也在发送控制器方法的请求参数 int id,我没有写那个)
$("#button").live("click", function(){
var arrayP, arrayT;
$.ajax({
url: "drawPieChart",
contentType: "application/json",
data: params,
success: function(data) {
$.each(data, function(messageIndex, message) {
if (messageIndex === 0) {
arrayP = message;
} else {
arrayT = message;
}
});
drawPieChart(arrayP, arrayT,'chart_div',600,400);
}
});
});
我知道这是很多代码 :) 但它是非常简单的代码,为了更好地理解流程,这是它的工作原理:
从我正在使用 AJAX 调用的按钮输入,映射方法到 drawPieChart 操作(在ChartController
类中),此方法通过调用 listPieChart 方法(来自ChartUtils
类)发送响应,该方法返回一个JsonResponse
对象,(包含 2 个数组列表)。这JsonResponse
应该转换为 JSON,因为在 AJAX 请求中,我告诉请求需要 JSON 输入(通过 contentType:“application/json”),它应该得到它,因为我@ResponseBody
在映射的控制器方法中使用这个请求。
我收到了这样的回复:
此请求标识的资源只能生成具有根据请求“接受”标头()不可接受的特征的响应。
(HTTP 状态 406)
请纠正我哪里错了,我就是不能让它工作,我不知道为什么......
我的servlet-context.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up
static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.bla.bla" />
<beans:import resource="classpath:springJDBC.xml" />
</beans:beans>