oblac / jodd

Jodd! Lightweight. Java. Zero dependencies. Use what you like.
https://jodd.org
BSD 2-Clause "Simplified" License
4.07k stars 722 forks source link

Question: how to populate string value to date type from JSP #132

Closed shawnye closed 9 years ago

shawnye commented 10 years ago

I have a pojo:with property: @DbColumn(sqlType=TimestampSqlType.class) private Date startTime;

then I want to populate value from jsp:(entity is the output parameter) <input type="text" name="entity.startTime" id="startTime" />

when submit the form , the property 'startTime' can not get the value from JSP I know mismatch type.between JSP and property cause that.error

I want to know where can I register sth like TypeConverter to populate without further intervention. thank you.

igr commented 10 years ago

First you need to have getter and setter for the property, to be able to display pojo value in JSP. For type conversion, you can register custom type converter in TypeConverterManager, but I think there is already one for your type.

Let me know if this helps and what conversion do you need :)

shawnye commented 10 years ago

When I deeply debug it, it works. but another question comes out:

I input 2014-7-11 in JSP, after I submit , the input echo Fri Jul 11 00:00:00 CST 2014, where can I format the echo value?

I've debuged jodd.typeconverter.impl.StringConverter and found no value.getClass()==Date.class, it seems that the 'input echo' does not use StringConverter to populate entity properties BACK to JSP input ?

Here for startTime : jsp ==> property Converter: DateConverter, property ==> jsp Converter: which one ?

Thanks .

igr commented 10 years ago

There is not special thing for formatting values in JSP, simply because there are too many ways how this can be done in front end, especially for the time. My approach is to create a util method and make either a JSP function or use it in scriptlet.

So for printing values in the JSP you have to make your own thing :) JSP is not using Jodd and therefore, type converters are not directly used. Thats why conversion from the JSP page to property works as it goes through Jodd (via Madvoc). But for displaying, you simply have to provide your own way.

Another ways it to add a getter in your bean that returns formatted string from your property, for example getTimeString, where you could use conversion.

I will take the opportunity to explain why Jodd is not providing much solutions for the front end. It's simply because there is such a variety of ways how to do front end, and we do not want to force any :) Jodd (Madvoc) allows you to easily send data to your presentation layer, and that is all. If you remember, we had a lot of utility JSP functions before; but at one moment we decided to remove them all. Even for the utilities, there was lot of ways how they could be called, and JSP functions can not be overloaded, and all that was just a mess. So instead, we just say: please, implement frontend utilities by yourself, especially when it comes to displaying and formatting data.

igr commented 10 years ago

I will close this question. As said, formatting properties on JSP is out of scope for Jodd.

shawnye commented 10 years ago

I debug into jodd.servlet.tag.FormTag for 'startTime' input

at line 133: Object valueObject = resolver.value(name);

First time,valueObject inspected like this (should be 2014-7-10):

    cdate   Gregorian$Date  (id=356)    
        cachedFixedDateJan1 735234  
        cachedFixedDateNextJan1 735599  
        cachedYear  2014    
        daylightSaving  0   
        dayOfMonth  10  
        dayOfWeek   5   
        era null    
        forceStandardTime   false   
        fraction    0   
                                                                            fastTime    1404921600000   

at line 137: String value = valueObject.toString(); value ="2014-7-10" ------------------------After submit the form, save and back to the JSP--------------------- valueObject inspected like this (should be 2014-7-11):

cdate   Gregorian$Date  (id=393)    
    cachedFixedDateJan1 735234  
    cachedFixedDateNextJan1 735599  
    cachedYear  2014    
    daylightSaving  0   
    dayOfMonth  11  
    dayOfWeek   6   
    era null    
    forceStandardTime   false   
    fraction    0   
    hours   0   
    leapYear    false   
    locale  null    
    millis  0   
    minutes 0   
    month   7   
    normalized  true    
    seconds 0   
    year    2014    
    zoneinfo    ZoneInfo  (id=212)  
    zoneOffset  28800000

fastTime    1405008000000   

String value = valueObject.toString(); value ="Fri Jul 11 00:00:00 CST 2014"

at this time, it causes the date format problem, I do not know what makes it different.

I've tried <input type="text" name="entity.startTime" id="endTime" value='<fmt:formatDate value="${entity.startTime }" pattern="yyyy-MM-dd"/>'/> It's of no veil.

So I have to pollute my Action or Entity code with 'startTimeString' property. and set the right date format aftersave, am I right ?

shawnye commented 10 years ago

Maybe jodd 3.6 will be different

igr commented 10 years ago

Ok I think I got it now, what you need is the use of j:form tag where you can also apply the format :))). This is an example of the usage:

<input type="text" name="entity.startTime" id="endTime" data-format="yyyy-MM-dd"/>

(where we have an additional attribute for specifying the format) or

<input type="text" name="entity.startTime" id="endTime" value="format:yyyy-MM-dd"/>

(where we define the format in the value attribute, as it is going to be replaced with real value).

What I do with time is the following: i always store time as milliseconds and keep time in form as hidden form field. Then I have another form field, usually a date/time picker that is bind to this hidden field. Therefore, all formatting is done on client-side, in javascript, and all my actions/entities are clean ;)

Yes, for now we don't have this, but let me think about of adding this feature to form tag.

Btw, please be aware that Actions (but not entities) are going to be polluted for the purpose of presentation view ;) It's simply something we have to accept for the sake of simplicity :) Sure, we have to keep this minimal as possible.

shawnye commented 10 years ago

Yeah , that's a great feature , Html5 support dataset attribute. Using 'data-format' attribute may be more acceptable, I vote it :+1:

PS: when populate form to entity, jodd use built-in DateConverter, can DateConverter get the date format here ? Of couse ,now DateConverter can recognize a lot of date format , there's not need to worry about for general use.

shawnye commented 10 years ago

I resolve it occasionally : just _reload_ the entity after save, in that case, the page shows '2014-7-11' that what I need

igr commented 10 years ago

So we can close this, right?

shawnye commented 10 years ago

Maybe not , it is always not Ok on different servers.

Even though it is OK,, it can not display custom format like '2014-7-11 15:54' , so my suggestion is you could implement it later at your convenience.

Thank you!

igr commented 9 years ago

Closing as backlog