• 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,但是顺序没有保障,所以不推荐。

  • 相关阅读:
    借用构造函数实现继承
    原型链
    创建对象 之 组合使用构造函数模式和原型模式
    6.原型对象的问题
    Spring MVC
    AOP
    谈谈对Spring IOC的理解
    Mybatis3.x与Spring4.x整合(转)
    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
    Appweb写法
  • 原文地址:https://www.cnblogs.com/wyhong/p/2388886.html
Copyright © 2020-2023  润新知