Java 将带有pathvariable的字符串返回到另一个控制器
这里我的主要问题是从一个控制器向另一个控制器返回一个带有pathvariable值的字符串 请看这里:Java 将带有pathvariable的字符串返回到另一个控制器,java,spring,spring-mvc,Java,Spring,Spring Mvc,这里我的主要问题是从一个控制器向另一个控制器返回一个带有pathvariable值的字符串 请看这里: @RequestMapping(value = "/profile/{location}") public ModelAndView profile(@PathVariable("location") String location) throws Exception { return new ModelAndView("profile", "*", *); } @RequestMa
@RequestMapping(value = "/profile/{location}")
public ModelAndView profile(@PathVariable("location") String location) throws Exception {
return new ModelAndView("profile", "*", *);
}
@RequestMapping(value = "/records", method = "RequestMethod.POST")
public String inRecords(@Valid User user, BindingResult result) {
if(result.hasErrors()) {
return "profile/system";
}
else {
.....
return "somewhere";
}
}
我这里的问题是返回“profile/system”转到WEB-INF/views/profile/system.jsp。@PathVariable或return语句本身有什么问题吗
有什么建议吗?你为什么不试试这样的东西呢
@RequestMapping(value = "/records", method = "RequestMethod.POST")
public void inRecords(@Valid User user, HttpServletResponse response) {
if(result.hasErrors()) {
response.sendRedirect("/YourApp/profile/system")
}
我认为ModelAndView正在获取返回的字符串,并尝试运行ViewResolver以获取jso,避免调用或将请求直接重定向到所需的端点
或者,如果要保留modelAndView,请使用此
return new ModelAndView("redirect:/profile/system");
查看重定向属性。刚刚尝试过,但错误仍然不会显示。错误不会自行显示。这取决于你是如何使用它们的。我有一个错误提示,当有错误时会显示出来,因为它重定向到了配置文件/系统页面,所以应该有。但是结果显示没有错误?我说得通吗?对不起,谢谢。我不知道您如何使用
BindingResult
或RedirectAttributes
,所以我帮不了您。谢谢:)!我不久前就发现了这一点,尽管这是一个糟糕的做法,但我只是在result.hasrerrors()下复制了ModelAndView profile的内容,并使用returnnewmodelandview(“profile”、“*”、*)。