将当前日期放在输入值(jsp)中

时间:2018-04-18 17:08:23

标签: html jsp

我正在尝试在输入字段中添加当前日期,以便用户不必总是输入它。

我的jsp如下所示:

enter image description here

但我希望它看起来如下:

enter image description here

我有以下代码,但它没有显示当前日期:

<tr>
    <td align="left" width="19%">Start Date :</td>
    <td width="33%" align="left">
        <input type="text" name="bean.dateProperty" value="${fmtDate}"/>
        <fmt:formatDate var="fmtDate" value="${form.bean.dateProperty}" pattern="dd/MM/yyyy"/>                      
    </td>
    <td width="15%">End Date :</td>
    <td width="33%">
        <input type="text" value="" id="picker1" name="picker1" alt="Pick a date"/>
    </td>
</tr>

2 个答案:

答案 0 :(得分:1)

您需要首先声明变量,然后使用它,因此您的jsp代码需要更改如下:

<fmt:formatDate var="fmtDate" value="${form.bean.dateProperty}" pattern="dd/MM/yyyy HH:mm:ss"/>  
<input type="text" name="bean.dateProperty" value="${fmtDate}" placeholder="${fmtDate}"/>

还需要确保值form.bean.dateProperty不为空,如果它为空,则可以使用下面的代码段来声明:

<fmt:formatDate var="fmtDate" value="<%=new java.util.Date()%>" pattern="dd/MM/yyyy HH:mm:ss"/>  

答案 1 :(得分:0)

This probably isn't the cleanest way to do this, but you can create the date as shown in https://www.tutorialspoint.com/jsp/jsp_handling_date.htm, you have alot of options in how you format it. Then all you need to do is put that value as the value for the placeholder attribute of your input element. So it could probably look something like:

<input type="text" placeholder="<%
     Date dNow = new Date( );
     SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
     out.print(ft.format(dNow));
  %>"/>