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;
}
}
}