Let your Struts process the date type

xiaoxiao2021-03-06  127

The Struts Use date includes automatically transforming String into Date Fill to Form, and displaying the date in the Form in the specified format in the TEXTFIELD of HTML. First, the first case solution:

Create as follows:

Import java.util. *; import org.apache.commons.beanutils. *; import java.text.SIMPLEDATEFORMAT;

Public Class Dateconvert Implements Converter {Static SimpleDateFormat DF = New SimpleDateFormat ("YYYY / MM / DD"); public dateconvert () {

} Public Object Convert (Class Type, Object Value) {if (value == NULL) Return NULL; if ((String) Value) .trim (). Length () == 0) Return Null;

If (value instanceof string) {Try {returnif.Parse ((String) value);} catch (exception ex) {throw new conversionException ("Enter the date type is not YYYY / MM / DD" value.getClass () }

} Else {throw new conversionException ("Input is not a character type" value.getClass ());

}

Then in your system, use the following (such as the Web's init method) ConvertUTILS.REGISTER (New DateConvert (), java.util.date.class; parameter is used to indicate the transformation of the DATECONVERT class responsible for processing java.util.date types

The second case is how to display the date type in the form to HTML: text, I use the way to modify the Struts code, regenerate a new struts.jarorg.apache.struts.taglib.html.BasefieldTAG's DostartTag method to find If (value! = null) {results.Append (Value) The content below, you need to modify this code so that the output date type is as follows: if (value! = null) {results.append (Responseutils .filter (value));} else if (redisplay ||! "password" .equals (type)) {Object value = requestUtils.lookup (pageContext, name, proty, null); //system.out.println (" Lijz " value); if (value instanceof java.util.date) {//system.out.println ("date=" Value); if (value == null) value =" "; else {java.util .Date d = (java.util.date) value; try {results.Append (Responseutils.Filter (DF.Format (D)));} catch (exception ex) {system.out.println ("Form Error:" ex.getMessage ());}}} else {= (va) Lue == NULL) Value = ""; Results.Append (value.tostring ());}}}} }.Append ("/"); results.append (prepareEventHandlers (); results.Append (PrepareStyles ()); results.append (getElementClose ());

// Print this field tour output writer responseutils.write (pageContext, results.tostring ());

// Continity processing this page return (eval_body_tag); recompile struts into struts.jar. Putting the principles in the project you need, don't say more, look at the Struts source code, you will understand :) Hope can improve your development efficiency

转载请注明原文地址:https://www.9cbs.com/read-126804.html

New Post(0)