If you are using <esql/> you may need to transform a custom date string to a valid Java Date object.

Here is an example:

select * from reports where date = #<xsp-request:get-parameter name="date"/>#

The actual date of the <esql:query/> has to be in the format e.g. 01/29/2003. Using the esql parameter will result in java code that looks something like the following:

PreparedStatement statement = conn.prepareStatement("select * from reports where date = ?");
statement.setDate(1, myDate);

... but if you are a in e.g. Germany you are used to this date format: 29.01.2003.

Step 1 Create a HTML-Form save it as date.html in your cocoon-webapp dir.

<meta name="author"content="ThorstenScherler">
<p>Please press Submit.</p>
<form action="date.xml" method="GET">
<input type="text" name="date" value="29.01.2003"/>
<input type="submit" value="Submit"/>

On submit this will result in attaching /date.xml?date=29.01.2003 to the uri where the html form was called from.

Step 2 Create a file date.xsp and put this in your cocoon-webapp dir.

<xsp:page language="java" xmlns:xsp="http://apache.org/xsp">
String datePattern = "dd.MM.yyyy";
SimpleDateFormat dateFormat = new SimpleDateFormat( datePattern );
String sDate_one ="";
 catch( ParseException e )
 getLogger().error("XSP param error: ", e);
Date date_one = null;
    date_one = dateFormat.parse( sDate_one );
catch( ParseException e )
 getLogger().error("XSP date error: ", e);

Step 3 put this in your cocoon-webapp sitemap.xmap

<map:match pattern="date">
<map:read src="date.html"/>
<map:match pattern="date.xml">
<map:generate type="xsp" src="date.xsp"/>
<map:serialize type="xml"/>

Testing Now request http://localhost:8080/cocoon-webapp/date and press the Submit-Button.

If everything works fine you will see a XML-page with a valid date. If not - please check the logs.

Modifying the date-Format

String datePattern = "dd.MM.yyyy";

If you wish to modify the date-format please see SimpleDateFormat Class Reference.

Giving you an idea of how to do it:

String datePattern = "MM/dd/yyyy";

expect 01/31/2003 as the string-format.

Please let me know if you had problems with it. Scherler

  • No labels