struts2-type converter

Source: Internet
Author: User
Tags first string

STRUTS2 will convert the received data to the corresponding data type after receiving the data. Here is the role of the type converter in Struts2, where the default type converter is as follows (in the Struts-default.xml file):

The page data is submitted, the converter executes the convertvalue () method after the data is received, the corresponding string string is converted according to the intrinsic corresponding property type, and when the data is passed back to the page, there is a converter that converts it into a string string.

To implement data echo in Struts2 (when the format is wrong, jump back to the submission page, display the data and prompt for errors) to use the struts-tags label, and under the Struts.xml corresponding page action, add the name = "Input" page (Struts2 is required by default).

<result name= "Input" >/test.jsp</result>

test.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
    pageencoding=" Utf-8 "%>
<%@ taglib uri="/struts-tags "prefix=" s "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

As above, there will be hints and data echo when the data format is wrong:

Custom type converters (Time-type converters):
1. Reference source

2, Mydateconvert.java

public class Mydateconvert extends Defaulttypeconverter {//value accepts that the String[],class is the type to be converted, because the page to the server and the server to the page are going through the converter,
    With internal is bidirectional logic. 
        @Override public Object Convertvalue (object value, Class totype) {SimpleDateFormat SDF = null;//new a time format empty object try{if (ToType = = Date.class) {//page to server-side String datestr = ((string[]) value) [0];//gets the data to be converted to the Date parameter The first string string SDF = Getsimpledateformat (DATESTR);//Gets the corresponding format Actioncontext.getcontext () by method. Getsessi
        On (). put ("SDF", SDF);//Convert the format to the session in order to format the return Sdf.parse (DATESTR) in the Echo and page fetch data;//returns the date value converted by format} else if (ToType = = String.class) {//service-to-page date date = (date) value;//echo time data single value, strong to Date SDF = (Sim 
        Pledateformat) Actioncontext.getcontext (). Get ("SDF");//session gets the format return Sdf.format (date);//returns the string string converted by format }}catch (ParseException e) {e.printstacktrace ();//This parsing exception will only be thrown in the background, not in the Echo page hints, so in the method of getting the format throws the type conversion XOR
        Often} return Super.convertvalue (value, ToType); The private SimpleDateFormat Getsimpledateformat (String datestr) {//is judged according to the format here, and throws an exception Sim if there is no corresponding format
        Pledateformat SDF = null;
        if (Pattern.matches ("^\\d{4}-\\d{2}-\\d{2}$", Datestr)) {//Use module matching to determine the format SDF = new SimpleDateFormat ("Yyyy-mm-dd"); }else if (pattern.matches ("^\\d{4}/\\d{2}/\\d{2}$", Datestr)) {SDF = new SimpleDateFormat ("Yyyy/mm/dd")
        ;
        }else if (pattern.matches ("^\\d{4}\\d{2}\\d{2}$", Datestr)) {SDF = new SimpleDateFormat ("YyyyMMdd");
    }else {throw new typeconversionexception ();//Joge does not conform then throws an exception} return SDF;
 }

}

3. This is a custom converter, to be configured, specify the converter for the action.
Register by local type conversion: Create a new properties file under the specified action package, action name-conversion.properties content: Property = Fully qualified class name of the converter

Run:

4, here the data echo after the error prompt is English, so you can customize: under the action package, create a new action name. Properties content: Invalid.fieldvalue. Variable name = Error message


5. Test run:

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.