datejspstruts2textfieldstruts-tags

How to set a date generated from scriplet as default value in <s:textfield>?


I have this scriplet in my JSP, and I want it to be somehow include as a default value in my <s:textfield>.

<%java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy/MM/dd"); %> 

How can I access variable df? I'm thinking like this:

<s:textfield name="mediaBean.acquireDate" style="width:150px;" 
        theme="simple" id="datepicker" type="text" 
        value="%{df.format(new java.util.Date())}" />

I tried to put it directly as value="%{new java.util.Date()} and it worked, but without format. I want to have the yyyy/MM/dd format.


Solution

  • Do not use scriplets, use tag libs. What you want can be done with Struts2 tags <s:date> and <s:param>.

    <s:textfield name="mediaBean.acquireDate" style="width:150px;" theme="simple" id="datepicker" type="text">
      <s:param name="value">
        <s:date name="new java.util.Date()" format="yyyy/MM/dd"/>
      </s:param>
    </s:textfield>