html:cancel
html:cancel标签生成一个取消按钮。当点击该按钮后action servlet会绕过相应的form bean的validate()方法,同时将控制权交给相应的action。在该action中可以使用Action.isCancelled(HttpServletRequest)方法判断是否被取消了。如果返回true表示这个action被取消了,否则表示这个action没有被取消。
请注意,如果您修改了html:cancel标签的property属性值,那么struts提供的cancel探测机制就失效了,您自己必须提供类似的机制。
下面是可取消的action的配置文件,注意<set-property property="cancellable" value="true"/>这一行,如果不添加Struts会抛出org.apache.struts.action.InvalidCancelException异常。这是我在完成本指南的过程中发现的唯一向下不兼容的地方。
<action path="/cancel"
type="org.solo.struts.action.CancelAction" name="cancelForm"
scope="request">
<set-property property="cancellable" value="true"/>
<forward name="success" path="/cancel.jsp" />
</action>
下面是html:cancel标签的代码:
<html:cancel>取消</html:cancel>
下面是对应的action中的代码:
if(isCancelled(request)){
//action被取消时要做的事情写在这里
return mapping.findForward("cancel");
}else{
//action没有被取消时要做的事情写在这里
html:errors
html:errors标签和html:messages标签的功能相似,所以我们放到一起来介绍。
html:errors标签将由name属性指定的ActionMessages、ActionErrors、String和String[]直接输出到页面中。
html:messages标签将用由name属性(注意message属性值对它的影响)指定的ActionMessages、ActionErrors、String和String[]创建一个新的属性和scripting变量,使用id属性值作为名称。
html:errors标签和html:messages标签的property属性是用来为errors和messages分类的。我们可以给这两个标签指定property属性,以便只显示某一类的错误或消息。
在资源文件增加了如下的内容:
# -- standard errors --
errors.header=<ul>
errors.prefix=<li>
errors.suffix=</li>
errors.footer=</ul>
error=error with none value .
error1=error1 with one value is {0} .
error2=error2 with two values are {0} , {1} .
error3=error3 with three values are {0} , {1} , {2} .
error4=error4 with four values are {0} , {1} , {2} ,{3} .
下面的代码示例了actionErrors的构造:
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors actionErrors = new ActionErrors();
actionErrors.add("property1",
new ActionMessage("error"));
actionErrors.add("property2",
new ActionMessage("error1","value0"));
actionErrors.add("property2",
new ActionMessage("error2","value0","value1"));
actionErrors.add("property3",
new ActionMessage("error3","value0","value1","value2"));
actionErrors.add("property3",
new ActionMessage("error4","value0","value1","value2","value3"));
actionErrors.add("property4",
new ActionMessage("error1",new Object[]{"value0"}));
actionErrors.add("property4",
new ActionMessage("error2",new Object[]{"value0","value1"}));
actionErrors.add("property4",
new ActionMessage("error3",new Object[]{"value0","value1",
"value2"}));
actionErrors.add("property5",
new ActionMessage("error4",new Object[]{"value0","value1",
"value2","value3"}));
actionErrors.add("notBundle",
new ActionMessage("not a bundle key",false));
return actionErrors;
}
errors标签代码示例:
<html:errors/>
<br/>
<html:errors property="property4"/>
messages标签代码示例:
<logic:messagesPresent>
<ul>
<html:messages id="message">
<li><bean:write name="message"/></li>
</html:messages>
</ul>
</logic:messagesPresent>
<br/>
<logic:messagesPresent>
<ul>
<html:messages id="message" property="property4">
<li><bean:write name="message"/></li>
</html:messages>
</ul>
</logic:messagesPresent>
html:javascript
html:javascript标签生成用于校验的javascript脚本代码。
下面的代码片段示例了html:javascript标签的基本用法,其中formName属性的值是在validation.xml文件的<formset>中定义的form的名称。有一点值得注意的是在确定<formset>时要使用合适的language属性值。
<html:javascript formName="dataForm"/>
html:xhtml
html:xhtml标签在页面中一出现就是告诉该页的所有其它的Struts html标签以XHTML1.0兼容的形式输出。这和将html:html标签的xhtml属性值指定为true有些类似。如果上述情况都没有出现,Struts html标签将以html4.01兼容的形式输出。