JavaEE ActionForm的高级应用

scope="request"每次提交都生成一个新的actionform对象,而 scope="session"时,再一次绘画中生成一个actionform对象,保存在session中,获取时从session中获取。 scope决定actionform的生命周期。

大多数情况用request。也有特殊情况用session。如表单跨页Prj8_1

Forward类型的action只负责跳转打包不做其他事情

  <action name="reg1Form" forward="/P2.jsp" path="/toP2" scope="session">
      <set-property property="cancellable" value="true" />
    </action>
   
    <action name="reg1Form" path="/reg1" type="prj8_1.action.Reg1Action" scope="session">
      <set-property property="cancellable" value="true" />
    </action>

注意两个scope都是session,否则前两个的值保留不住。

验证时用隐藏表单域的方法验证。

public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  // TODO Auto-generated method stub
  
  if (page.equals("1")) {
   if (account.length() == 0) {
    
   }
   if (password.length() == 0) {
    
   }
  }
  if (page.equals("2")) {
   if (phone.length() == 0) {
    
   }
   if (address.length() == 0) {
    
   }
  }
  return null;
 }

其中page属性就是被隐藏的属性在表单中用<html:hidden property="page" value=""/>用value的值来区别开来。

//

比如有的页面有三个电话提交时我们可以将他们分别定义成三个属性,但这样造成ActionForm属性太多;我们也可以将其定义为一个数组属性,即String[] phones;这样跳转回来的话有个问题就是三个文本框里会出现[Ljava.lang.String;@17e4dee这是由于它将整个数组返回来了,解决的办法就是<html:text property="phones" value=""/>使其变为空,但很难让他们各填各值。另外还可以利用索引属性,实现如下

ActionForm中的属性是随着i而变化的

String[] phones = new String[3];
 
 public String getPhones(int i) {
  return phones[i];
 }

 public void setPhones(int i, String phone) {
  this.phones[i] = phone;
 }

JSP

 <html:form action="/reg3">
   phone1:<html:text property="phones[0]"></html:text><br>
   phone2:<html:text property="phones[1]"></html:text><br>
   phone3:<html:text property="phones[2]"></html:text><br>
   <html:submit/><html:cancel/>
  </html:form>

Action

Reg3Form reg3Form = (Reg3Form) form  
  for (int i = 0; i < 3; i++) {
   System.out.println(reg3Form.getPhones(i));
  }
  return new ActionForward("/reg3.jsp");

第三种方法可以使text中的文本保持住。

但是我们可以看到上面这种方法也有一种缺陷,就是你必须知道个数,但能够接受。可以用arraylist,但是顺序没有保障,所以不推荐。

转载于:https://www.cnblogs.com/wyhong/archive/2012/03/10/2388886.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值