Struts logon case and custom interceptor

Source: Internet
Author: User

Struts logon case:
Struts. xml
<Struts>
<Constant name = "struts. devmode" value = "true"/>
<Constant name = "struts. Custom. i18n. Resources" value = "messages"> </constant>
<Package name = "Basic" extends = "struts-Default" namespace = "/">
<Interceptors>
<Interceptor name = "Privilege" class = "cn. itcast. Interceptor. privilegeinterceptor"> </interceptor>
</Interceptors>
<Global-Results>
<Result name = "login">/login. jsp </result>
</Global-Results>
<Action name = "book _ *" class = "cn. itcast. Action. bookaction" method = "{1}">
<Interceptor-ref name = "Privilege"> </Interceptor-ref>
<Interceptor-ref name = "defaultstack"> </Interceptor-ref>
</Action>
</Package>
<Package name = "login" extends = "struts-Default" namespace = "/">
<Action name = "login" class = "cn. itcast. Action. loginaction">
<Result name = "input">/login. jsp </result>
<Result>/index. jsp </result>
</Action>
</Package>
</Struts>
Messages. properties (International dialect information ):
Username. Required = user name cannot be blank
Username. Length = the user name cannot be less than three characters
Password. Required = the password cannot be blank
Loginfail = incorrect username or password

Action:
Loginaction:
Public class loginaction extends actionsupport implements modeldriven <user> {
Private user = new user ();
@ Override
Public user GetModel (){
Return user;
}
@ Override
Public String execute () throws exception {
Queryrunner QR = new queryrunner (dbcputil. getdatasource ());
String SQL = "select * from user where username =? And Password =? ";
User loginuser = QR. Query (SQL, new beanhandler <user> (user. Class), user. GetUserName (), user. GetPassword ());
If (loginuser = NULL ){
This. addactionerror (this. gettext ("loginfail "));
Return input;
} Else {
Servletactioncontext. getrequest (). getsession (). setattribute ("loginuser", loginuser );
Return success;
}
}
}

LoginAction-validation.xml:
<! Doctype validators public
"-// Apache struts // xwork validator 1.0.3 // en"
Http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd>
<Validators>
<Field name = "username">
<Field-validator type = "requiredstring">
<Message key = "username. Required"> </message>
</Field-validator>
<Field-validator type = "stringlength">
<Param name = "minlength"> 3 </param>
<Message key = "username. length"> </message>
</Field-validator>
</Field>
<Field name = "password">
<Field-validator type = "requiredstring">
<Message key = "password. Required"> </message>
</Field-validator>
</Field>
</Validators>

JSP:
Login. jsp:
<S: fielderror/>
<S: actionerror/>
<Form action = "$ {pagecontext. Request. contextpath}/login. Action" method = "Post">
Username: <input type = "text" name = "username"/>
Password: <input type = "password" name = "password"/>
<Input type = "Submit" value = "login">
</Form>
</Body>


Custom interceptor
The use of interceptor comes from the idea of Spring AOP (for Aspect-Oriented Programming ).
The interceptor adopts the responsibility chain mode.
* In the responsibility chain mode, many objects are connected by each object's reference to its next home to form a chain.
* Each node of the responsible chain can continue to call the next node or stop the process from being executed.

In struts2, multiple interceptors can be defined to form an interceptor stack (each interceptor in the stack is called sequentially)

1. All interceptors of struts2 must implement the interceptor interface.
2. The abstractinterceptor class implements the interceptor interface and provides a blank implementation for init and destroy.

In all practical development, the custom interceptor only needs to inherit the abstractinterceptor class and provides the intercept method implementation.

3. Common struts2 interceptor
<Interceptor-ref name = "modeldriven"/> model-driven
<Interceptor-ref name = "fileupload"/> File Upload
<Interceptor-ref name = "Params"> Parameter Parsing Encapsulation
<Interceptor-ref name = "conversionerror"/> type conversion error
<Interceptor-ref name = "validation"> request parameter verification
<Interceptor-ref name = "workflow"> intercept jump to the input View

Case: log on and use the custom Interceptor to control access to other actions.
Import jar packages (struts2 jar, c3p0, dbutils, MySQL driver)
Web. xml
Struts. xml
Jdbcutils tool class

Step 1: Compile index. jsp to provide four functions for adding, deleting, modifying, and querying books
Write a bookaction and provide four business methods

Step 2: complete the login function

Step 3: You must log in to manage books.
Use filter for permission control ---- filter all Web requests (all Web Resource Access)
Use the interceptor for permission control-mainly intercepts action access (cannot intercept JSP)

Define Interceptor to inherit abstractinterceptor

Configure interceptor
Method 1
<! -- Register the interceptor -->
<Interceptors>
<Interceptor name = "Privilege" class = "cn. itcast. Interceptor. privilegeinterceptor"> </interceptor>
</Interceptors>
<Action name = "book _ *" class = "cn. itcast. Action. bookaction" method = "{1}">
<! -- Use interceptor -->
<! -- When a custom interceptor is used, the default interceptor will become invalid. -->
<Interceptor-ref name = "defaultstack"> </Interceptor-ref>
<Interceptor-ref name = "Privilege"> </Interceptor-ref>
</Action>
Method 2
<! -- Register the interceptor -->
<Interceptors>
<Interceptor name = "Privilege" class = "cn. itcast. Interceptor. privilegeinterceptor"> </interceptor>
<! -- Custom interceptor Stack -->
<Interceptor-stack name = "privilegestack">
<Interceptor-ref name = "defaultstack"> </Interceptor-ref>
<Interceptor-ref name = "Privilege"> </Interceptor-ref>
</Interceptor-stack>
</Interceptors>

<! -- Set all actions in the current package to use the custom interceptor Stack -->
<Default-interceptor-ref name = "privilegestack"> </default-interceptor-ref>

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.