在struts2中集成了对dojo的支持,这里以一个比较常用的日期标签来说明它的使用:
1。把struts2的jar包放入到工程中。
2。在页面头部加入:
<script type="text/javascript">
// Dojo configuration
djConfig = {
baseRelativePath: "struts/dojo",
isDebug: false,
bindEncoding: "UTF-8",
debugAtAllCosts: true // not needed, but allows the Venkman debugger to work with the includes
};
</script>
<script type="text/javascript" src="struts/dojo/dojo.js"></script>
<script type="text/javascript" src="struts/simple/dojoRequire.js"></script>
3。在需要日期标签的地方加入如下代码:
<div dojoType="dropdowndatepicker" inputName="startDate" dateFormat="%Y年%m月%d日"
value="${startDate}" containerToggle="explode" saveFormat="rfc">
</div>
其中的属性dojoType指明widget的类型(全部小写就可以了)。inputName就是生成的input字段的name,提交时候作为表单的一个 参数。dateFormat是日期的格式。weekStartsOn是日历中显示的一周以哪一天开始,这里是以周一开始(0-6,0代表周日)。 adjustWeeks指明是否需要根据每个月的实际天数来调整日历中每月显示的天数(如果不调整,每个月除了自己的天数,还会多出一些相邻月份的天数, 总的天数固定为42天)。如果需要给日期设置一个初始值,可以添加value属性,例如value="2006-10-25"。
下面看完整的例子:
- <%@ page contentType="text/html; charset=GBK"%>
- <%@ taglib uri="/WEB-INF/struts-bsst.tld" prefix="bsst"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <html>
- <head>
- <script type="text/javascript">
- // Dojo configuration
- djConfig = {
- baseRelativePath: "struts/dojo",
- isDebug: false,
- bindEncoding: "UTF-8",
- debugAtAllCosts: true // not needed, but allows the Venkman debugger to work with the includes
- };
- </script>
- <!-- 引入dojo包 -->
- <script type="text/javascript" src="struts/dojo/dojo.js"></script>
- <script type="text/javascript" src="struts/simple/dojoRequire.js"></script>
- </head>
- <body>
- <form action="" method="post" name="form1" >
- <tr class="MultiColor2">
- <td align="right">起始时间:</td>
- <td colspan="2">
- <div dojoType="dropdowndatepicker" inputName="startDate" dateFormat="%Y年%m月%d日"
- value="${startDate}" containerToggle="explode" saveFormat="rfc"></div>
- <span class="alert"> *</span>
- </td>
- </tr>
- <tr class="MultiColor1">
- <td align="right">截至时间:</td>
- <td colspan="2">
- <div dojoType="dropdowndatepicker" inputName="endDate" dateFormat="%Y年%m月%d日"
- value="${endDate}" containerToggle="explode" saveFormat="rfc"></div>
- <span class="alert"> *</span>
- </td>
- </tr>
- </table>
- <iframe id="resultFrame" name="resultFrame" style="display:none;"></iframe>
- </body>
- </html>