Unable to redirect from one controller to another

2019-06-06 07:50发布

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");
    }
}

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.

What changes are required to have request url as http://localhost:8080/Online_Fund_Trading/Customer/Searchform.spring.

1条回答
老娘就宠你
2楼-- · 2019-06-06 08:21

A simple slash / is required

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.

查看更多
登录 后发表回答