用户输入两个日期 它需要点击按钮......
如果日期有效,则调用相同的jsp页面并在请求中设置一些值...在此jsp中,如果setSeachDone为true,则生成图表...
为图像调用另一个服务控制器...但是已在请求中设置的值为空
@Controller
@RequestMapping("/statError")
public class StatisticError {
@Autowired
private IUserService userService;
@InitBinder("statisticForm")
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new StatisticErrorFormValidator());
}
@RequestMapping(method = RequestMethod.GET)
public String statistic(Model model) {
StatisticErrorForm statisticForm = new StatisticErrorForm();
model.addAttribute("statisticForm", statisticForm);
return "statisticError";
}
@RequestMapping(method = RequestMethod.POST)
public String statistiqueResult(@Valid @ModelAttribute StatisticErrorForm statisticForm, BindingResult result, ModelMap model, HttpServletRequest request,
HttpServletResponse response) {
if (!result.hasFieldErrors() && !result.hasErrors()) {
request.setAttribute("startDate", statisticForm.getStartDate());
request.setAttribute("endDate", statisticForm.getEndDate());
statisticForm.setSearchDone(true);
}
model.addAttribute(statisticForm);
return "statisticError";
}
}
servlet控制器
@Controller
@RequestMapping("/statError.jpg")
public class ImageErrorController {
@Autowired
private IUserService userService;
@RequestMapping(method = RequestMethod.GET)
public void generateChart(HttpServletRequest request,
HttpServletResponse response) {
if (request.getAttribute("startDate") != null && request.getAttribute("endDate") != null) {
response.setContentType("image/jpg");
AxisChart axisChart = userService.generateChart();
ServletEncoderHelper.encodeJPEG(axisChart, 1.0f, response);
}
}
有没有办法将用户输入的值发送到imageErrorController? 将模型添加到generateChart?
答案 0 :(得分:1)
您需要在视图中将它们作为图片网址的参数传递,如下所示:
<img src = "<c:url value = "/statError.jpg">
<c:param name = "startDate" value = "${startDate}" />
<c:param name = "endDate" value = "${endDate}" />
</c:url>" />