【问题标题】:Not able to redirect to another method of controller in Spring MVC using path variable无法使用路径变量重定向到 Spring MVC 中的另一种控制器方法
【发布时间】:2015-09-07 14:34:54
【问题描述】:

我有一个控制器类,其中有两种方法。我正在从 jsp 提交一个表单,然后控制来自第一种方法,从第一种方法我使用路径变量重定向到第二种方法,但它不起作用。

下面是我的代码:

@Controller
public class UserController {

 @RequestMapping(value = "saveUserDetails", method = RequestMethod.POST)
public String saveUser(ModelMap map, HttpSession session,HttpServletRequest request) {
    String strId= request.getParameter("userId");;
    return "redirect:userView/" + strId;
}


 @RequestMapping(value="userView/{id}", method = RequestMethod.GET)
public String displayUser(@PathVariable String id, Model model) {
    System.out.println("in method2");
    return "showUser";
}

}

当用户从 jsp 提交表单然后控制进入 saveUser() 方法时,我正在重定向到另一个方法 displayUser(),但由于某种原因它不起作用。 有人可以帮我吗?

【问题讨论】:

  • 描述不工作
  • @SotiriosDelimanolis : 我已经放了调试点 displayUser() 方法,控件不在这个方法中。
  • 发生了什么?您的浏览器试图将您重定向到哪里?什么网址? Spring 日志显示什么?
  • @SotiriosDelimanolis - 它没有给出任何错误或日志,但是浏览器中的 url 是 localhost/UserApp/userView/9625 并且页面没有完全呈现。
  • 再次,page is not fully rendering 是什么意思。渲染“有点”吗?一点也不?启用调试日志并检查 Spring 在做什么。请明确并使用这些相关详细信息更新您的问题。

标签: java spring-mvc


【解决方案1】:

您在 userView 之前错过了 /

return "redirect:/userView/" + strId;

查看弹簧文档here

【讨论】:

  • 这里RedirectAttributesjavadoc有什么相关性?为什么他们需要/
猜你喜欢
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多