Java 使用 spring mvc 3 将一个控制器重定向到另一个控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18096053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
redirecting one to another Controller using spring mvc 3
提问by ASUR
below is my controller
下面是我的控制器
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String ABC(Registratio registration, ModelMap modelMap,
HttpServletRequest request,HttpServletResponse response){
if(somecondition=="false"){
return "notok"; // here iam returning only the string
}
else{
// here i want to redirect to another controller shown below
}
}
@RequestMapping(value="/checkPage",method = RequestMethod.GET,)
public String XYZ(ModelMap modelMap,
HttpServletRequest request,HttpServletResponse response){
return "check"; // this will return check.jsp page
}
since the Controller ABCis @ResponceBody type it will always return as string, but i want that in else contion it should be redirected to the XYZcontroller and from which it return a jsppage which i can show. i tried using return "forward:checkPage";also with return "redirect:checkPage";but doesn't work. any help.
由于控制器ABC是 @ResponceBody 类型,它总是以字符串形式返回,但我希望在其他情况下它应该被重定向到XYZ控制器,并从中返回一个我可以显示的jsp页面。我尝试使用return "forward:checkPage"; 还带有 return "redirect:checkPage"; 但不起作用。任何帮助。
Thanks.
谢谢。
采纳答案by Septem
I think you have to remove @ResponseBody if you want to either render response yourself or redirect in one controller method based on some condition, try this:
我认为如果您想自己呈现响应或根据某些条件在一个控制器方法中重定向,则必须删除 @ResponseBody,请尝试以下操作:
@RequestMapping(method = RequestMethod.GET)
//remove @ResponseBody
public String ABC(Registratio registration, ModelMap modelMap,
HttpServletRequest request,HttpServletResponse response){
if(somecondition=="false"){
// here i am returning only the string
// in this case, render response yourself and just return null
response.getWriter().write("notok");
return null;
}else{
// redirect
return "redirect:checkPage";
}
}
--edit--
- 编辑 -
if you want to access controller via ajax, you'd better include the datatype parameter on you request to indicate that you are simply expecting a text response:
如果你想通过 ajax 访问控制器,你最好在你的请求中包含 datatype 参数,以表明你只是期待一个文本响应:
$.get("/AAA-Web/abc",jQuery.param({})
,function(data){
alert(data);
}, "text");
回答by Mohsin Shaikh
edirected to the XYZ controller and from which it return a jsp page instead of using the following code i/e
重定向到 XYZ 控制器并从中返回一个 jsp 页面,而不是使用以下代码 i/e
@RequestMapping(value="/checkPage",method = RequestMethod.GET,)
public String XYZ(ModelMap modelMap,
HttpServletRequest request,HttpServletResponse response){
return "check"; // this will return check.jsp page
}
use
用
@RequestMapping(value ="/checkPage",method = RequestMethod.GET)
public ModelAndView XYZ(HttpServletRequest req)
{
ModelAndView m=new ModelAndView();
m.setViewName("check");
return m;
}
回答by Ifesinachi Bryan
return new ModelAndView("redirect:/admin/index");
The code above works for me. I was redirecting from the present controller to index in AdminController.
上面的代码对我有用。我正在从当前控制器重定向到 AdminController 中的索引。