STRUTS2 type converter Configuration

Source: Internet
Author: User

The presentation layer or JSP page data is generally string type, and sometimes we need to convert it to the type we need. Struts used to encapsulate data with a javabean, and then write a converter. STRUTS2 provides a Defaulattypeconverter class for type conversions.

It is important to note that in the process of rewriting the configuration and transformation classes, the following points must be noted:

1. The name of the element inside the form should be consistent with the attributes inside the Registrar *-conversion.properties. For example: The form is as follows:

<s:form action= "Login" method= "POST" >
<s:textfield name= "username" label = "User name"/>
<s:textfield name= "password" label = "password" key= "pass"/>
<s:textfield name= "name" label = "name" key= "name"/>
<s:textfield name= "classes" label = "Class" key= "classes"/>
<s:textfield name= "Phone" label = "Telephone" key= "Phones"/>
<s:textfield name= "email" label = "email" key= "email"/>
<s:submit value = "Register" key= "Regist"/>
<s:reset value = "Refill"/>
</s:form>

To convert the username, the Registrar *-conversion.properties must be configured as follows:

Username=yanggui. Userconverter

Where Yangguij is the package name, Userconverter is the converter,

It is also important to note that the purpose of username is to manipulate it within the action class. In the STRUTS2, a column cutter is truncated to the action class, so in the action, define a class to be associated with the Usrname parameter, except for its type, which is getusername () and must be the only way to get the parameter from the bar cutter.

The following action handles the Usename code for the preceding form:

Package yang.stutent;

Import Yang.model.User;

Import com.opensymphony.xwork2.Action;


public class Registaction implements action{
private user user;
Private String tip;

public void SetUser (user user)
{
This.user = user;
}

public void Settip (String tip)
{
This.tip = tip;
}

Public User GetUser ()
{
return (This.user);
}

Public String Gettip ()
{
return (THIS.TIP);
}

Public String Execute () throws Exception
{
if (GetUser (). GetUserName (). Equals ("Yang")
&& GetUser (). GetPassword (). Equals ("123"))
{
Settip ("Conversion successful");
return SUCCESS;
}
Else
{
Settip ("conversion failed");
return ERROR;
}
}

}

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.