高级ActionFrom

 

 

 

在某些页面上要输入许多相同类型的信息,如一个人的三个电话号码,你采用什么办法?

一、采用phone数组:

Reg1.jsp

 

<%@ page language="java" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

 

<html>

    <head>

       <title>JSP for Reg1Form form</title>

    </head>

    <body>

       <html:form action="/reg1">

       phone1:<html:text property="phone"></html:text>

       phone2:<html:text property="phone"></html:text>

       phone3:<html:text property="phone"></html:text>

           <html:submit/><html:cancel/>

       </html:form>

    </body>

</html>

 

 

RegAction.java

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import prj1.form.Reg1Form;

 

 

public class Reg1Action extends Action {

      

       public ActionForward execute(ActionMapping mapping, ActionForm form,

                     HttpServletRequest request, HttpServletResponse response) {

              Reg1Form reg1Form = (Reg1Form) form;

              String[] phone=reg1Form.getPhone();

              for(int i=0;i<phone.length;i++){

                     System.out.println(phone[i]);

              }

              return new ActionForward("/reg1.jsp");

       }

}

 

 

 

 

 

提交到本来页面的时候,

 

会在文本框中加返回一些字符。如:

 

解决方案:phone1:<html:text property="phone" ></html:text>

       phone2:<html:text property="phone"></html:text>

       phone3:<html:text property="phone"></html:text>

 

                                              

 

变为;phone1:<html:text property="phone" value=”” ></html:text>

       phone2:<html:text property="phone" value=””></html:text>

       phone3:<html:text property="phone" value=””></html:text>

 将不会出现字符。

 

提交后:

 

此方法有一不好:当提交到本页以后,文本框里面的值都会消失,如何保留呢?

解决方法:索引属性-------JAVABEAN中的一种规范。

 

如下:

——————————————————————————————————————————————————--————————————————————————-----——————

Reg2.jsp

 

<%@ page language="java" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

 

<html>

    <head>

       <title>JSP for Reg1Form form</title>

    </head>

    <body>

       <html:form action="/reg2">

       phone1:<html:text property="phone[0]"></html:text><br>

       phone2:<html:text property="phone[1]"></html:text><br>

       phone3:<html:text property="phone[2]"></html:text><br>

           <html:submit/><html:cancel/>

       </html:form>

    </body>

</html>

 

Reg2Form.java

 

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

 

public class Reg2Form extends ActionForm {

    private String[] phones = new String[3];

 

    public String getPhone(int i) {

      

       return phones[i];

    }

 

    public void setPhone(int i, String phone) {

       this.phones[i] = phone;

    }

 

    public ActionErrors validate(ActionMapping mapping,

           HttpServletRequest request) {

       // TODO Auto-generated method stub

       return null;

    }

 

    public void reset(ActionMapping mapping, HttpServletRequest request) {

       // TODO Auto-generated method stub

    }

}

 

 

 

------------------------------------------------------------------------------------------------------

Reg2Action.java

 

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import prj1.form.Reg2Form;

 

 

public class Reg2Action extends Action {

                                              

                                               public ActionForward execute(ActionMapping mapping, ActionForm form,

                                                        HttpServletRequest request, HttpServletResponse response) {

                                                 Reg2Form reg2Form = (Reg2Form) form;

                                              

                                                  for(int i=0;i<3;i++){

                                                        System.out.println(reg2Form.getPhone(i));

                                                 }

                                                 return new ActionForward("/reg2.jsp");

                                               }

}

 

 

提交后得到:

 

 

此方法有个缺点:

ActionForm中必需知道表单中的变量个数。

如上例:Reg2Form.java

 

public class Reg2Form extends ActionForm {

    private String[] phones = new String[3];

 

    public String getPhone(int i) {

      

       return phones[i];

    }

 

    public void setPhone(int i, String phone) {

       this.phones[i] = phone;

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值