Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to input Date to a Struts2 action from a jsp?

Tags:

java

jsp

struts2

I am facing a lot of problems here in my jsp page. What tag I should use to get a date (yyyy-MM-dd) from user and store it in a Date property of a Struts2 action ?

The property in my action is declared to be of java.util.Date. I want the input from jsp page to land in this property.

please help.

I get Invalid field error (in JSP) if is use a s:textfield tag to manyally enter a date in the right format.

like image 569
Anand Avatar asked Mar 07 '10 18:03

Anand


People also ask

How do you accept a date input?

<input type="date"> <input> elements of type="date" create input fields that let the user enter a date, either with a textbox that validates the input or a special date picker interface. The resulting value includes the year, month, and day, but not the time.


2 Answers

This is how I solved this.

    <s:date name="nameOfInputVal" var="formattedVal"/>
    <s:textfield name="nameOfInputVal" value="%{#formattedVal}" key="labelkey" />

And in your message properties a key/value:

struts.date.format=dd/MM/yyyy

Which indicates the default format and you don't have to write it in each date tag.

Hope this helps

like image 31
Alfredo Osorio Avatar answered Oct 03 '22 05:10

Alfredo Osorio


I know this post is a bit old but a solution may be useful to others.

The default converter of Struts does not seem to work properly. The field error even occurs with readonly fields populated from the action.

I resolved it by defining my own converter:

  1. Create the converter class (using the date format you need):

    public class StringToDateTimeConverter extends StrutsTypeConverter {
        // WARNING not safe in multi-threaded environments
        private static final DateFormat DATETIME_FORMAT = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
    
        public Object convertFromString(Map context, String[] strings, Class toClass) {     
            if (strings == null || strings.length == 0 || strings[0].trim().length() == 0) {
                return null;
            }
    
            try {
                return DATETIME_FORMAT.parse(strings[0]);
            } catch (ParseException e) {
                throw new TypeConversionException("Unable to convert given object to date: " + strings[0]);
            }
        }
    
        public String convertToString(Map context, Object date) {
            if (date != null && date instanceof Date) {         
                return DATETIME_FORMAT.format(date);
            } else {
                return null;
            }
        }
    }
    
  2. Put the conversion annotation on the property setter (or use a conversion.properties file)

    @Conversion()
    public class MyEditAction {
        // ...
    
        @TypeConversion(converter="my.app.common.converter.StringToDateTimeConverter")
        public void setUploadedDate(Date uploadedDate) {
            this.uploadedDate = uploadedDate;
        }
    

In my case I did not need to edit it but wanted to specify the format in the jsp. I used an additional hidden field to keep the original value (another alternative would have been the use of Preparable interface):

<s:textfield name="uploadedDateDisplay" value="%{getText('format.datetimesecond',{uploadedDate})}" size="70" disabled="true"/>
<s:hidden name="uploadedDate" />
like image 102
Samuel Avatar answered Oct 03 '22 03:10

Samuel