WebWork2与SpringFramework集成之实例篇(原创)(5-1)

5、实现表单交互

1)辅助ActionPriceIncreaseForm

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         PriceIncreaseFormxwork.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,会将表单域元素的值设置到priceIncreasepercentage属性

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.percentageVelocity不会解析,会直接显示在文本域中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值