This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12800|Reply: 5

[JavaEE] struts2 custom interceptor

[Copy link]
Posted on 12/28/2014 9:32:45 PM | | |
1. Write an action first

package action; public class InterceptorAction {                public String fun1() {                return "ok";        }}


2. Configure action

        <package name="base" extends="struts-default">                <global-results>                <result name="message">/index.jsp</result>                <result name="message">/WEB-INF/page/message.jsp </result>        </global-results>        </package>        <package name="delversi" namespace="/test" extends="base">                <interceptors>                <interceptor name="permission" class="interceptor.InterceptorDemo"></interceptor>                <interceptor-stack name="permissionStack">                        <interceptor-ref name="defaultStack"></interceptor-ref>                        <interceptor-ref name="permission"></interceptor-ref>                </interceptor-stack>        </interceptors>        <action name="interceptor_*" class="action.InterceptorAction" method="{1}">                <interceptor-ref name="permission"></interceptor-ref>                <result name="ok">/ddd.jsp</result>        </action>
The interceptor stack is defined to be able to use the system's built-in interceptors

3. Write a permission blocker

package interceptor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class InterceptorDemo implements Interceptor {        public void destroy() {                // TODO Auto-generated method stub                        }        public void init() {                // TODO Auto-generated method stub } public String intercept(ActionInvocation invocation) throws Exception { System.out.println("Enter interceptor"); }                if (ActionContext.getContext().getSession().get("user") !=null ) {                        return invocation.invoke();                }else { ActionContext.getContext().put("message", "no permission");                        return "message";                }                        }}Logged in, returned OK, and returned message if not

4. Write a jsp and inject user into the session

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% request.getSession().setAttribute("user","111");%>



Please indicate the source for reprintinghttp://blog.csdn.net/z1104222568/article/details/42218125

Comments

to remove the picture  Posted on 2014-12-29 10:16




Previous:Indonesian official: The missing AirAsia flight did not send a distress signal
Next:The explosion of a manure suction truck in Hechi, Guangxi, has improved the quality of the people
Posted on 12/28/2014 9:39:26 PM |
The picture is wrong and won't load
 Landlord| Posted on 12/29/2014 12:25:29 AM |
admin posted on 2014-12-28 21:39
The picture is wrong and won't load

Copied from my blog
 Landlord| Posted on 12/29/2014 12:25:30 AM |
admin posted on 2014-12-28 21:39
The picture is wrong and won't load

Copied from my blog
 Landlord| Posted on 12/29/2014 12:25:32 AM |
admin posted on 2014-12-28 21:39
The picture is wrong and won't load

Copied from my blog
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com