Input Date in Struts2 - struts2

How to change the format of java.util.Date object in struts 2. What I have tested, Struts 2 accepts m/d/yy. Is it possible to change the format?
And also how to change the default message "Invalid field value for field date"?
date tag in struts2 can only be used in output.

You can pass format to s:date tag
<s:date name="myDate" format="yyyy-MM-dd" />
To change the default error message, in your validation.xml you can add message parameter
<field name="myDate">
<field-validator type="date">
<message key="errors.myDateRequired" />
</field-validator>
</field>
Then in your ApplicationResources.properties file you will provide value for errors.myDateRequired
Update: As per comment
s:date is only a read only tag which means cannot be used to accept input from user.
For enabling textField to accept date in different format, couple of approaches:
Use struts2-jQuery plugin, it has a very nice date picker which support different date timeformat, showcase, DatePickerTag.
Custom javascript code to format user input.
Use jQuery (if allowed) and use jQuery datepicker
You can use custom converter to use text field for date, example

The date format Struts accepts depends on the user's locale. So for different locales you can have different date formats. This way you won't be able to support multiple date formats for the same locale but I don't see why you would need this anyway, it's only going to confuse your users. The only way to support different date formats for the same locale without javascript would be with a custom date converter as #mprabhat notes.

You could see the official doc about this:
Define the format for every language y the resource bundle. For instance, an appropriate en_US format definition extension could look like this:
format.date = {0,date,MM/dd/yy}
In parallel, you could add the following to your de_DE bundle:
format.date = {0,date,dd.MM.yyyy}
http://struts.apache.org/2.1.6/docs/formatting-dates-and-numbers.html

Related

PrimeFaces calendar change pattern after date selection

I use PrimeFaces calendar component on page. Pattern attribute is set to MM/dd/yyyy HH:mm:ss. Component value attribute is linked to java.util.Date variable in bean. When i open the page date in format which i expect, but after change of the value, format will always change into different format.
Expected format - 06/15/2015 08:00:30
Format after change of value - 06/15/2015 8:00 AM
Component ussage.
<p:calendar value="#{someBean.someJava.UtilDateVariable}" pattern="MM/dd/yyyy HH:mm:ss" locale="en">
<f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss"/>
</p:calendar>
I already tried to check primefaces showcase and documentation, different locale specification and also usage of the localization script for current localization but with no effect.
Every suggestion is welcome.
You do not need <f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss"/>. This tag converts String to Object and viceversa.
The Primefaces documentation only uses the Pattern attribute and sometimes the locale attribute causes troubles.
Try:
<p:calendar value="#{someBean.someJava.UtilDateVariable}" pattern="MM/dd/yyyy HH:mm:ss"/>
I know this is old, but I had simalar problem, calendar showing right date, but when I opened the datepicker the pattern seemed to be wrong.
After three hours I ended it was bootstrap pattern.
To solve quickly I included a javascript file on the jsf page with setting for pattern:
In XHTML
<h:outputScript library="js" name="myscripts.js" />
IN JS
$(document).ready(function() {
$.fn.datepicker.defaults.format = "dd/mm/yyyy";
});

Struts2 : internationalize s:date?

<s:date name="mydate" format="dd/MM/yyyy HH:mm" /> works perfectly for fomatting my date to the french standard.
But It's hard-written in my jsp. So I have to build if clause to switch according to the locale.
Is there a way to put this format to a general property file?
I tried:
format.date = {0,date,dd/MM/yyyy}
format.time = {0,time,HH:mm}
put in my global .properties but it's not taken into account when, I try just a <s:date name="mydate"> or a <s:property value="mydate"/>.
Create localized date format inside your properties files, e.g. with struts.date.format key:
struts.date.format = dd.MM.yyyy
And use getText method to get this date format in <s:date> tag format attribute:
<s:date name="date" format="%{getText('struts.datetime.format')}"/>
Absolutely. Internationalization (i18n) is handled out-of-the-box in almost every framework out there, then it's better to spend time on understand how to make it works than to write some unneded, buggy custom solution.
With Struts2, you must ensure to have:
I18nInterceptor in your Interceptor Stack;
I18nInterceptor defined in struts.xml: <constant name="struts.custom.i18n.resources" value="global" />;
the localized, correctly named global.properties file for each locale you want to handle;
the request_locale` parameter (if you have not changed the language in your browser) when calling your Actions, giving the I18nInterceptor the ability to hijack the request to the correct resource;
an appropriate character encoding on your pages: <%# page contentType=”text/html;charset=UTF-8″ %>.
Take a look at the guides available on the web too:
http://www.mkyong.com/struts2/struts-2-i18n-or-localization-example/
http://www.2bloggers.com/2011/12/localization-i18n-in-struts2.html
http://www.roseindia.net/struts/struts/struts2.2.1/tags/i18ntag.html
http://www.roseindia.net/struts/strutsinternationalization.shtml
http://www.roseindia.net/tutorials/I18N/internationalison-code.shtml

Setting a dateformat in struts2 tag using application aware

The requirement is that the admin will select the date format from the list say
("dd-MM-yyyy","d MMM yyyy") based on the this selection all the date to be displayed should be as per the selection.
as this setting should apply throught out the application i tried saving it in a application scope using application aware . i was able to save but the problem was when trying to implement the changes
means
GlobelSetting global = new GlobelSetting();
global.setFormatDate("dd-MM-yyyy");
applicationMap.put("dateFormat", global.getFormatDate());
now trying to set the date format in date tag
<td align="left">
<s:date name="employee.dateCreated" format="#application.dateFormat"/></td>
but i get a error while displaying the error is
java.lang.IllegalArgumentException: Illegal pattern character 'p'
The "format" attribute needs to have evaluation forced:
<s:date name="employee.dateCreated" format="%{#application.dateFormat}" />

Struts2 <s:url> / <s:param> tags outputting date parameter values that fail in type conversion

I'm struggling with a Struts2 date formatting issue. If I understand correctly, type conversion in Struts2 is locale aware, and any form fields/parameters that map to Date objects should be strings formatted in their Locale specific SHORT format; the default output for a Date object on the value stack is also output as the Locale specific SHORT format (unless overridden with custom formatting).
Although form fields have worked fine with dates, when using the <s:url> tag I can't seem to get the <s:param> tag to encode date parameters correctly. When I try something such as this
<s:url action="foo" >
<s:param name="endDateParam" value="#endDate"/>
</s:url>
the result is pretty obviously not the SHORT format:
/foo.action?endDateParam=Sat+Jan+14+00%3A00%3A00+EST+2012
I re-read the Struts2 documentation but they mostly discuss creating custom date formats in the i18n'ized properties files, which doesn't seem like the right solution.
Any help with this problem would be greatly appreciated.
You can send it like that :
<s:param name="dateFrom">
<s:date name="dateFrom" format="dd.MM.yyyy"/>
</s:param>
You've probably already solved this by formatting a string in the action the way you need it. This I would advise first, unless you are supper stickler for model/view separation or there isn't a one to one mapping between the action and the view in which case this zealousness my be justified.
Lets say you felt the formatting wasn't the business of the action in that case you could use OGNL to it's full effect:
Here is an example that displays the current date (it uses new to construct a new date but you can very easily just replace "new java.util.Date()" with "endDate". It was constructed this way so anyone can just paste it into their JSP without any action dependencies.
<p>
<s:property value="#java.text.DateFormat#getDateInstance(#java.text.DateFormat#SHORT, #java.util.Locale#CANADA).format(new java.util.Date())"/>
</p>
NOTE: requires OGNL static method access to be true. Easiest way to do that is to add the following to struts.xml:
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
Using OGNL to this level is a bit suspicious but it is easy to read and the intention is clearly view/presentation related. Although it isn't that easy to construct... The easiest way is to write everything as one line of java and then apply ognl syntax rules which you would find here:
http://commons.apache.org/ognl/language-guide.html
Also for quick reference:
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html

grails g;datepicker how to handle partial dates

I have the following datepicker in a form
<g:datePicker name="born" default="none" precision="day"
noSelection="['':' ']" relativeYears="[-25..2]"
value="${animalInstance?.born}" />
Since I allow a no selection in the datepicker it's possible to enter a date with no days, no months but a valid year. It's exactly the intended behavior.
But the params come in the following format
born=""
born_year="1995"
Controller and Command object do not bind this correctly and the whole date is considered null.
Is there a proper way to handle this without doing some If...Then...Else code?

Resources