A common application is to access a page because of insufficient permissions. The user-friendly design enables the system to jump to the page that the user originally needs to access after login. This can be achieved through the interceptor.
Obtain the request address in the interceptor that we verify the user login and store it in the session.
1 package COM. tuanplus. interceptor; 2 3 Import Java. util. map; 4 Import javax. servlet. HTTP. httpservletrequest; 5 import Org. apache. struts2.servletactioncontext; 6 Import COM. opensymphony. xwork2.actioncontext; 7 Import COM. opensymphony. xwork2.actioninvocation; 8 Import COM. opensymphony. xwork2.interceptor. interceptor; 9 10/** 11 * Verify User Login 12*13 * @ author mzule14 * 15 */16 public class userlogininterce Ptor implements interceptor {17 18 Private Static final long serialversionuid = 1593745236481514166l; 19 20 public void destroy () {21} 22 23 public void Init () {24} 25 26 Public String intercept (actioninvocation Invocation) throws exception {27 actioncontext context = invocation. getinvocationcontext (); 28 // get session29 Map <string, Object> session = context. getsession (); 30 Object User = session. get ("User"); 31 // The user has not logged on to 32 If (user = NULL) {33 // get the httpservletrequest object 34 httpservletrequest Req = servletactioncontext. getrequest (); 35 // get the address of this request. The request address contains the application name. Perform the substring operation to remove application name36 string Path = req. getrequesturi (). substring (10); 37 // obtain the request parameter 38 string querystring = req. getquerystring (); 39 // prevent null pointer 40 if (querystring = NULL) {41 querystring = ""; 42} 43 // piece together to get the address 44 stri before login Ng realpath = path + "? "+ Querystring; 45 // save it to the session to conveniently call 46 sessions. put ("prepage", realpath); 47 return "login"; 48} 49 // The user has logged on and allowed 50 return invocation. invoke (); 51} 52 53}
Add the string-type prepage attribute to the user's login action to store the prepage value of the interceptor into the session (that is, the pre-login request address ).
1 package COM. tuanplus. action; 2 3 Import COM. tuanplus. po. user; 4 Import COM. tuanplus. service. iuserservice; 5 import COM. tuanplus. util. authcodeutil; 6 7/** 8 * login action 9*10 * @ author mzule11 * 12 */13 public class loginaction extends baseaction {14 15 Private Static final long serialversionuid =-6179170126070438432l; 16 private iuserservice userservice; 17 private user; 18 // Verification Code 19 private string auth; 20 // 21 private string prepage before logon; 22 23 Public String execute () {24 // obtain the login user object 25 user seuser = userservice. get (user. getemail (); 26 // Add session27 session. put ("user", seuser); 28 // obtain the address of the page before the jump to the logon interface. The interceptor provides 29 prepage = (string) session. get ("prepage"); 30 // clear the data in the session 31 session. remove ("prepage"); 32 If (prepage = NULL) {33 // not redirected to the login page by the interceptor, visit the login page 34 return "myorder "; 35} else {36 return success; 37} 38} 39... 40}
Configure the prepage attribute of action in struts. XML to determine the physical view resource.
1... 2 <! -- Login --> 3 <action name = "login" class = "loginaction"> 4 <result type = "redirectaction" >$ {prepage} </result> 5 <result name = "myorder" type = "redirectaction"> myorder </result> 6 <result name = "input">/login. JSP </result> 7 </Action> 8...
Go to the pre-Logon page after logon in struts2