struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chriss Nold" <chriss.n...@gmail.com>
Subject Re: Using iterator tag to retrieve list from session
Date Tue, 26 Aug 2008 19:54:47 GMT
Thanks, LuKasz, I will look into the SessionAware Interface, I've never used
it before.
The iterator is now displaying the results, but when I added more records to
the database, I found that the session is holding three different records,
but the iterator tag is displaying the last record three times.  Any ideas?
The code is the same as previously send except that the iterator value is
#session.eventList


Thanks,
Chriss

On Tue, Aug 26, 2008 at 1:59 PM, Chriss Nold <chriss.nold@gmail.com> wrote:

> Thank you very much.  I changed the getList to getEventList, and changed
> the iterator to #session.eventList and it works fine now.
> I knew it had to be something simple that I was missing.
>
> Thanks again for the help.
> Chriss
>
>
>
> On Tue, Aug 26, 2008 at 12:46 PM, Jim Kiley <jhkiley@summa-tech.com>wrote:
>
>> This doesn't directly address your question, but it seems to me that it
>> isn't a great idea to name your action method getList().  The default
>> assumption a reader will make is that getList() is a method intended to
>> return the value of a field called "list".  It is not clear to me how this
>> would interact with Struts.  Anyway -- I'd rename that method to execute()
>> or something, if I were you.
>>
>> Anyway, as to your actual question, you appear to be iterating over the
>> "round" field of the "eventList" object, which doesn't have a "round"
>> field
>> at all.  I suspect that you just want to iterate over eventList itself (or
>> #session.eventList, I forget offhand).
>>
>> jk
>>
>> On Tue, Aug 26, 2008 at 1:30 PM, Chriss Nold <chriss.nold@gmail.com>
>> wrote:
>>
>> > Hello All,
>> >     I am trying to retreive a list from the session and display that
>> list
>> > on a jsp, but I am not entirely sure of the correct syntax for reading
>> the
>> > list.  Any help would be appreciated.  Here is the code that I am
>> cuurently
>> > using:
>> > Action class:
>> > package round;
>> >
>> > import java.sql.Connection;
>> > import java.sql.DriverManager;
>> > import java.sql.ResultSet;
>> > import java.sql.SQLException;
>> > import java.sql.Statement;
>> > import java.util.ArrayList;
>> > import java.util.List;
>> > import java.util.Map;
>> >
>> > import com.opensymphony.xwork2.ActionContext;
>> > import example.ExampleSupport;
>> > public class GetListAction extends ExampleSupport{
>> >    public String getList() throws SQLException {
>> >        Round round = new Round();
>> >        List<Round> eventList = new ArrayList<Round>();
>> >        Map session = (Map)ActionContext.getContext().get("session");
>> >        String userName = String.valueOf(session.get("userName"));
>> >        try {
>> >            Statement stmt;
>> >            Class.forName("com.mysql.jdbc.Driver");
>> >            String url =
>> >                "jdbc:mysql://localhost:3306/Nexus";
>> >            String sql = "Select * " +
>> >            "FROM round WHERE userName='" + userName + "'";
>> >            Connection con =
>> >                DriverManager.getConnection(
>> >                        url,"root", "*****");
>> >            stmt = con.createStatement();
>> >            ResultSet rs = stmt.executeQuery(sql);
>> >              while(rs.next()){
>> >                  round.setMatchID(rs.getInt(1));
>> >                  round.setEventID(rs.getInt(2));
>> >                  round.setUserName(rs.getString(3));
>> >                  round.setRound(rs.getInt(4));
>> >                  round.setPersona(rs.getString(5));
>> >                  round.setResult(rs.getInt(6));
>> >                  round.setOpponent(rs.getString(7));
>> >                  eventList.add(round);
>> >                }
>> >            con.close();
>> >        }catch( Exception e ) {
>> >            e.printStackTrace();
>> >        }
>> >        session.put("eventList", eventList);
>> > System.err.println("Session objects: " + session.get("eventList"));
>> >        return SUCCESS;
>> >    }
>> > }
>> >
>> > Query is successful, and System.err.println displays: Session objects:
>> > [round.Round@1cd6782]
>> >
>> > JSP:
>> > <%@taglib prefix="s" uri="/struts-tags" %>
>> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "
>> > http://www.w3.org/TR/html4/strict.dtd">
>> > <html>
>> > <head>
>> > <title>Welcome to Nexus!!!</title>
>> > <LINK rel="stylesheet" type="text/css" href="../css/styles.css">
>> > </head>
>> >
>> > <body background="../images/containment.jpg">
>> >
>> > <table width="100%">
>> > <tr>
>> > <td width="10%" rowspan="4">
>> >
>> > <s:include value="../includes/menu.jsp"/>
>> >
>> > </td>
>> > <td>
>> > <table border="0" width="90%" align="center">
>> >    <tr>
>> >        <td align="center">
>> >            <h3>
>> >                Welcome to the Nexus, <s:property
>> > value="#session.userInfo.firstName"/>
>> >            </h3>
>> >        </td>
>> >    </tr>
>> >    <tr>
>> >        <td align="center">
>> >    <table class="round">
>> >     <tr>
>> >         <th><font color="white">Match ID</font></th>
>> >         <th><font color="white">Event ID</font></th>
>> >         <th><font color="white">Persona Used</font></th>
>> >         <th><font color="white">Opponent</font></th>
>> >         <th><font color="white">Result</font></th>
>> >         <th><font color="white">Round</font></th>
>> >     </tr>
>> >     <tr>
>> > <s:iterator value="#eventList.round" status="rowstatus">
>> >  <tr>
>> >    <s:if test="#rowstatus.odd == true">
>> >      <td style="background: grey">
>> >         <s:property value="matchID" />
>> >         <s:property value="eventID" />
>> >         <s:property value="persona" />
>> >         <s:property value="opponent" />
>> >         <s:property value="result" />
>> >         <s:property value="round" />
>> >    </s:if>
>> >    <s:else>
>> >         <s:property value="matchID" />
>> >         <s:property value="eventID" />
>> >         <s:property value="persona" />
>> >         <s:property value="opponent" />
>> >         <s:property value="result" />
>> >         <s:property value="round" />
>> >    </s:else>
>> >  </tr>
>> > </s:iterator>
>> >    </tr>
>> >  </table>
>> >        </td>
>> >        </tr>
>> > </table>
>> > </table>
>> > </body>
>> > </html>
>> >
>> > Displays users first name, but not the list.
>> >
>> > Struts2 mapping:
>> >
>> >        <action name="viewTournaments" class="round.GetListAction"
>> > method="getList">
>> >            <result
>> > name="success">/jsp/tournament/tournamentList.jsp</result>
>> >            <interceptor-ref name="basicStack"/>
>> >        </action>
>> >
>> > Thanks in advance for the help,
>> > Chriss
>> >
>>
>>
>>
>> --
>> Jim Kiley
>> Technical Consultant | Summa
>> [p] 412.258.3346 [m] 412.445.1729
>> http://www.summa-tech.com
>>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message