我正在使用带有XML的Struts 2 Validator框架。但服务器端验证无效。以下是代码段。
struts.xml中
<interceptor-stack name="MyStack">
<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\..*,^struts\..*,.*\\.*,.*\(.*,.*\).*,.*@.*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse,reset</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse,reset</param>
</interceptor-ref>
</interceptor-stack>
<action name="process" method="process" class="org.web.action.MyAction">
<interceptor-ref name="MyStack" />
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
<result name="wait">wait.jsp</result>
<result name="input">Index.jsp</result>
</action>
MyAction.java
public class MyAction extends ActionSupport {
private String amount;
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String process() throws Exception {
//some processing done here
}
}
MyAction - 处理 - 的validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator Config 1.0//EN"
"@@xwork.validator.dtd@@">
<validators>
<field name="amount">
<field-validator type="requiredstring" short-circuit="true">
<message key="order.amount.required"/>
</field-validator>
<field-validator type="amountValidator" short-circuit="true">
<message key="order.amount.invalid" />
</field-validator>
</field>
</validators>
AmountValidator.java
public class AmountValidator extends FieldValidatorSupport {
public Struts2DonationAmountValidator() {
super();
}
public void validate(Object object) throws ValidationException {
//some validation check here
}
}
validators.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator Config 1.0//EN"
"https://s3.amazonaws.com/static/xwork-validator-config-1.0.dtd">
<validators>
<validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator" />
<validator name="amountValidator" class="org.web.validator.AmountValidator"/>
</validators>
的index.jsp
<input type="text" id="amountTemp" name="amount">
我已将sysout放在验证器的第一行以及操作方法中。未调用 AmountValidator.java 中的sysout。即使在调试时,也不会调用验证,控件会跳转到action方法。控制台中没有任何异常提示。动作方法流程正常。
提前致谢!!!
答案 0 :(得分:2)
我在http://struts.apache.org/release/2.1.x/docs/validation.html找到了另一个解决方案。我将MyAction-process-validation.xml的名称更改为MyAction-processAction-validation.xml,并将我的操作命名为processAction而不是process。这意味着我使用了动作名称而不是方法。
感谢您的支持。