Dwz ajax session Timeout jump to the logon page (struts2 custom interceptor), dwzstruts2

Source: Internet
Author: User

Dwz ajax session Timeout jump to the logon page (struts2 custom interceptor), dwzstruts2

1. Define the struts2 Interceptor (there are many examples on the Internet)

The Code is as follows:

Package rt. intercepter; import java. util. map; import javax. servlet. http. httpServletRequest; import rt. pojo. userInfo; import rt. util. struts2Utils; import com. opensymphony. xwork2.Action; import com. opensymphony. xwork2.ActionInvocation; import com. opensymphony. xwork2.interceptor. abstractInterceptor; public class LoginValidate extends actinterceptor {/*****/private static final long serialVersionUID = 1L; @ Override public String intercept (ActionInvocation invocation) throws Exception {System. out. println ("interceptor intercept"); Map <String, Object> session = invocation. getInvocationContext (). getSession (); UserInfo userInfo = (UserInfo) session. get ("userInfo"); HttpServletRequest request = Struts2Utils. getRequest (); if ("XMLHttpRequest ". equalsIgnoreCase (request. getHeader ("X-Requested-With") | Request. getParameter ("ajax ")! = Null) {if (null = Struts2Utils. getSession () | null = userInfo) {Struts2Utils. getResponse (). getWriter (). write ("{\" statusCode \ ": \" 301 \ ", \" message \ ": \" the session has expired! Log on again! \ "}"); Return null ;}} return invocation. invoke ();}}

2. Configure the interceptor

<! -- Parent package for CRUD Action --> <package name = "crud-default" extends = "convention-default"> <! -- Added store interceptor Based on paramsPrepareParamsStack to ensure that actionMessage will not be lost after redirect --> <interceptors> <span style = "color: # ff0000; "> <interceptor name =" sessionout "class =" rt. intercepter. loginValidate "/> </span> <interceptor-stack name =" crudStack "> <interceptor-ref name =" timer "/> <interceptor-ref name =" logger "/> <interceptor-ref name = "store"> <param name = "operationMode"> AUTOMATIC </param> </interceptor-ref> <interceptor-ref name = "paramsPrepareParamsStack"/> <span style = "color: # ff0000; "> <interceptor-ref name =" sessionout "/> </span> </interceptor-stack> </interceptors> <default-interceptor-ref name =" crudStack "/> </package>


3. test:

After logging on, wait for one minute and click "Logon timeout". The logon page is displayed.

If you want to set the page not to jump after the session times out, but to log on in the pop-up box, Please modify it on the dwz initialization page:

<Script type = "text/javascript"> $ (function () {DWZ. init ("dwz. frag. xml ", {loginUrl:" login_dialog.html ", loginTitle:" Logon ", // The logon dialog box is displayed. // loginUrl:" login. jsp ", // jump to the login page statusCode: {OK: 200, error: 300, timeout: 301}, // [Optional] keys: {statusCode:" statusCode ", message: "message"}, // [Optional] pageInfo: {pageNum: "pageNum", numPerPage: "numPerPage", orderField: "orderField", orderDirection: "orderDirection "}, // [Optional] debug: false, // debug mode [true | false] callback: function () {initEnv (); $ ("# themeList "). theme ({themeBase: "themes"}); setTimeout (function () {$ ("# sidebar. toggleCollapse div "). trigger ("click") ;}, 10) ;}}) ;}); </script>

This is done, hoping to help you.

Related Article

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.