Analysis and use of Struts2 Interceptor-bottom
I. Case study: The study of interceptor ended here. The reason for this is that it is divided into four chapters to give you a systematic understanding of the principle and implementation method of interceptor, on the other hand, let everyone learn the benefits of viewing the source code and experience the source code. Finally, this chapter will be summarized in a small case.
Case: implement the login verification function.
Ii. Case Analysis: after the project is configured, we first access login1 in the browser. We do not enter admin. If we change other words, the current logon page will be skipped. In this case, when we access login2 in a browser, the error. jsp page is displayed, proving that the interceptor has successfully intercepted login2. Access login1 again, enter admin correctly, and then jump to the success. jsp page. Then we access login2, And the success. jsp page is also displayed. Verify that the logon is successful.
A) Create LoginAction. java and Login2Action. java. Determine the user name and password in LoginAction. java. If the operation succeeds, the logon object is placed in the session. If the operation succeeds, the logon page is displayed.
if(user.getUsername().trim().equalsIgnoreCase("admin") && user.getPassword().equals("admin")){ActionContext.getContext().getSession().put("user", user);return SUCCESS;}
B) Create the interceptor MyInterceptor. java and implement the judgment logic: determine whether the current access action is LoginAction. java, yes, run it directly, and call the next interceptor. No, the system checks whether a user object exists in the session. If a user object exists, it is executed normally. If the user object does not exist, the logon page is displayed.
Object obj = invocation.getAction().getClass();if(LoginAction.class == obj){return invocation.invoke();}User user = (User) invocation.getInvocationContext().getSession().get("user");if(null == user){return Action.LOGIN;}return invocation.invoke();
C) Configure struts. xml
Define your own interceptor and interceptor Stack
Iii. Experience:
A) there is an app under the struts package we downloaded. The war package below can be directly put under tomcat for access. These are some small functions that help you understand and improve struts.
B) when everyone is fine, be familiar with their own compilation tools. I used eclipse before, but now it is myeclipse. Now I feel like it is getting better and better, and there is an impulse to use it unconsciously. Haha. Let everyone know, mainly to understand some shortcut keys, you can Baidu, there are many. This will help increase your preference for programming and development.
4. Reference project: http://download.csdn.net/detail/u011638419/8207123
5. happy every day:
Tom and his friends passed by a watermelon stall and saw many large watermelons in it. Then they asked the stall owner: the watermelon is not sweet!
It must be sweet.
Oh, pick a sweet one for me!
The right to final interpretation belongs to partner aishang (www.23hhr.com). Please indicate the source for reprinting.