从控制器生成图表
我使用spring 3.0,在jsp中,我尝试显示数据和图表...
@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"/>
我可以看到信息...所以控制器的获取工作正常,但是图像从未显示,
所以我不知道我是否可以使用相同的控制器,或者需要创建一个新控制器仅用于创建图像......
有什么想法吗?
i use spring 3.0, in a jsp, i try to display data and a chart...
@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) {
}
}
}
in the jsp i try to display the chart with
<img src="generateChart"/>
i can see the information... so the get of the controller work fine, but the image is never displayed
so i don't know if i can use the same controller or need to create a new one only for creation of the image...
any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用 Jfreechart 做了一些类似的事情,我最终创建了另一个图表控制器,其映射类似于
@RequestMapping("/chart.jpg")
,然后通过。在控制器中,您必须设置
response.setHeader("Content-Type", "image/jpg");
。编辑:
这是我的一个例子(它是groovy格式的,是png的,但应该有帮助)
I've done something similer with Jfreechart, I ended up creating another chart controller with a mapping like
@RequestMapping("/chart.jpg")
and then link to it via<img src="cart.jpg"/>
. In the controller then you have to setresponse.setHeader("Content-Type", "image/jpg");
.Edit:
Here a example of mine (it's in groovy and it's a png, but it should help)
我正在为多个报告处理程序使用单一界面。
ModelAndView 始终是预期的结果。
我认为这是一般情况,当您需要处理不同的报告并以不同格式返回数据时。
因此,我通过扩展 AbstractView 解决了问题。
注意:代码示例从磁盘加载文件。
您必须使用自己的图像生成器和模型中表示的数据创建字节数组输出流。
创建 ModelAndView 的示例:
View 类的实现:
I am using the single interface for several report handlers.
ModelAndView is always expected as a result.
I think this is generic situation, when you need to process different reports and return data in different formats.
So, I resolved problem by extending AbstractView.
Note: Code example loads file from the disk.
You have to create byte array output stream using your own image generator and data represented in the model.
Example creating ModelAndView:
Implementation of the View class: