默认显示当前日期

时间:2022-11-12 18:07:32

有个需求是检索条件中的日期默认显示当前日期,这个实现方式有两种:
(1)在前台通过js实现
(2)在后台通过java代码实现,然后在前台通过取值就可以默认显示
我是通过第二种的方式实现的这种效果:
后台代码我写在action里面,实现如下:

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String nowDate = sdf.format(date);
servletRequest.setAttribute("shfsStartDate", nowDate);
servletRequest.setAttribute("shfsEndDate", nowDate);

然后通过在前台取值就可以得到,取值的方式有很多种,比如EL表达式,struts tags等,我试了一下此处我使用EL表达式取不到值,所以采用了struts tags,最后取到了值,代码实现如下:

<th ><font color="red">*</font>伤害发生日期</th>
<td>
<input id="shfsStartDate" name="shfsStartDate" size="15" value='<s:property value="#request.shfsStartDate" />'
style='width: 70px;' onClick="WdatePicker()">


<input id="shfsEndDate" name="shfsEndDate" size="15" value='<s:property value="#request.shfsEndDate" />'
style='width: 70px;' onClick="WdatePicker()">

</td>