[英]Redirect to a POST request from one controller to another controller Spring Boot
[英]Unable to redirect from one controller to another controller-Spring MVC
我是Spring MVC的新手,面臨一些錯誤。
我有兩個控制器,如下所示
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
但是在瀏覽器URL中,我可以看到該請求URL為http://localhost:8080/Online_Fund_Trading/log/Customer/Searchform.spring
。 隨着在Customer/Searchform.spring
之前添加log
,我得到404-The requested resource is not available
錯誤。
http://localhost:8080/Online_Fund_Trading/Customer/Searchform.spring
請求網址為http://localhost:8080/Online_Fund_Trading/Customer/Searchform.spring
需要進行哪些更改。
需要一個簡單的斜杠/
ModelAndView view=new ModelAndView("redirect:/Customer/Searchform.spring");
否則,該路徑將被視為相對於您當前正在處理的請求的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.