struts判断标签
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 > ??
<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 > ??
<html:cancel>取消</html:cancel>
?下面是对应的action中的代码:
- if (isCancelled(request)){??? ??
- ?? //action被取消时要做的事情写在这里 ??
- ?? return ?mapping.findForward( "cancel" ); ??
- } else { ??
- ?? //action没有被取消时要做的事情写在这里 ??
- ?? return ?mapping.findForward( "success" ); ??
- }??
if(isCancelled(request)){ //action被取消时要做的事情写在这里 return mapping.findForward("cancel"); }else{ //action没有被取消时要做的事情写在这里 return mapping.findForward("success"); }