Struts2 Interceptor (5): inherits the abstract class methodfilterinterceptor to implement the interceptor of the interception method.

Source: Internet
Author: User

By default, if we define an interceptor for an action, the interceptor intercepts all methods in the action. However, in some cases, we do not want to intercept all methods. We only need to intercept some specific methods. In this case, we need to use the method filter feature of the struts2 interceptor.

To implement method filtering, struts2 providesMethodfilterinterceptorAbstract class, which is a subclass of abstractinterceptor. If you need a custom Interceptor to support the method filtering feature, you should inherit methodfilterinterceptor and override it.Dointercept (actioninvocation Invocation)Method.

Index. jsp:

<% @ Page contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <HTML> 

Welcome. jsp:

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

Struts. xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"    "http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts>    <package name="demo" extends="struts-default">           <interceptors>          <interceptor name="methodFilter" class="interceptor.SimpleMethodFilterInterceptor"/>       </interceptors>              <action name="*" class="action.LoginAndRegisterAction" method="{1}">          <result name="success">/welcome.jsp</result>          <result name="error">/index.jsp</result>                    <interceptor-ref name="defaultStack"/>          <interceptor-ref name="methodFilter">            <param name="excludeMethods">register</param>            <param name="includeMethods">login</param>          </interceptor-ref>       </action>    </package></struts>

Simplemethodfilterinterceptor. Java:

Public class simplemethodfilterinterceptor extends methodfilterinterceptor {@ overridepublic string dointercept (actioninvocation Invocation) throws exception {system. out. println ("before the action is intercepted, of course you can do something here... "); string result = invocation. invoke (); system. out. println ("after the action is intercepted, of course you can do something here... "); return result ;}}

Loginandregisteraction. Java:

public class LoginAndRegisterAction extends ActionSupport {private String name;private String pass;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPass() {return pass;}public void setPass(String pass) {this.pass = pass;}public String login(){if(getName().equals("scott")&& getPass().equals("tiger")){Map<String,Object> session=ActionContext.getContext().getSession();session.put("name",getName());session.put("tip","login");return "success";}return "error";}public String register(){if(getName().equals("scott")&& getPass().equals("tiger")){Map<String,Object> session=ActionContext.getContext().getSession();session.put("name",getName());session.put("tip","register");return "success";}return "error";}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.