Image 从控制器生成图表
我使用Spring3.0,在jsp中,我尝试显示数据和图表Image 从控制器生成图表,image,spring,servlets,spring-mvc,Image,Spring,Servlets,Spring Mvc,我使用Spring3.0,在jsp中,我尝试显示数据和图表 @Controller @RequestMapping("/user.htm") public class UserController { @Autowired private IUserService userService; @RequestMapping(method = RequestMethod.GET) public ModelAndView user(HttpServletRequest request, H
@Controller
@RequestMapping("/user.htm")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(method = RequestMethod.GET)
public ModelAndView user(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView modelAndView = new ModelAndView("user");
modelAndView.addObject("statUser", userService.getStatUser());
return modelAndView;
}
public void generateChart(HttpServletRequest request,
HttpServletResponse response){
try {
AxisChart axisChart = userService.generateChart();
ServletEncoderHelper.encodeJPEG13( axisChart, 1.0f, response );
} catch (ChartDataException ex) {
} catch (PropertyException ex) {
} catch (IOException ex) {
}
}
}
在jsp中,我尝试使用
<img src="generateChart"/>
我可以看到信息。。。因此,控制器的get工作正常,但图像永远不会显示
所以我不知道我是否可以使用相同的控制器,或者只需要创建一个新的控制器来创建图像
有什么想法吗?我用Jfreechart做了一些类似的事情,我最终创建了另一个图表控制器,它有一个类似于
@RequestMapping(“/chart.jpg”)
的映射,然后通过
链接到它。在控制器中,您必须设置response.setHeader(“内容类型”、“图像/jpg”)代码>
编辑:
这里是我的一个例子(它在groovy中,是一个png,但应该会有所帮助)
我对几个报表处理程序使用单一接口。
因此,ModelAndView始终是预期的。
我认为这是一般情况,当您需要处理不同的报告并以不同的格式返回数据时
所以,我通过扩展AbstractView解决了这个问题。
注意:代码示例从磁盘加载文件。
您必须使用自己的图像生成器和模型中表示的数据创建字节数组输出流
创建ModelAndView的示例:
ModelAndView result = new ModelAndView();
result.setView(new ImageView());
result.addObject("chart_data_param1", "1");
result.addObject("chart_data_param2", "2");
result.addObject("chart_data_param3", "3");
视图类的实现:
public class ImageView extends AbstractView {
public ImageView() {
setContentType("image/png");
}
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
ByteArrayOutputStream baos = createTemporaryOutputStream();
byte[] img = FileUtils.readFileToByteArray(new File("<some-file-path>"));
baos.write(img, 0, img.length);
baos.close();
writeToResponse(response, baos);
}
}
公共类ImageView扩展了AbstractView{
公共图像视图(){
setContentType(“图像/png”);
}
@凌驾
受保护的void RenderMergeDoutpModel(映射模型、HttpServletRequest请求、HttpServletResponse响应)引发异常{
ByteArrayOutputStream baos=createTemporaryOutputStream();
字节[]img=FileUtils.readFileToByteArray(新文件(“”);
写入(img,0,img.长度);
baos.close();
writeToResponse(响应,BAS);
}
}
遇到相同的问题此控制器从未调用@controller@RequestMapping(“/userStat.jpg”)公共类ImageController{@Autowired private IUserService userService;public void generateChart(HttpServletRequest请求,HttpServletResponse响应){try{AxisChart AxisChart=userService.generateChart();ServletEncoderHelper.encodeJPEG13(AxisChart,1.0f,响应);}catch(ChartDataException ex){}catch(PropertyException ex){}catch(IOException ex){}我将尝试使用jfreechart,但这不适用于JChart。我是否需要在配置文件中设置一些内容?您指的是哪个配置文件?
public class ImageView extends AbstractView {
public ImageView() {
setContentType("image/png");
}
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
ByteArrayOutputStream baos = createTemporaryOutputStream();
byte[] img = FileUtils.readFileToByteArray(new File("<some-file-path>"));
baos.write(img, 0, img.length);
baos.close();
writeToResponse(response, baos);
}
}