Empty field problem of actionform and Struts type converter

Source: Internet
Author: User

Blank field in actionform

The blank field of actionform indicates that when this input field is not in the form, the El expression is used to take the value and the JSP script is used to take the value. The other is that the form contains this input field, but there is no value. The El expression is used to take the value and the JSP script is used to take the value. The result is as follows:

There is no input field in the form. The value received by the JSP script is null, the string is null, And the integer is 0. The value received by the El expression is an empty string.
If the value in the form is not input, the values received by the JSP script and El expression are empty strings.

 

 

 

Struts type converter of actionform

Struts's automatic collection process for actionform:
* Place the page data in map. The key in map is the name in the page, and the value in map is the value in the page.
* Call the beanutils. setproperties method to set values in map to the actionform instance one by one. For each attribute in actionform
Call the corresponding Converter Based on the type, and then call the corresponding convert method to convert the corresponding string to the type specified in actionform

For Java. util. date, a custom type converter is required.

Because the data transmitted from the page is of the string type, different types are used in the background, so the converter is required to convert the data transmitted from the page.

Demo with an instance:

Fill in data on the home page:

<Li> test the actionform type converter </LI> <br>
<Form action = "typeconvert. Do" method = "Post">
Intvalue: <input type = "text" name = "intvalue"> <br> // test the integer type, that is, convert the input value to an int type converter.
Doublevalue: <input type = "text" name = "doublevalue"> <br> // test the double-precision value. After the value is input, the type converter converts it to double.

Booleanvalue: <input type = "text" name = "booleanvalue"> <br> // test the Boolean value. After the value is input, the type converter converts it to a Boolean value.
Java. SQL. Date: <input type = "text" name = "sqldate"> <br> // test SQL. Date
Java. util. Date: <input type = "text" name = "utildate"> <br> // test util. Date.
<Input type = "Submit" value = "Submit">
</Form>

Create an actionform for data collection:

 

/**
* Test type conversion in struts
* @ Author Administrator
*
*/
Public class typeconvertactionform extends actionform {

Private int intvalue;
 
Private double doublevalue;
 
Private Boolean booleanvalue;
 
Private java. SQL. Date sqldate; // the data type is Java. SQL. Date.
 
Private java. util. Date utildate;

Public int getintvalue (){
Return intvalue;
}

Public void setintvalue (INT intvalue ){
This. intvalue = intvalue;
}

Public double getdoublevalue (){
Return doublevalue;
}

Public void setdoublevalue (double doublevalue ){
This. doublevalue = doublevalue;
}

Public Boolean isbooleanvalue (){
Return booleanvalue;
}

Public void setbooleanvalue (Boolean booleanvalue ){
This. booleanvalue = booleanvalue;
}

Public java. SQL. Date getsqldate (){
Return sqldate;
}

Public void setsqldate (Java. SQL. Date sqldate ){
This. sqldate = sqldate;
}

Public java. util. Date getutildate (){
Return utildate;
}

Public void setutildate (Java. util. Date utildate ){
This. utildate = utildate;
}
}

 

Create action:

 

/**
* Test type conversion in struts
* @ Author Administrator
*
*/
Public class typeconverttestaction extends action {

@ Override
Public actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response)
Throws exception {
Return Mapping. findforward ("success ");
}

}

 

Create a display page:

Value on this page: If the value can be correctly retrieved, the conversion will be OK, otherwise there will be problems.

<Body>
Intvalue: $ {typeconvertform. intvalue} <br> // can be automatically converted
Doublevalue: $ {typeconvertform. doublevalue} <br> // can be automatically converted
Booleanvalue: $ {typeconvertform. booleanvalue} <br> // can be automatically converted
Java. SQL. datevalue: $ {typeconvertform. sqldate} <br> // can be automatically converted
Java. util. utilvalue: $ {typeconvertform. utildate} <br> // The conversion cannot be performed automatically. You need to write the converter yourself.
</Body>

Configure:

<Form-bean name = "typeconvertform" type = "com. Mine. Struts. typeconvertactionform"/>

<Action Path = "/typeconvert"
Type = "com. Mine. Struts. typeconverttestaction"
Name = "typeconvertform"
Scope = "request"
>
<Forward name = "success" Path = "/typeconvert_success.jsp"/>
</Action>

 

Start the test. The result is as follows:

Boolean: Yes, 1, on, and true are all converted to true type, case insensitive, otherwise converted to false
Date type conversion:
For Java. SQL. date, the format of the page date must be yyyy-mm-dd (separated by a horizontal line)
For Java. util. Date, Struts cannot be converted by default.

Steps for implementing a custom Converter
Implement the converter interface and the convert method
Register the implemented conerter. Generally, Servlet registration is used. the actionservlet in the action in the jar package. in class, there is an Init () method in this method, and many methods are called in this method. Among them, there is an inther ther () method, and the converter is registered here)
When using Servlet registration, pay attention to the label configuration, <load-on-startup> 10 </load-on-startup> (you can also use Struts PlugIn registration)

Custom converter process: (a custom java. util. Date converter is used as an example)

 

 

First, write a class to implement the converter interface and implement the convert method:

 

Import java. Text. parseexception;
Import java. Text. simpledateformat;
Import java. util. date;

Import org. Apache. commons. beanutils. converter;

/**
* Java. util. date type converter
* @ Author Administrator
*
*/
Public class utildateconverter implements converter {

Public object convert (class type, object Value ){
System. Out. println ("utildateconverter. value =" + value );
If (value = NULL ){
Return value;
}
If (value instanceof date ){
Return value;
}
Date d = NULL;
If (value instanceof string ){
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd"); // format
Try {
D = SDF. parse (string) value );
} Catch (parseexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Return D;
}

}

 

Write a servlet to register the custom converter:

 

Import java. util. date;

Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;

Import org. Apache. commons. beanutils. convertutils;

/**
* Register the java. util. Date Converter
* @ Author Administrator
*
*/
Public class utildateconverterinitwithservlet extends httpservlet {

@ Override
Public void Init () throws servletexception {// Method for re-writing initialization.
// System. Out. println ("utildateconverterinitwithservlet. INIT ()");
Convertutils. Register (New utildateconverter (), date. Class); // refer to the struts built-in converter registration method. The first parameter is a custom converter object, and the second parameter is a type. In this way, we will register our own converter into map.
}

}

 

The last step is to configure it: configure it in Web. XML (load-on-startup is initialized when the web server is started, as long as it is greater than 0)

<Servlet>
<Servlet-Name> utildateconverterinitwithservlet </servlet-Name>
<Servlet-class> com. bjsxt. Mine. utildateconverterinitwithservlet </servlet-class>
<Load-on-startup> 3 </load-on-startup> // It indicates initialization when Tomcat is started.
</Servlet>

 

 

Another method
Create a class to implement the plugin interface. There is an init method in this interface, which can be implemented here.

 

Import java. util. date;

Import javax. servlet. servletexception;

Import org. Apache. commons. beanutils. convertutils;
Import org. Apache. Struts. Action. actionservlet;
Import org. Apache. Struts. Action. plugin;
Import org. Apache. Struts. config. moduleconfig;

Public class utildateconverterinitwitplans ugin implements plugin {

Public void destroy (){
}

Public void Init (actionservlet servlet, moduleconfig config)
Throws servletexception {
System. Out. println ("utildateconverterinitwit2d-ugin. INIT ()");
Convertutils. Register (New utildateconverter (), date. Class );
}
}

 

Then configure in struts-config.xml :( at the end)

<Plug-in classname = "com. Mine. Struts. utildateconverterinitwitplans"/> // use the plug-in label to write the complete path
</Struts-config>

 

 

 

 

 

You can also refer to: http://twh1224.javaeye.com/blog/266929

2008 - 11 - 14 Struts custom type converter

Keyword: Java. util. date type converterStruts's automatic collection process for actionform:
* Place the page data in map. The key in map is the name in the page, and the value in map is the value in the page.
* Call the beanutils. setproperties method to set values in map to the actionform instance one by one. For each attribute in actionform
Call the corresponding Converter Based on the type, and then call the corresponding convert method to convert the corresponding string to the type specified in actionform

For Java. util. date, a custom type converter is required.

1. Implement the converter interface JavaCode

  1. PackageCom. TWh. Struts;
  2. ImportJava. Text. parseexception;
  3. ImportJava. Text. simpledateformat;
  4. ImportJava. util. date;
  5. ImportOrg. Apache. commons. beanutils. converter;
  6. /** 
  7. * Java. util. date type converter 
  8. * @ Author Administrator 
  9. */
  10. Public ClassUtildateconverterImplementsConverter {
  11. PublicObject convert (class type, object Value ){
  12. System. Out. println ("Value ="+ Value );
  13. If(Null= Value ){
  14. ReturnValue;
  15. }
  16. If(ValueInstanceofDate ){
  17. ReturnValue;
  18. }
  19. Date d =Null;
  20. If(ValueInstanceofString ){
  21. Simpledateformat SDF =NewSimpledateformat ("Yyyy-mm-dd");
  22. Try{
  23. D = SDF. parse (string) value );
  24. }Catch(Parseexception e ){
  25. // Todo auto-generated Catch Block
  26. E. printstacktrace ();
  27. }
  28. }
  29. ReturnD;
  30. }
  31. }
Package COM. TWh. struts; import Java. text. parseexception; import Java. text. simpledateformat; import Java. util. date; import Org. apache. commons. beanutils. converter;/*** Java. util. date type converter * @ author administrator **/public class utildateconverter implements converter {public object convert (class type, object Value) {system. out. println ("value =" + value); If (null = value) {return value;} If (value instanceof date) {return value;} date d = NULL; if (value instanceof string) {simpledateformat SDF = new simpledateformat ("yyyy-mm-dd"); try {d = SDF. parse (string) value);} catch (parseexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} return D ;}}

2. Register Java code through Servlet

  1. PackageCom. TWh. Struts;
  2. ImportJavax. servlet. servletexception;
  3. ImportJavax. servlet. http. httpservlet;
  4. ImportOrg. Apache. commons. beanutils. convertutils;
  5. /** 
  6. * Register the java. util. Date Converter 
  7. * @ Author Administrator 
  8. */
  9. Public ClassUtildateconverterinitwithservletExtendsHttpservlet {
  10. @ Override
  11. Public VoidInit ()ThrowsServletexception {
  12. // Register the java. util. Date Converter
  13. Convertutils. Register (NewUtildateconverter (), java. util. Date.Class);
  14. }
  15. }
Package COM. TWh. struts; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import Org. apache. commons. beanutils. convertutils;/*** register Java. util. date converter * @ author administrator **/public class utildateconverterinitwithservlet extends httpservlet {@ overridepublic void Init () throws servletexception {// register Java. util. date converter convertutils. register (New utildateconverter (), Java. util. date. class );}}

3. Configure in Web. XML (load-on-startup is initialized when the web server is started, as long as the value is greater than 0) Java code

    1. <Servlet>
    2. <Servlet-Name> utildateconverterinitwithservlet </servlet-Name>
    3. <Servlet-Class> Com. TWh. Struts. utildateconverterinitwithservlet </servlet-Class>
    4. <Load-on-startup>4</Load-on-startup>
    5. </Servlet>
 
<Servlet> <servlet-Name> utildateconverterinitwithservlet </servlet-Name> <servlet-class> COM. TWh. struts. utildateconverterinitwithservlet </servlet-class> <load-on-startup> 4 </load-on-startup> </servlet>

----------------------------------------------------------------------
Another method
2. Implement the Java code of the plugin Interface

  1. PackageCom. TWh. Struts;
  2. ImportJavax. servlet. servletexception;
  3. ImportOrg. Apache. commons. beanutils. convertutils;
  4. ImportOrg. Apache. Struts. Action. actionservlet;
  5. ImportOrg. Apache. Struts. Action. plugin;
  6. ImportOrg. Apache. Struts. config. moduleconfig;
  7. /** 
  8. * Register the java. util. Date Converter 
  9. * @ Author Administrator 
  10. */
  11. Public ClassUtildateconverterinitwit2d-uginImplementsPlugin {
  12. Public VoidDestroy (){
  13. }
  14. Public VoidInit (actionservlet servlet, moduleconfig config)
  15. ThrowsServletexception {
  16. // Register the java. util. Date Converter
  17. Convertutils. Register (NewUtildateconverter (), java. util. Date.Class);
  18. }
  19. }
Package COM. TWh. struts; import javax. servlet. servletexception; import Org. apache. commons. beanutils. convertutils; import Org. apache. struts. action. actionservlet; import Org. apache. struts. action. plugin; import Org. apache. struts. config. moduleconfig;/*** register Java. util. date converter * @ author administrator **/public class utildateconverterinitwit2d-ugin implements plugin {public void destroy () {} public void Init (actionservlet, moduleconfig config) throws servletexception {// register Java. util. date converter convertutils. register (New utildateconverter (), Java. util. date. class );}}

3. Configure Java code in the struts-config.xml

    1. <Plug-in classname ="Com. TWh. Struts. utildateconverterinitwithzugin"/>

 

 

 

 

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.