使用自定义的拦截器配置在struts配置文件的action中,还要引入defaultStack,否则使用了modelDriven的formBean将不能获取到form表单中字段的值。
action:
- public class LoginAction implements Action, ModelDriven<UserForm> {
- private UserForm userForm=new UserForm();
- private IUserBO iUserBO;
- public String login() {
- User user=iUserBO.getUser(userForm.getUsername(), userForm.getPassword());
- if (user!=null) {
- return SUCCESS;
- } else {
- return ERROR;
- }
- }
- public String execute() throws Exception {
- // TODO Auto-generated method stub
- return SUCCESS;
- }
- public UserForm getModel() {
- return userForm;
- }
- public IUserBO getiUserBO() {
- return iUserBO;
- }
- public void setiUserBO(IUserBO iUserBO) {
- this.iUserBO = iUserBO;
- }
- }
拦截器:
- public class FilterTest extends MethodFilterInterceptor {
- Logger logger = Logger.getLogger(this.getClass());
- @Override
- protected String doIntercept(ActionInvocation invocation) throws Exception {
- // TODO Auto-generated method stub
- logger.info("==========action执行前插入代码===============");
- String s=invocation.invoke();
- logger.info("==========invocation.invoke():"+s);
- logger.info("==========action执行后插入代码===============");
- return s;
- }
- }
struts配置文件:
- <struts>
- <package name="struts" extends="struts-default" namespace="/test">
- <interceptors>
- <interceptor name="testInterceptor" class="filterBase.FilterTest">
- </interceptor>
- </interceptors>
- <action name="loginAction" class="LoginAction" method="login">
- <result name="success">/success.jsp</result>
- <result name="error">/error.jsp</result>
- <interceptor-ref name="defaultStack"/>
- <!-- 通过配置参数excludeMethods,设置不被拦截的方法,excludeMethods被拦截的方法 -->
- <interceptor-ref name="testInterceptor">
- <param name="includeMethods">login</param>
- </interceptor-ref>
- </action>
- </package>
- </struts>
阅读(0)
|
评论(0)