我的Spring控制器看起来像这样:
@Controller
@RequestMapping(value = "calc")
public class CalcController {
protected final Log logger = LogFactory.getLog(getClass());
@Autowired
private MyService myService;
@RequestMapping(method = RequestMethod.GET)
public String showCalcPage(
@ModelAttribute("myModel") MyModel myModel,
Model model, HttpServletRequest request) {
// assemble page
return "calc";
}
@RequestMapping(method = RequestMethod.POST)
public String showResultsPage(
@ModelAttribute("myModel") MyModel myModel,
BindingResult result, Model model,
final RedirectAttributes redirectAttributes,
HttpServletRequest request) {
myService.evaluate(myModel);
redirectAttributes.addFlashAttribute("myModel", myModel);
model.addAttribute("myModel", myModel);
return "redirect:calc/results";
}
@RequestMapping(value = "/results")
public String showResultsPage(ModelMap model,
@ModelAttribute("myModel") final MyModel myModel,
final BindingResult bindingResult) {
// assemble page
return "results";
}
}
我有一个网址calc
与GET和POST的映射,另一个用于calc/results
。
这对我来说非常合适,但每当我尝试直接访问calc/results
时,该页面仍会呈现。
因此我对其RequestMethod
进行了POST限制,如:
@RequestMapping(value = "/results", method = RequestMethod.POST)
public String showResultsPage(ModelMap model,
@ModelAttribute("myModel") final MyModel myModel,
final BindingResult bindingResult) {
// assemble page
return "results";
}
这消除了通过抛出405
来直接查看映射,但是当我从calc
提交表单时,错误仍然存在。
如何合并我拥有的这两种情况?
我实际上只想要两个像下面这样的控制器来实现POST和页面限制,但它不适用于我(我将其诊断为jsp
的不同映射。
@Controller
@RequestMapping(value = "calc")
public class CalcController {
protected final Log logger = LogFactory.getLog(getClass());
@Autowired
private MyService myService;
@RequestMapping(method = RequestMethod.GET)
public String showCalcPage(
@ModelAttribute("myModel") MyModel myModel,
Model model, HttpServletRequest request) {
// assemble page
return "calc";
}
@RequestMapping(value = "/results", method = RequestMethod.POST)
public String showResultsPage(
@ModelAttribute("myModel") MyModel myModel,
BindingResult result, Model model,
final RedirectAttributes redirectAttributes,
HttpServletRequest request) {
// assemble page
myService.evaluate(myModel);
model.addAttribute("myModel", myModel);
return "redirect:results";
}
}
答案 0 :(得分:0)
我最终实现了POST
限制和成功查看calc/results
页面(但没有redirect
,因为它根据我的Tomcat服务器导致“重定向循环”。
这是最终的控制器:
@Controller
public class CalcController {
protected final Log logger = LogFactory.getLog(getClass());
@Autowired
private MyService myService;
@RequestMapping(value = "calc", method = RequestMethod.GET)
public String showCalcPage(
@ModelAttribute("myModel") MyModel myModel,
Model model, HttpServletRequest request) {
// assemble page
return "calc";
}
@RequestMapping(value = "calc/results")
public String showResultsPage(
@ModelAttribute("myModel") MyModel myModel,
ModelMap model, final BindingResult bindingResult,
HttpServletRequest request) {
// assemble page
// apply BindingResult validation in da fyoochoor
myService.evaluate(myModel);
model.addAttribute("myModel", myModel);
return "results";
}
}
现在直接访问calc/results
会抛出HTTP 500
,这样可以保证安全。只需确保在web.xml
中声明此异常的页面,以便在部署时获得美学效果。