Struts 2 type converter input Checksum interceptor

Source: Internet
Author: User

In Struts 2, converters that convert between string types and common types can meet most of the conversion requirements, but cannot perform conversions between strings and user objects.
The OGNL project has a Typeconvert interface, which is the interface that the custom type converter must implement, and OGNL also provides an implementation class:
Defaulttypeconverter, the custom type converter (Userconverter Class) must override the Convertvalue () method of the Defaulttypeconverter class. The method can be used to convert strings and objects before each other

registering a local type converter : Registering a local type converter is specified with a local type conversion file, as long as it is added in the local type conversion file:
<propName>=<ConverterClass>
#指定Action的user属性需要使用UserConverter类来完成类型转换
User=org.crazy.app.converter.userconverter
At this point, the local type conversion succeeds
Global type converter : Registering a Global type converter should provide a xwork-conversion.properties, which consists of multiple
<propType>=<ConverterClass> composition,

Flow of Input validation
1. The type converter performs a type conversion on the request parameter and assigns the converted value to the property in the action.
2. If an exception occurs during the execution of a type conversion, the system will save the exception information to the Actioncontext,conversionerror interceptor to add the exception information to the fielderrors.
Regardless of whether the type conversion has an exception, the 3rd step will be entered.
3, the system through the reflection technology first call action in the ValidateXxx () method, XXX is the method name.
4. Call the Validate () method in action again.
5, through the above 4 steps, if the system in the Fielderrors error message (that is, the collection of error information is larger than 0), the system automatically forwards the request to a view named input.

If the fielderrors in the system does not have any error messages, the system performs the processing in the action.

Input check: client check to prevent incorrect input of normal browser, can reduce server load, service side prevent malicious user malicious behavior
<!--Specifies that the age property must be--> within the specified range;
<field name= "age"
<field-validator type= "int";
<param name= "min" >1</PARAM>
<param name= "Max" > 150</param>
<message> age must be between 1 and 150 </message>
</field-validator>
</field>
Each action has a quarantine file, the file name should be actionname-validation.xml
configuration struts.xml file
<!--user registered action-->
<action Name= "Regist" class= "org.crazyit.app.action.RegistAction" >
<!--type conversion failed, input checksum failed, to input corresponding view-->
<result name= "Input" >/WEB-INF/content/registForm.jsp</result>
<result>/web-inf/content/ Show.jsp</result> </action>
Add <s:fielderror> in registform.jsp to output the error prompt.
Manual completion of validation requires overriding the Validate () method, overriding the ValidateXxx () method

First, understand the interceptor :
The 1.struts2 interceptor is a method that accesses an action or action, the field is intercepted before or after it, and the Struts2 interceptor is pluggable, an interceptor is an AOP
Achieve Add when needed, and cancel the interceptor configuration when it is not needed.
2. Interceptor Stack (interceptor stack). The Struts2 interceptor Stack is a chain that binds interceptors in a certain order. Struts2 intercepts when accessing intercepted methods or fields
The interceptors in the chain are called in the order they were previously defined. The interceptor stack can also contain another interceptor stack.
Second, the realization of Struts2 interceptor principle
The implementation of the STRUTS2 interceptor is relatively straightforward, and when the action of STRUTS2 is requested, Struts 2 looks for the configuration file and instantiates the relative interceptor object based on its configuration, and then
String into a list, the last one to call the list of interceptors

Because struts2 such as file upload, data validation, package request parameters to action and other functions are implemented by the system's default Defaultstack interceptors, so we define the interception
The system needs to refer to the default defaultstack, so that the application can use the many features provided by the STRUTS2 framework.
<!--applications need to use interceptors configured under this element-
<interceptors>
<!--configuring Mysimple interceptors--
<interceptor name= "Mysimple" lass= "Org.crazyit.app.interceptor.SimpleInterceptor" >
<!--specify parameter values for the Interceptor--
<param name= "name" > Simple Interceptor </param>
</interceptor>
</interceptors>

<action name= "Login" class= "Org.crazyit.app.action.LoginAction" >
<result name= "Error" >/WEB-INF/content/error.jsp</result>
<result>/WEB-INF/content/welcome.jsp</result>
<!--Configure the system's default interceptor--
<interceptor-ref name= "Defaultstack"/>
<!--apply a custom mysimple interceptor--
<interceptor-ref name= "Mysimple" >
<param name= "name" > renamed Interceptor </param>
</interceptor-ref>
</action>

To customize the interceptor you need to implement the Com.opensymphony.xwork2.interceptor.Interceptor interface: Contains init (), Destory (), intercept () three methods
The Abstractinterceptor class provides an empty implementation of the Init (), Destory () method, which can be simplified by interceptor inheritance that does not require open resources
Simpleinterceptor extends Abstractinterceptor implements the Intercept () method,

Struts 2 type converter input Checksum interceptor

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.