开发者

Spring MVC equivalent for Struts 2 Preparable

开发者 https://www.devze.com 2023-01-07 03:24 出处:网络
Is there something similar to Struts 2 Preparable interface / prepare method in Spring 3 MVC? That is, a method executed every time the controller is requested.

Is there something similar to Struts 2 Preparable interface / prepare method in Spring 3 MVC?

That is, a method executed every time the controller is requested.

Thanks.

EDIT: What I want to achieve, for example, is to fill a group of properties depending on the user I am, for every request in this cont开发者_如何转开发roller, trying to avoid this:

@Controller
@RequestMapping("my")
public class MyController {

    private void fillProperties() {...}

    public void request1() {
        fillProperties();
        ...
    }

    public void request2() {
        fillProperties();
        ...
    }

}


You can use Interceptors


Here are a couple of options:

  • use a ServletFilter
  • add an AOP before advise point cut to match the controller method(s) you want.


  • Make a constructor MyController(){}
  • and call all methods

or

  • You can also use annotations @postconstruct and @predestroy annotations

https://docs.oracle.com/javaee/5/api/javax/annotation/PostConstruct.html

0

精彩评论

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