Type conversions in Struts2

Source: Internet
Author: User

1. Type conversions in Struts2

We know that data submitted to the background via HTTP is in the form of strings, and the type of data we need is certainly not just a string type. So, we need type conversion!

In Struts2, the concept of type conversion can be used to convert a specific type to a string, in addition to converting the string passed by the interface to a specific type (convertfromstring) (that is, when we want to render a certain type of object in the JSP, To convert it to a string to display) (convertertostring).

In Struts2, there are two ways to support global type conversions and local type conversions. The so-called global type conversion, in all Action/model, uses the same type converter to transform a particular type of object, whereas a local type conversion means a specific type converter defined for a property in a Action/model.

How do I write a type converter?

Whether it is a global type conversion or a local type conversion, its converters are written in the same way! As long as you inherit Strutstypeconverter, override the method.

Global Type Conversions

The same type of attribute definition converter for the entire system

Just:

1. Define the Xwork-conversion.properties file in the root directory of the classpath

2, in the file in such a format to declare which type to use which converter:

a) the full path class name of the property type = The full path class name of the converter

3, STRUTS2 will be able to automatically discover this file, and according to the definition of the specific type of call you specified type converter for type conversion

such as the point type:

Package Cn.com.leadfar.model;

Public class Point {

Private int left;

Private int right;

Public int GetLeft () {

return left;

}

Public void setleft (int left) {

this. left = left;

}

Public int getRight () {

return right;

}

Public void setright (int right) {

this. Right = right;

}

}

Converters for Point types:

Package cn.com.leadfar.struts2.actions;

import Java.util.Map;

import Org.apache.struts2.util.StrutsTypeConverter;

import Cn.com.leadfar.model.Point;

Public class Pointconverter extends strutstypeconverter {

@Override

Public Object convertfromstring (Map context, string[] value, Class totype) {

TODO Pre-condition judgment

String p = value[0];

String[] PS = P.split (",");

int left = Integer. parseint (Ps[0]);

int right = Integer. parseint (Ps[1]);

Point point = new point ();

Point.setleft (left);

Point.setright (right);

return point;

}

@Override

Public String converttostring (Map context, Object point) {

Point P = [Point] point;

return p.getleft () + "-" +p.getright ();

}

}

The contents of the Xwork-conversion.properties file are as follows:

Cn.com.leadfar.model.point=cn.com.leadfar.struts2.actions.pointconverter

Local type conversions

A converter that is defined for a property of an action or model

1, in the same package with Action/model , define the Action/model class name-conversion.properties file

2, inside the file in such a format to declare which property needs to use which type converter:

Property name = Converter's full path class name

For example: For java.util.Date types, we can declare different classes using different type converters

Package Cn.com.leadfar.model;

import java.util.Date;

Public class User {

Private Date endDate;

Public Date Getenddate () {

return endDate;

}

Public void setenddate (Date endDate) {

this. endDate = EndDate;

}

}

Package cn.com.leadfar.struts2.actions;

import java.util.Date;

import Cn.com.leadfar.model.Point;

import Cn.com.leadfar.model.User;

import Com.opensymphony.xwork2.ModelDriven;

Public class Useraction implements modeldriven{

Private User user;

Private Date begindate;

@Override

Public Object Getmodel () {

if (user = = null) {

user = new user ();

}

return user;

}

Public String Add () {

return "Success";

}

Public User GetUser () {

return user;

}

Public void setUser (user user) {

this. user = user;

}

Public Date Getbegindate () {

return begindate;

}

Public void setbegindate (Date begindate) {

this. begindate = Begindate;

}

}

Let's say we want the Begindate property in the Useraction class and the EndDate property in the user class to use a different type converter, as follows:

Package cn.com.leadfar.struts2.actions;

import java.text.ParseException;

import Java.text.SimpleDateFormat;

import Java.util.Map;

import Org.apache.struts2.util.StrutsTypeConverter;

Public class Begindateconverter extends strutstypeconverter {

Private SimpleDateFormat format = new simpledateformat ("Yyyy-mm-dd");

@Override

Public Object convertfromstring (Map context, string[] value, Class totype) {

String d = value[0];

Try {

return format.parse (d);

} catch (ParseException e) {

E.printstacktrace ();

}

return null;

}

@Override

Public String converttostring (Map context, Object date) {

return Format.format (date);

}

}

Package cn.com.leadfar.struts2.actions;

import java.text.ParseException;

import Java.text.SimpleDateFormat;

import Java.util.Map;

import Org.apache.struts2.util.StrutsTypeConverter;

Public class Enddateconverter extends strutstypeconverter {

Private SimpleDateFormat format = new simpledateformat ("Yyyy/mm/dd");

@Override

Public Object convertfromstring (Map context, string[] value, Class totype) {

String d = value[0];

Try {

return format.parse (d);

} catch (ParseException e) {

E.printstacktrace ();

}

return null;

}

@Override

Public String converttostring (Map context, Object date) {

return Format.format (date);

}

}

Well, we need to create a file under the user Class package: Cn.com.leadfar.model, named as follows:

User-conversion.properties, the contents of the file are as follows:

Enddate=cn.com.leadfar.struts2.actions.enddateconverter

Represents the EndDate property in the user class, using the Enddateconverter type converter.

Then, in the package where the Useraction class is located: Cn.com.leadfar.struts2.actions, create a file that is named as follows:

Useraction-conversion.properties, the contents of the file are as follows:

Begindate=cn.com.leadfar.struts2.actions.begindateconverter

Represents the Begindate property in the Useraction class, using the Begindateconveter type converter.

Note that only global type conversions are supported in Struts1, but local type conversions are not supported!

Type conversions in Struts2

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.