开发者

Redirect in controllers spring3

In Spring 3 you map urls as simply as this:

@RequestMapping(value = "/index.ht开发者_JS百科ml", method = RequestMethod.GET)
public String index(Model model)  {
    return "index";
}

Is it possible to make this kind of method to kinda redirect to another url like:

@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String index(Model model)  {
    return "second.html";
}

@RequestMapping(value = "/second.html", method = RequestMethod.GET)
public String second(Model model)  {
//put some staff in model
    return "second";
}


You don't need to redirect - just call the method:

@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String index(Model model)  {
    return second(model);
}

@RequestMapping(value = "/second.html", method = RequestMethod.GET)
public String second(Model model)  {
    //put some staff in model
    return "second";
}

This is one of the nice things about the annotation style; you can just chain your methods together.

If you really want a redirect, then you can return that as a view:

@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public View index(Model model)  {
    return new RedirectView("second.html");
}

@RequestMapping(value = "/second.html", method = RequestMethod.GET)
public String second(Model model)  {
    //put some staff in model
    return "second";
}


Yes redirect will work. In index method, change last line to return "redirect:/second.html" ;

Edit context path and controller mapping are required. If DispatcherServlet is mapped to /ABC and request mapping for controller is /XYZ then you will have to write:
return "redirect:/ABC/XYZ/second.html";

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜