您好我在struts.xml中提到了struts验证拦截器
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<interceptors>
<!-- Interceptor to handle allowing only admins to certain actions -->
<interceptor name="adminOnly" class="adminInterceptor"/>
<!-- Interceptor to handle accessDenied exceptions thrown from service/model layer called from within actions -->
<interceptor name="accessDenied" class="accessDeniedInterceptor"/>
<!-- Copied from struts-default.xml and changed validation exclude methods -->
<interceptor-stack name="defaultStack">
<interceptor-ref name="accessDenied"/>
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">cancel,execute,delete,edit,list</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,browse,cancel</param>
</interceptor-ref>
</interceptor-stack>
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
<interceptor-stack name="adminCheck">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="adminOnly"/>
</interceptor-stack>
</interceptors>
<action name="editUser" class="userAction" method="edit">
<interceptor-ref name="adminCheck"/>
<result name="success">/WEB-INF/pages/userForm.jsp</result>
<result name="input">/WEB-INF/pages/admin/userList.jsp</result>
</action>
<action name="cancelUser" class="userAction" method="cancel">
<result name="cancel" type="redirectAction">admin/users</result>
<!-- <result name="cancel">/WEB-INF/pages/admin/userList.jsp</result> -->
</action>
<action name="saveUser" class="userAction" method="save">
<!-- <result name="cancel" type="redirectAction">admin/users</result> -->
<result name="input">/WEB-INF/pages/userForm.jsp</result>
<result name="success" type="redirectAction">admin/users</result>
</action>
然后我有一个jsp页面,我提到了
<s:form name="userForm" action="saveUser" method="post" validate="true" cssClass="well" autocomplete="off">
表单有3个提交按钮。
<div id="actions" class="form-group">
<s:submit type="button" cssClass="btn btn-primary" method="save" key="button.save" theme="simple">
<i class="icon-ok icon-white"></i>
<fmt:message key="button.save"/>
</s:submit>
<c:if test="${param.from == 'list' and not empty user.id}">
<s:submit type="button" cssClass="btn btn-danger" method="delete" key="button.delete"
onclick="return confirmMessage(msgDelConfirm)" theme="simple">
<i class="icon-trash"></i>
<fmt:message key="button.delete"/>
</s:submit>
</c:if>
<s:submit type="button" cssClass="btn btn-default" method="cancel" action="cancelUser" key="button.cancel" theme="simple">
<i class="icon-remove"></i>
<fmt:message key="button.cancel"/>
</s:submit>
</div>
</s:form>
我的动作类有以下方法:
public String execute() {
return SUCCESS;
}
public String cancel() {
if (!"list".equals(from)) {
return "home";
}
return "cancel";
}
public String save() throws Exception {
Integer originalVersion = user.getVersion();
boolean isNew = ("".equals(getRequest().getParameter("user.version")));
// only attempt to change roles if user is admin
// for other users, prepare() method will handle populating
if (getRequest().isUserInRole(Constants.ADMIN_ROLE)) {
user.getRoles().clear(); // APF-788: Removing roles from user
// doesn't work
String[] userRoles = getRequest().getParameterValues("userRoles");
for (int i = 0; userRoles != null && i < userRoles.length; i++) {
String roleName = userRoles[i];
try {
user.addRole(roleManager.getRole(roleName));
} catch (DataIntegrityViolationException e) {
return showUserExistsException(originalVersion);
}
}
点击取消按钮也会触发验证。有没有办法阻止点击取消时触发验证?我对此有点新鲜,我对此做了很多研究,但没有帮助。任何有关这方面的帮助将不胜感激。
提前致谢。
Changed code :
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<interceptors>
<!-- Interceptor to handle allowing only admins to certain actions -->
<interceptor name="adminOnly" class="adminInterceptor"/>
<!-- Interceptor to handle accessDenied exceptions thrown from service/model layer called from within actions -->
<interceptor name="accessDenied" class="accessDeniedInterceptor"/>
<!-- Copied from struts-default.xml and changed validation exclude methods -->
<interceptor-stack name="myDefaultStack">
<interceptor-ref name="accessDenied"/>
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">cancel,execute,delete,edit,list</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input, back, browse, cancel</param>
</interceptor-ref>
</interceptor-stack>
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="myDefaultStack"/>
</interceptor-stack>
<!-- <default-interceptor-ref name="myDefaultStack"/> -->
<interceptor-stack name="adminCheck">
<interceptor-ref name="myDefaultStack"/>
<interceptor-ref name="adminOnly"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myDefaultStack"/>
答案 0 :(得分:0)
我完成了配置。我在问它有什么问题。可能我的问题标题是错误的。但我打算问的是为什么它不适合我。我的代码出了什么问题。
在您的代码中,您使用method
标记上的s:submit
属性。默认情况下,此属性不再有效,因为DMI(动态方法调用)已禁用。 xml配置中有一个设置常量,您可以更改该设置以启用此功能。但是,如果您没有隐含的方法安全性,则可能会遇到其他安全问题。
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.mapper.action.prefix.enabled" value="false"/>
并使用
<s:submit type="button" cssClass="btn btn-default" method="cancel"
key="button.cancel" theme="simple">
另一种方法是将属性method
更改为action
并使用
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.mapper.action.prefix.enabled" value="true"/>
并使用
<s:submit type="button" cssClass="btn btn-default" action="cancelUser"
key="button.cancel" theme="simple">
您无法在method
标记中同时使用action
和s:submit
两个属性。如果上述两个设置均为false
,则会执行绑定到s:form
标记的操作。这就是验证发生的原因。