(4)表单处理Action:PriceIncreaseFormAction
package web;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import bus.PriceIncrease;
import bus.ProductManager;
import com.opensymphony.xwork.ActionSupport;
public class PriceIncreaseFormAction extends ActionSupport {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private ProductManager prodMan;
private PriceIncrease priceIncrease;
public String execute() throws Exception {
int increase = getPriceIncrease().getPercentage();
logger.info("Increasing prices by " + increase + "%.");
prodMan.increasePrice(increase);
return SUCCESS;
}
public ProductManager getProdMan() {
return prodMan;
}
public void setProdMan(ProductManager prodMan) {
this.prodMan = prodMan;
}
public PriceIncrease getPriceIncrease() {
return priceIncrease;
}
public void setPriceIncrease(PriceIncrease priceIncrease) {
this.priceIncrease = priceIncrease;
}
}
l PriceIncreaseFormAction实现PriceIncreaseFormController相同的功能,但是要简一些:
Ø 不需要去收集结果视图需要的数据,这由SpringappAction实现(参看PriceIncreaseFormAction的配置)
Ø 数据回显功能是在页面上实现的
l PriceIncreaseFormAction在xwork.xml中配置
<action name="priceIncrease"
class="web.PriceIncreaseFormAction">
<external-ref name="prodMan">prodMan</external-ref>
<result name="input" type="velocity">
<param name="location">/WEB-INF/template/priceincrease.vm</param>
</result>
<!--
<result name="input" type="dispatcher">
<param name="location">/WEB-INF/jsp/priceincrease.jsp</param>
</result>
-->
<result name="success" type="chain">
<param name="actionName">springapp</param>
</result>
<interceptor-ref name="externalValidationWorkflowStack" />
</action>
l <result name="input">表示如果表单域元素数据有效性验证失败,重定向的URL
l chain类型的<result>表示处理成功后重定向到指定的Action,由参数actionName指定该Action在xwork.xml中定义的Action引用名
l <interceptor-ref>增加用于数据有效性验证的Interceptor引用,以便在执行Action之前进行数据有效性验证,如果验证失败,不执行Action,重定向到<result name="input">指定的URL
l externalValidationWorkflowStack已经在前面的<interceptors>中定义,包含了用于外部引用的reference-resolver和用于数据有效性验证的validationWorkflowStack(WebWork2定义的缺省验证Interceptor堆栈)
<interceptors>
<interceptor name="reference-resolver"
class="com.opensymphony.xwork.interceptor.ExternalReferencesInterceptor"/>
<interceptor-stack name="externalStack">
<interceptor-ref name="reference-resolver"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
<interceptor-stack name="externalValidationWorkflowStack">
<interceptor-ref name="reference-resolver"/>
<interceptor-ref name="validationWorkflowStack" />
</interceptor-stack>
</interceptors>
(5)数据有效性验证
l WebWork2基于XWork,采用XWork的数据有效性验证机制
l XWork的验证框架允许在执行Action之前,应用输入验证规则到Action
l 注册验证器:ww2/WEB-INF/classes/validators.xml
<validators>
<validator name="required" class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/>
<validator name="requiredstring" class="com.opensymphony.webwork.validators.JavaScriptRequiredStringValidator"/>
<validator name="int" class="com.opensymphony.webwork.validators.JavaScriptIntRangeFieldValidator"/>
<validator name="date" class="com.opensymphony.webwork.validators.JavaScriptDateRangeFieldValidator"/>
<validator name="expression" class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/>
<validator name="fieldexpression" class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/>
<validator name="email" class="com.opensymphony.webwork.validators.JavaScriptEmailValidator"/>
<validator name="url" class="com.opensymphony.webwork.validators.JavaScriptURLValidator"/>
<validator name="visitor" class="com.opensymphony.webwork.validators.JavaScriptVisitorFieldValidator"/>
<validator name="conversion" class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>
<validator name="stringlength" class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/>
</validators>
Ø 上面是XWork和WebWork2提供的一些常用验证器,可以直接使用
Ø <validator>的name属性指定验证器的引用名,在验证规则文件中使用;class属性指定验证器类的全路径
Ø 如果上面的验证器都不适合,可以扩展XWork的验证框架,自定义验证器使用,同样也在validators.xml中注册
l 激活验证:
Ø 对于需要数据有效性验证的Action,都需要在配置中添加对ValidationInterceptor的引用,
Ø 缺省的validationWorkflowStack已经包含了ValidationInterceptor的引用,通常更多的是添加validationWorkflowStack引用(如前面的Action配置所述)
l 指定验证规则(与Action类同目录):PriceIncreaseFormAction-validation.xml
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
<field name="priceIncrease.percentage">
<field-validator type="required" short-circuit="true">
<message key="error.not-specified">Value required.</message>
</field-validator>
<field-validator type="fieldexpression" short-circuit="true">
<param name="expression">priceIncrease.percentage > 0</param>
<message key="error.too-low">Value too low.</message>
</field-validator>
<field-validator type="int" short-circuit="true">
<param name="max">50</param>
<message key="error.too-high">Value too high.</message>
</field-validator>
</field>
</validators>
l 数据验证规则定义在xml文件中,文件名是actionName-validation.xml,其中actionName是对应的Action类名或者是其父类名,命名规则同资源束类似
l <field>定义要验证的表单域;name属性指定表单域名字
l <field-validator>指定要进行的验证;type属性指定验证器的引用 ,与validators.xml中指定的验证器引用一致,以便使用对应的验证器进行验证;short-circuit属性可选,表示如果验证失败,是否继续下面的验证
l 同一个表单域可以定义多个验证器进行验证,不同的验证器有不同的参数,如“fieldexpression”验证器有expression参数,表示要验证的表达式,而“int”验证器有max参数,表示最大数值
l <field-validator>至少要有一个<message>元素,提供验证失败的提示信息;<message>的key属性可选,用来指定提示信息为资源束中的消息,如果没有找到,使用<message>体内容提供的缺省消息
error.not-specified=Percentage not specified!!!
error.too-low=You have to specify a percentage higher than ${expression}%!
error.too-high=Don't be greedy - you can't raise prices by more than ${max}%!
l 在资源束中的消息可以使用${paramname}的形式使用验证器的参数,但是没法使用其它参数,这一点没有Spring MVC Framework灵活
l 另外,由于WebWork2是先设置属性值,后进行验证,而对于int型属性,如果为null,又自动转换为0,因此对于第一个验证没有太大意义(有什么更好的方法?要么自定义验证器)