简体   繁体   中英

Intercepting session creation spring mvc

I'm trying to have some object in my view but I don't want to repeat/include code in each method in my Controller so instead I thought to put this object in a session.

This way it can be available in view and I don't repeat code.

So my question is how to intercept form creation and set some object to every session created?

When you want to make some data/object available to all of your controller methods, better put that in Model at controller's method level (using @ModelAttribute). Example below:

@Controller
public class MyController{

    @ModelAttribute
    public void getEmpRoles(Model model) {
        List<String> roles = myservice.loadEmpRoles();
        model.addAttribute(roles);
    }

    @RequestMapping(...)
    public String m1(Model model){
        // You roles model is available here
    }
}

See spring doc here(http://bit.ly/JCutg2) to know more about @ModelAttribute at method level

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