Summary of struts2 logon timeout interceptor

Source: Internet
Author: User

Problem domain: the login user has not interacted with the server for a long time. If the operation is performed again, the system prompts "timeout". Please log on again.

Solution: session timeout settings and struts interceptor intercept interactive operations


Implementation

Use web. xml configuration to set session Timeout

<Session-config>

<Session-timeout> 10 </session-timeout>

</Session-config>


Struts interceptor code implementation

Public class SessionIterceptor extends actinterceptor {@ Override public String intercept (ActionInvocation actionInvocation) throws Exception {ActionContext ctx = ActionContext. getContext (); Map session = ctx. getSession (); Action action = null; // this step is written because some actions inherit ActionSupport and some actions inherit BaseAction if (actionInvocation. getAction () instanceof Action) action = (Action) actionInvocation. getAction (); if (action instanceof LoginAction) {return actionInvocation. invoke ();} Operator operator = (Operator) session. get ("OPERATOR_SESSION"); if (operator = null) {HttpServletRequest request = (HttpServletRequest) actionInvocation. getInvocationContext (). get (ServletActionContext. HTTP_REQUEST); request. setAttribute ("timeoutMSG", "timeout, please log on again"); return "timeoutSession";} else {return actionInvocation. invoke ();}}}

Page capture transfer prompt information

<% @ Taglib prefix = "s" uri = "/struts-tags" %>

<S: property value = "# attr. timeoutMSG"/> <br/>


Configuration in struts. xml

<Package name = "myInterceptor" extends = "json-default"> <interceptors> <! -- Timeout interceptor --> <interceptor name = "timeoutSession" class = "com. sunyard. ices. web. interceptors. SessionIterceptor"> </interceptor> <! -- Interceptor stack --> <interceptor-stack name = "timeoutStack"> <interceptor-ref name = "timeoutSession"> </interceptor-ref> <interceptor-ref name = "defaultStack"> </interceptor-ref> </interceptor-stack> </interceptors> <! -- Configure the default interceptor --> <default-interceptor-ref name = "timeoutStack"/> <! -- Global Non-Logon interception redirects to the logon page --> <global-results> <result name = "timeoutSession">/login. jsp </result> </global-results> </package>

A package is specially configured because a large number of include operations are used. In other struts configuration files, you only need to inherit the package.

<package name="operatora" extends="myInterceptor">



So far, the problem has been solved. This is the first time that a beginner writes a blog post. To enhance memory, it is also convenient for beginners.

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.