5、实现表单交互
(1)辅助Action:PriceIncreaseForm
package web; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import bus.PriceIncrease; import com.opensymphony.xwork.ActionSupport; public class PriceIncreaseForm extends ActionSupport { /** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass()); private PriceIncrease priceIncrease; public String execute() throws Exception { priceIncrease = new PriceIncrease(); priceIncrease.setPercentage(20); logger.info("Show PriceIncrease Form."); return SUCCESS; } public PriceIncrease getPriceIncrease() { return priceIncrease; } public void setPriceIncrease(PriceIncrease priceIncrease) { this.priceIncrease = priceIncrease; } }
l WebWork2不提供类似Spring MVC Framework 的SimpleFormController机制,无法自动定向到表单视图,所以使用辅助的PriceIncreaseForm类进行重定向(谁有更好的方法?)
l 这里还是使用PriceIncrease对象封装表单域元素,在execute()中进行了初始化
l PriceIncreaseForm在xwork.xml中的配置如下:
<action name="priceIncreaseForm" class="web.PriceIncreaseForm"> <result name="success" type="dispatcher"> <param name="location">/WEB-INF/jsp/priceincrease.jsp</param> </result> </action>
(2)表单视图页面:priceincrease.jsp
<%@ taglib uri="webwork" prefix="ww" %> <html> <head><title><ww:text name="'title'"/></title></head> <body> <h1><ww:text name="'priceincrease.heading'"/></h1> <form action="priceIncrease.action" method="post"> <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5"> <tr> <td alignment="right" width="20%">Increase (%):</td> <td width="20%"> <input type="text" name="priceIncrease.percentage" value ="<ww:property value="priceIncrease.percentage" />"> </td> <td width="60%"> <ww:iterator value="fieldErrors.get('priceIncrease.percentage')"> <font color="red"><ww:property /></font> </ww:iterator> </td> </tr> </table> <br> <ww:if test="fieldErrors.size() > 0" > <b>Please fix all errors!</b> </ww:if> <br><br> <input type="submit" alignment="center" value="Execute"> </form> <a href="springapp.action">Home</a> </body> </html>
l 在<form>标记中使用action属性指定处理表单数据的Action类的URL模式
l 表单域元素的name属性要和Action类中的属性名一致,以便能设置表单域元素的值到对应的属性
l 这里比较特殊:priceIncrease是一个PriceIncrease对象,又是Action类中的属性,通过设置表单域元素的name属性为priceIncrease.percentage,会将表单域元素的值设置到priceIncrease的percentage属性
l 推荐:用数据对象封装表单域元素的数据,这样只要在Action类中提供该对象的引用就可以了;然后使用ObjectName.PropertyName的形式指定表单域元素的name属性
l 输入值的回显使用<ww:property>标记
l fieldErrors是一个内建对象,表示表单域元素验证无效的错误信息集合,通过get()方法可以获得具体表单域元素相关的所有验证错误信息的集合
(3)使用Velocity实现表单视图:priceincrease.vm
<html> <head><title>$action.getText('title')</title></head> <body> <h1>$action.getText('priceincrease.heading')</h1> <form action="priceIncrease.action" method="post"> <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5"> <tr> <td alignment="right" width="20%">Increase (%):</td> <td width="20%"> <input type="text" name="priceIncrease.percentage" value ="$!priceIncrease.percentage"> </td> <td width="60%"> #foreach( $percentageError in $fieldErrors.get('priceIncrease.percentage') ) <font color="red">$percentageError</font> #end </td> </tr> </table> <br> #if( $fieldErrors.size() > 0 ) <b>Please fix all errors!</b> #end <br><br> <input type="submit" alignment="center" value="Execute"> </form> <a href="springapp.action">Home</a> </body> </html>
l 在Velocity模版文件中变量要以$开头,所以fieldErrors前面要加$
l $!priceIncrease.percentage比较特殊,主要是消除Velocity解析时的异议,如果使用$priceIncrease.percentage,Velocity不会解析,会直接显示在文本域中 
|