Tags Struts2 <s: url> / <s: param> show values ​​of date parameters that are not executed during type conversion

I am struggling with the Struts2 date formatting issue. If I understand correctly, the conversion type in Struts2 is local, and any form fields / parameters that map to Date objects must be strings formatted in their SHORT format in the locale; the default output for the Date object in the value stack is also displayed in the form of the SHORT format in the local format (if it is not redefined using special formatting).

Although the form fields worked fine with dates, when using the <s:url> I cannot get the <s:param> to encode the date parameters correctly. When I try something like this

 <s:url action="foo" > <s:param name="endDateParam" value="#endDate"/> </s:url> 

the result is clearly not a short format:

 /foo.action?endDateParam=Sat+Jan+14+00%3A00%3A00+EST+2012 

I re-read the Struts2 documentation, but mostly discuss creating custom date formats in i18n'ized property files, which doesn't seem to be the right solution.

Any help with this issue would be greatly appreciated.

+4
source share
2 answers

You can send it like this:

 <s:param name="dateFrom"> <s:date name="dateFrom" format="dd.MM.yyyy"/> </s:param> 
+3
source

You may have already decided this by formatting the line in action as you need. I would advise this first if you are not having dinner to separate the model / presentation or if there is no comparison between actions and presentation, in which case this zeal is justified.

Suppose you thought that formatting was not a matter of action, in which case you can fully use OGNL:

Here is an example that displays the current date (it uses new to create a new date, but you can easily just replace “new java.util.Date ()” with “endDate.” It was designed so that anyone could just insert him into your 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 : access to the static OGNL method is required to be true. The easiest way to do this is to add the following to struts.xml:

 <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> 

Using OGNL at this level is a little suspicious, but it's easy to read, and the intent is clearly related to the presentation / presentation. Although it’s not so easy to build ... The easiest way is to write everything as a single java line, and then apply the ognl syntax rules, which you will 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

0
source

Source: https://habr.com/ru/post/1403171/


All Articles