简体   繁体   中英

Spring 3 MVC Default view for spring controller

I have a simple question, but I cannot find a solution anywhere.

For a project I have a controller which pulls lists according to some business rules. I have multiple RequestMappings and multiple methods but they should all return the same view. Is there a way to specify a default view for a controller? Currently my code looks like this:

@Controller
public class OverviewController {

    @RequestMapping("/{name}-games")
    public String getOverview(@PathVariable("name") String name) {
        // Code
        return "view";
    }

    @RequestMapping("/{category}")
    public String getCategory(@PathVariable("category") String category) {
        // Code
        return "view";
    }

    @RequestMapping("/special-{promo}-games")
    public String getSpecialPromo(@PathVariable("promo") String namepromo) {
        // Code
        return "view";
    }

}

I can replace the return "view"; with something like return view(); everywhere but I am hoping to find something more like an annotation:

@DefaultView()
public String view() {
    return "view";
}

I am unable to find any such thing in the spring documentation. Is this possible or is the whole setup wrong to start with?

According to the Sping Reference ,

The RequestToViewNameTranslator interface determines a logical View name when no such logical view name is explicitly supplied.

(That is when your controller method returns Model , Map or void .)

You could implement this interface, but I think in your example the best thing you can do is defining a constant as CodeChimp has suggested.

Can you not go with the approach of having multiple view resolvers using order??

Have the beanNameViewResolver with order 0,which tries to map the matching bean to the modelAndView(common for controller in your case) that you return.

In case it doesnt match then you can default it onto a internalResourceViewResolver(order=1) to provide default behaviour .

Your default view page required some attributes that should be send via Model Attributes.Assuming,these required model attributes are same in all your methods of different Business logic.You can add them in Flash attributes and redirect to default Method.

Suppose X1,X2 attributes are same in all Handler method independent of Logic

@Controller
@SessionAttribute({"X1","X2"})
public class OverviewController {

    @RequestMapping("/{name}-games")
    public String getOverview(@PathVariable("name") String name,final RedirectAttributes redirectAttributes) {
        // Code

        //add attributes requires for view in Flash attribute
         redirectAttributes.addFlashAttribute("X1", "X1");
         redirectAttributes.addFlashAttribute("X2", "X2");
        return "redirect:defaultview";
    }

    @RequestMapping("/{category}")
    public String getCategory(@PathVariable("category") String category,final RedirectAttributes redirectAttributes) {
        // Code
        //add attributes requires for view in Flash attribute
         redirectAttributes.addFlashAttribute("X1", "X1");
         redirectAttributes.addFlashAttribute("X2", "X2");
        return "redirect:defaultview";
    }

    @RequestMapping("/special-{promo}-games")
    public String getSpecialPromo(@PathVariable("promo") String namepromo,final RedirectAttributes redirectAttributes) {
        // Code
        //add attributes requires for view in Flash attribute
         redirectAttributes.addFlashAttribute("X1", "X1");
         redirectAttributes.addFlashAttribute("X2", "X2");
        return "redirect:defaultview";
    }

    @RequestMapping("defaultview")
    public String default(Model model) {

        //here you can access all attributes in Flash Map via Model Attribute
        // Code
        model.addAttribute("X1","X1");
        model.addAttribute("X1","X1");
        return "view";
    }

}

Caution:you have to add requires attributes in Session also because if you refresh page ,this avoids well known Exception.

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM