Struts 中循环显示多个数组

7 篇文章 0 订阅
4 篇文章 0 订阅

提出问题:

现在需要在页面上显示一个table,每一列都对应form中的一个数组属性,现在要在页面上显示,格式如下:

 

ID

NAME

AGE

001

Name1

24

002

Name2

32

003

Name3

22

 

Form的源码如下:

import org.apache.struts.action.ActionForm;

 

public class TestAForm extends ActionForm {

    String login;

    String password;

   

    String[] id;

    String[] name;

    String[] age;

   

    //String strc;

    public String getLogin() {

       return login;

    }

    public void setLogin(String login) {

       this.login = login;

    }

    public String getPassword() {

       return password;

    }

    public void setPassword(String password) {

       this.password = password;

    }

    public String[] getAge() {

       return age;

    }

    public void setAge(String[] age) {

       this.age = age;

    }

    public String[] getId() {

       return id;

    }

    public void setId(String[] id) {

       this.id = id;

    }

    public String[] getName() {

       return name;

    }

    public void setName(String[] name) {

       this.name = name;

    }

   

 

}

 

那么在页面上如何遍历多个数组呢?思考中。。。。。。

 

解决办法:

 

显示页面,内容如下:

<%@ page language="java" pageEncoding="UTF-8"%>

 

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

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

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

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

 

 

<!DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN">

<html:html lang="true">

  <head>

    <html:base />

   

    <title>testout.jsp</title>

 

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

 

  </head>

 

  <body>

    This a struts page. <br>

    aForm.login=<bean:write name="aForm" property="login" /><br>

    aForm.password=<bean:write name="aForm" property="password" />

   

    <table align='left'>

    <th>

        <td>ID</td>

        <td>NAME</td>

        <td>AGE</td>

    </th>

    <logic:iterate id="item" name="aForm" property="id" indexId="index">

        <tr>

           <td><bean:write name="item"/></td>

           <td><bean:write name="aForm" property='<%="name[" + index + "]"%>'/></td>

           <td><bean:write name="aForm" property='<%="age[" + index + "]"%>'/></td>

        </tr>

    </logic:iterate>

    </table>

  </body>

</html:html>

 

配置文件Struts-config.xml,内容如下:

<struts-config>

  <data-sources />

  <form-beans>

      <form-bean name="aForm"

               type="com.ffs.test.form.TestAForm"/>

  </form-beans>

  <global-exceptions />

  <global-forwards />

  <action-mappings>

     <action path="/test"

             type="com.ffs.test.action.TestAction"

             name="aForm"

             scope="request"

             validate="false"

             parameter="test"

             input="/jsp/test/testin.jsp">

            <forward name ="success" path="/jsp/test/testout.jsp"/>

     </action>

  </action-mappings>

  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />

</struts-config>

 

入口页面testin.jsp,内容如下:

<%@ page language="java" pageEncoding="UTF-8"%>

 

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

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

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

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

 

<!DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN">

<html:html lang="true">

  <head>

    <html:base />

   

    <title>testin.jsp</title>

 

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

 

  </head>

 

  <body>

    <html:form action="test.do" method="post" focus="login">

      <table border="0">

        <tr>

          <td>Login:</td>

          <td><html:text property="login" /></td>

        </tr>

        <tr>

          <td>Password:</td>

          <td><html:password property="password" /></td>

        </tr>

        <tr>

          <td colspan="2" align="center"><html:submit /></td>

        </tr>

      </table>

    </html:form>

  </body>

</html:html>

 

还差一个Action文件TestAction.java,内容如下:

package com.ffs.test.action;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.actions.MappingDispatchAction;

 

import com.ffs.test.form.TestAForm;

 

public class TestAction extends MappingDispatchAction {

    public ActionForward test(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse rsp) {

       TestAForm tf = (TestAForm)form;

       tf.setLogin("A");

       tf.setPassword("B");

      

       String[] id = new String[]{"001", "002", "003", "004"};

       String[] name = new String[]{"name1", "name2", "name3", "name4"};

       String[] age = new String[]{"21", "22", "32", "24"};

      

       tf.setId(id);

       tf.setName(name);

       tf.setAge(age);

      

       //return (new ActionForward("success"));

       return (mapping.findForward("success"));   

    }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值