Let your Struts process the date type

xiaoxiao2021-03-06  144

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 Solution speaking the first case: Create the following classes: 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 of YYYY YYYY YYYY / Mm / dd " value.getClass ());}}} else {throw new conversionException (" Input is not a character type " value.getClass ());}}} then use it in your system as follows ( Web init method) ConvertUTILS.REGISTER (New DateConvert (), java.util.date.class; parameter is used to indicate how the DATECONVERT class is responsible for processing java.util.date types how to display the Date type in 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.app End (ResponseUtils.Filter (Value)) Code Row, 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);} Catch (Exception EX) {System.Out.println ("Form Error:" EX.getMessage ());}}} else {if (value == null) value = ""; results.Append (Responseutils.Filter Value.toString ());}} results.append ("/"); results.append (prepareEventHandlers ()); results.append (getElementClose ()); // print This Field To Our Output WriterResponseutils.write (PageContext, Results.Tostring ()); // Continue Processing this page

Return (eval_body_tag); recompile struts into struts.jar. Putting the principles in the project you need, don't say more, see the Struts source code is understood :) Hope can improve your development efficiency

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

New Post(0)