STRUTS2 custom type conversions, and handling type conversion errors

Source: Internet
Author: User

Struts2 Custom type conversions:

The types received from the foreground are all strings, and some of the basic type conversions from theStruts2 sometimes do not meet our special needs, such as: date string input format, and some custom classes, directly to the background, we need some of our own converters:

Example :

I have a point type:

Package Cn.jnit.bean;

public class Point {

Private Integer x;

Private Integer y;

Public Point () {

}

Public point (Integer x, integer y) {

this.x = x;

This.y = y;

}

Public Integer GetX () {

return x;

}

public void SetX (Integer x) {

this.x = x;

}

Public Integer GetY () {

return y;

}

public void Sety (Integer y) {

This.y = y;

}

}

Test type converter: My front-desk struts form

<s:form action= "point" method= "POST" >

<s:textfield name= "Point" ></s:textfield>

<s:submit value= "Submit" ></s:submit>

</s:form>

<%--<s:property value= "point"/>--%>

Action for the form:

Package cn.jnit.action;

Import Cn.jnit.bean.Point;

Import Com.opensymphony.xwork2.ActionSupport;

public class Convertacion extends actionsupport{

private point Point;

@Override

Public String Execute () throws Exception {

System.out.println (Point.getx ());

System.out.println (Point.gety ());

return SUCCESS;

}

Public Point GetPoint () {

return point;

}

public void SetPoint (point point) {

This.point = point;

}

}

We found the Xwork-core-2.2.1.jar core pack.COM.OPENSYMPHONY.XWORK2in thecom.opensymphony.xwork2.conversionin theTypeconverter.classin theCom.opensymphony.xwork2.conversion.TypeConverterInterfaces: ImplementingTypeConverterthe interface class hasDefaulttypeconverterclass, we can see the inheritance fromDefaulttypeconverterClass has an abstract class in the classStrutstypeconverter, our custom classes inherit from theStrutstypeconverter, and then rewrite the two methods:

1: encapsulating string Data from the foreground into objects

@Override

Public Object convertfromstring (Map context, string[] values, Class toclass) {}

2: convert background object back to string data echo to foreground

@Override

Public String converttostring (Map context, Object o) {}

Then my custom conversion class:

Package Cn.jnit.convert;

Import Java.util.Arrays;

Import Java.util.Map;

Import Org.apache.struts2.util.StrutsTypeConverter;

Import com.opensymphony.xwork2.conversion.TypeConversionException;

Import Cn.jnit.bean.Point;

public class Myconvert extends Strutstypeconverter {

@Override

Public Object convertfromstring (Map context, string[] values, Class toclass) {

/* SYSTEM.OUT.PRINTLN ("--------------------------Map---------------");

For (Object Obj:context.keySet ()) {

System.out.println (obj);

}

System.out.println ("-------------------------------");

System.out.println (arrays.tostring (values));

System.out.println ("-----------class--------------------");

System.out.println (Toclass);

System.out.println ("-------------------------------------------"); */

Point P=new Point ();

System.out.println (Values[0]);

String string =values[0].split (",") [0];

if (! ( Values[0].length () >0&&values[0].matches ("\\d{2,5},\\d{2,5}")) {

throws an exception if the value entered in the foreground page does not match the value of the split in my custom type conversion method

throw new Typeconversionexception ();

}

P.setx (Integer.valueof (Values[0].split (",") [0]);

P.sety (Integer.valueof (Values[0].split (",") [1]);

TODO auto-generated Method Stub

return p;

}

@Override

Public String converttostring (Map context, Object o) {

Point p= o;

return P.getx () + "," +p.gety ();

}

}

Because of the form that Struts comes with, the error message is automatically added to the submitted label, and if you use an HTML form, you need to add <s:fielderror> </s:fielderror> will display an error message ( a form that isrecommended with struts).

However, the error message is strut2 's own English message, if you want to change the error message, then we have two options:

The first is the type converter that is applied to the global scope, specifically:

First we need to add in struts.xml :

<constant name= "struts.custom.i18n.resources" value= "message" ></constant>

Because by default, all type conversion errors are generic i18n message keys

Xwork-default.invalid.fieldvalue to report the error message. Then we need to create a message.properties file under src , and then the inside is simply right:Name: Xwork.default.invalid.fieldvalue value: Invalid values for field '{0}' (in Properties view, if you use the Source The view is displayed as:xwork.default.invalid.fieldvalue=\u5b57\u6bb5\u201c{0}\u201d\u7684\u503c\u65e0\u6548

The second is to apply a type converter for a particular class, specifically:

In the action package, create a properties file with the same name as the action , and the Name=value are:

Invalid.fieldvalue.point=point conversion error,

If this class is a custom type, you need to add one: the -conversion.properties file with the same name as the Action name The value is point,value : The full class name of the custom type converter, such as:Point=cn.jnit.convert.myconvert

Finally remember that all properties file name a letter can not be wrong, otherwise no effect!

STRUTS2 custom type conversions, and handling type conversion errors

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.