[英]How to pass LIST from one controller to another controller in spring mvc
[英]redirecting one to another Controller using spring mvc 3
下面是我的控制器
@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
}
由於控制器ABC是@ResponceBody類型,它將始終作為字符串返回,但我希望在其他情況下,它應該被重定向到XYZ控制器,並從中返回一個我可以顯示的jsp頁面。 我嘗試使用return“forward:checkPage”; 還有返回“redirect:checkPage”; 但不起作用。 任何幫助。
謝謝。
我想如果你想自己渲染響應或者根據某些條件在一個控制器方法中重定向,你必須刪除@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";
}
}
- 編輯 -
如果你想通過ajax訪問控制器,你最好在你的請求中包含datatype參數,以表明你只是期望一個文本響應:
$.get("/AAA-Web/abc",jQuery.param({})
,function(data){
alert(data);
}, "text");
return new ModelAndView("redirect:/admin/index");
上面的代碼適合我。 我正在從當前控制器重定向到AdminController中的索引。
edirected到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
}
采用
@RequestMapping(value ="/checkPage",method = RequestMethod.GET)
public ModelAndView XYZ(HttpServletRequest req)
{
ModelAndView m=new ModelAndView();
m.setViewName("check");
return m;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.