Type Conversion failed: if the action class does not implement the validationaware interface, Struts will continue to call its Action Method in case of a type conversion error, as if nothing had happened. if the action class implements the validationaware interface (generally it inherits the actionsupport class, because the default action class has implemented the validationaware interface we need ): struts will not continue to call its Action Method in case of a type conversion error: struts will check whether the declaration of the related action element contains an input result. if yes, Struts will forward the control to the result element (usually configured to the page where an error occurs). If no input result exists, struts throws an exception as a member of the default interceptor,
The conversionerror interceptor adds error messages related to type conversion (prerequisite: The action class must implement the validationaware Interface) and saves the original values of each request parameter.If the field label is not a simple topic, illegal field input will lead to an error message in the following format: illegal character conversion "Age" overwrite the default error message
Create the actionclassname. properties file and classname file in the package where the corresponding action class is located.
That is, the Class Name of the action class containing the input field
Add the following key-value pairs to the property file:Invalid. fieldvalue. fieldname = xxx: custom error message style: Each error message is packaged in an HTML span element. You can overwrite the CSS style (Template) marked as errormessage by overwriting its rows. fielderror in simple. FTL defines a simple topic to change the format of the error message. show error message: for a simple topic, you can use the <s: fielderror> </S: fielderror> label to Display error messages. Question 1: How to overwrite the default error message? Because the getfielderrors () method in actionsupport has already defined the format of the error message. to unify the layout, We need to overwrite the error message.
1). Create a new one in the package where the corresponding action class is located.
Actionclassname. properties file, actionclassname is the class name of the action class containing the input field
2) Add the following key-value pairs to the property file: Invalid. fieldvalue. fieldname = xxx
Question 2: If it is a simple topic, will the error message be automatically displayed? (Not automatically displayed). What if not displayed?
1) through the debug tag, we can see that if a conversion error occurs, there will be a fielderrors attribute in the action (implemented validationaware Interface) object of the value stack.
The attribute type is Map <string, list <string> key: field (attribute name), value: list composed of error messages. Therefore, you can use LE or ognl.
To display the error message: $ {fielderrors. if the age [0]} el expression is a common attribute, you can get the attribute value by $ {name}. If it is a list, you need $ {listname. name [Index]}
2). You can also use the S: fielderror label to display. You can use the fieldname attribute to display errors of the specified field.
Question 3. If the simple topic is used and <s: fielderror fieldname = "Age"> </S: fielderror> is used to display the error message, the message is displayed in
Ul, Li, and span. How to remove ul, Li, and span?
Fielderror. FTL under template. Simple defines the style of the error message in the S: fielderror label under the simple topic. Therefore, modify
You can create the template. Simple package under SRC, create the fielderror. FTL file, and add the content in native fielderror. FTL.
Copy to the newly created fielderror. FTL and remove ul, Li, and span.
Type conversion in struts2 failed