无法从一个控制器重定向到另一控制器-Spring MVC [英] Unable to redirect from one controller to another controller-Spring MVC
问题描述
我是Spring MVC的新手,遇到了一些错误.
我有两个控制器,如下所示
1)LoginController.java
I am new to spring MVC and facing some error.
I have two controllers as below
1) LoginController.java
@Controller
@RequestMapping("/log")
public class LoginController {
@Autowired
private LoginService service;
@RequestMapping(value="login.spring",method=RequestMethod.GET)
public ModelAndView prepareLoginForm()
{
System.out.println("In get");
return new ModelAndView("Login", "login", new Login());
}
@RequestMapping(value="login.spring",method=RequestMethod.POST)
public ModelAndView processLogin(@ModelAttribute("login") Login login,BindingResult result)
{
int i=service.validateLogin(login);
if(i==0){
return new ModelAndView("redirect:login.spring");
}
ModelAndView view=new ModelAndView("redirect:Customer/Searchform.spring");
return view;
}
}
2)CustomerController.java
@Controller
@RequestMapping("/Customer")
public class CustomerController {
@Autowired
private CustomerService customerService;
@RequestMapping(value="Searchform.spring",method=RequestMethod.GET)
public ModelAndView prepareCustomer()
{
System.out.println("In customer controller");
CustomerSearchForm customerSearchForm=new CustomerSearchForm();
return new ModelAndView("CustomerSearch","customerSearchForm",customerSearchForm);
}
@RequestMapping(value="Search.spring",method=RequestMethod.POST)
public ModelAndView searchCustomer(@ModelAttribute("customer") CustomerSearchForm customerSearchForm,BindingResult result)
{
int i=customerService.serachCustomer(customerSearchForm);
if(i==1)
return new ModelAndView("Holdings");
return new ModelAndView("redirect:Customer");
}
}
因此,成功登录后,我尝试重定向到CustomerController
,但在
浏览器网址我可以看到请求网址是
http://localhost:8080/Online_Fund_Trading/log/Customer/Searchform.spring
.
在Customer/Searchform.spring
之前添加log
时,出现了404-The requested resource is not available
错误.
So after successful login I am trying to redirect to CustomerController
but in
browser url i can see that request url is
http://localhost:8080/Online_Fund_Trading/log/Customer/Searchform.spring
.
As log
gets added before Customer/Searchform.spring
I am getting 404-The requested resource is not available
error.
要使请求URL为http://localhost:8080/Online_Fund_Trading/Customer/Searchform.spring
,需要进行哪些更改.
What changes are required to have request url as http://localhost:8080/Online_Fund_Trading/Customer/Searchform.spring
.
推荐答案
需要一个简单的斜杠/
ModelAndView view=new ModelAndView("redirect:/Customer/Searchform.spring");
否则,该路径将被视为相对于您当前正在处理的请求的路径.
Otherwise the path will be considered relative to the path of the request you are currently handling.
这篇关于无法从一个控制器重定向到另一控制器-Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!