<logic:iterate>中用hashmap和用List,数组是没有区别的,用法一样,写法也一样

解决方案 »

  1.   

    <logic:iterate id="element" name="monthNames"
    indexId="idx_int">
        <td align="right">
            <bean:write name="element"/>
            <li><em><bean:write
    name="element"/></em>&nbsp;[<bean:write
    name="idx_int"/>]</li>
            <html:multibox property="monthList">
                Jan
            </html:multibox>
        </td>
    </logic:iterate>
      

  2.   

    不好意思,丢了一部分:
    <jsp:useBean id="idx" scope="page"
    class="com.anshin.MonthForm"/>
    <bean:define id="idx_int" name="idx"
    property="index"/>
      

  3.   

    谢谢,上面写的我不懂?程序我改了一下,把这一个帖子的四条信息构造一个类AllData,在构造方法中将这四个字符串初始化,然后再建立一个类NoteDataMap,定义一个HashMap,在构造函数中将一个整型变量和一个AllData对象存入HashMap中,Forum.jsp中表单提交到AppearAction,在此Action中New一个AllData对象和NoteDataMap对象,然后怎样将这个NoteDataMap对象中的数据用Logic:iterate输出:还有用户从注册表单注册成功后,到另一个表单中发帖子,他的用户名我如何从那个Action中读到这个AppearAction中?程序如下:
    AllData.class:
    public class AllData {
      private String name;
      private String caption;//标题
      private String time;
      private String content;//内容
      public AllData(){}
    public AllData(String caption,String name,String time,String content){
      this.name = name;
        this.caption = caption;
        this.time = time;
        this.content = content;
      }
      public String getCaption(){
        return this.caption;
      }
      public void setCaption(String caption){
        this.caption = caption;
      }
      public String getName(){
        return this.name;
      }
      public void setName(String name){
        this.name = name;
      }
      public String getTime(){
        return this.time;
      }
      public void setTime(String time){
        this.time = time;
      }
      public String getContent(){
        return this.content;
      }
      public void setContent(String content){
        this.content = content;
      }
    }
    NoteDataMap.java:
    import java.util.HashMap;
    import com.dareway.test.AllData;
    public class NoteDataMap {
      public HashMap note = new HashMap();
      public NoteDataMap(){}
      public NoteDataMap(int noteNumber,AllData info){
        Integer r = new Integer(noteNumber);
        note.put(r,info);
      }
      public HashMap getAllNote(){
        return note;
      }
    }
    AppearAction.java:
    import javax.servlet.http.*;
    import org.apache.struts.action.*;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.validator.DynaValidatorForm;
    import java.util.HashMap;
    import java.util.Date;
    import com.dareway.test.UserDataMap;
    import com.dareway.test.NoteDataMap;
    import com.dareway.test.AllData;public class AppearAction extends Action {
      public ActionForward perform(ActionMapping actionMapping,
                                   ActionForm actionForm, HttpServletRequest httpServletRequest,
                                   HttpServletResponse httpServletResponse) {
        String forwardPath = null;
        int num = 0;
        String name = null;//这个用户的名字该怎么从另外一Action中读出???
        ActionErrors errors = new ActionErrors();
        DynaValidatorForm mForm = (DynaValidatorForm) actionForm;
         String caption = (String) mForm.get("caption");
         String content = (String) mForm.get("content");
         String dateString = new Date().toString();
         if ( caption != null) {
           if( content != null) {
              num++;
              AllData captionInfo = new AllData(caption,name,dateString,content);
              NoteDataMap noteInfo = new NoteDataMap(num,captionInfo);
              httpServletRequest.setAttribute("noteInfo", noteInfo);
              forwardPath = "forum";
           } else {
             errors.add("content",new ActionError("error.content.error"));
           }
         } else {
           errors.add("caption",new ActionError("error.caption.error"));
         }
           saveErrors(httpServletRequest,errors);
         if(forwardPath == null){
           return new ActionForward(actionMapping.getInput());
         } else {
           return actionMapping.findForward("forum");
         }  }
    }frum.jsp:
    <%@ page contentType="text/html; charset=GBK" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head>
    <body>
    <div align="left"><font color="#0033FF"></font><font size="7" face="华文行楷"><font color="#00FF00"></font></font>
      <table width="75%">
        <tr>
          <td width="51%" height="27"></td>
          <td width="49%" rowspan="2"><font size="7" face="华文行楷"><font color="#00FF00">灌水论坛</font></font>
          </td>
        </tr>
        <tr>
          <td><font color="#006666"><em><font color="#0033FF">用户您好,您现已登陆!</font></em></font><font color="#0033FF"><em></em></font></td>
        </tr>
      </table>
    </div>
    <div align="center"> </div>
    <hr>
    <p>当前有条记录</p>
    <table width="75%" align="center" border="1">
      <tr>
        <td width="10%">条数</td>
        <td width="50%">主题</td>
        <td width="20%">作者</td>
        <td width="20%">时间</td>
      </tr>
    <logic:iterate id="item" name="noteInfo" property="allNote" scope="request">
    <tr>//这里怎么写,怎么处理一个HashMap中Value的多个属性,将它们都读出来
      <td><bean:write name="item" property="key" ignore="true"/></td>
      <td><bean:write name="item" property="value" ignore="true"/></td>
      <td>???????</td>
      <td>??????</td>
    </tr>
    </logic:iterate>
    </table>
    <hr><html:form  method="post" action="/appearAction" onsubmit="return check(this);">
      <div align="center">
        <div align="left"></div>
        <p align="left"><font color="#993399"><em>我要发帖:   </em></font></p>
        <table width="62%">
          <tr>
            <td width="13%"><strong><font color="#FF33FF">主题:</font><font color="#663366"></font></strong><font color="#663366"></font>
              </td>
            <td width="87%">
                <input name="textfield" type="text" size="50">
            </td>
          </tr>
          <tr>
            <td width="13%"><strong><font color="#FF00FF">内容:</font></strong><font color="#FF00FF"></font></td>
            <td width="%87">
                  <textarea name="textarea" cols="50" rows="10"></textarea>
            </td>
          </tr>
        </table>
        <p>
          <input type="submit" name="Submit" value="发表">
        </p>
      </div>
    </html:form>
    <hr>
    <p align="right">&nbsp;</p>
    <p align="right"><em><strong><font color="#9933CC">论坛进行中&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></strong></em></p>
    </body>
    </html>
      

  4.   

    我建议你把登陆成功后的“用户名”还有其他要用的东西保存在session中。
    整型变量就不用了,只有一个hashtable就够了,加上index属性就可以了。
      

  5.   

    我以list为例:
    java.util.ArrayList list = new java.util.ArrayList();
        list.add("First");
        list.add("Second");
        list.add("Third");
        list.add("Fourth");
        list.add("Fifth");
        pageContext.setAttribute("list", list, PageContext.PAGE_SCOPE);在jsp中的调用方法:
    <logic:iterate id="item" name="list" indexId="index"
           offset="2" length="3">
      <li><em><bean:write name="item"/></em>&nbsp;[<bean:write name="index"/>]</li>
    </logic:iterate>显示结果:
    Third [2] 
    Fourth [3] 
    Fifth [4]String[]和hasntable的用法和list一样的。
      

  6.   

    谢谢,我也找到方法输出Hashmap了,在Action中定义HsahMap后,将它添加到application中:
     noteInfo.getAllNote().put(new Integer(number,new AllData(caption,username,dateString, content));
      application.setAttribute("noteInfo", noteInfo);
    在Jsp中:
    <logic:iterate id="item" name="noteInfo" property="allNote" scope="application">
    <tr>
      <td><bean:write name="item" property="key"/></td>
      <td><bean:write name="item" property="value.caption"/></td>
      <td><bean:write name="item" property="value.name"/></td>
      <td><bean:write name="item" property="value.time"/></td>
    </tr>
    </logic:iterate>
    问一下:Hashtable现在不是不建议是用了吗?
    我是新人,现在很穷,所以只能给楼上的那位大哥很少的分了!
    对不住了!