Renny
Renny

Reputation: 114

Dynamic url using s:iterator in struts2

My question is: how to give dynamic param to an url using <s:itarator> in struts2? I have some data stored in a MySQL database, I put the data (Id and Name) in two ArrayList

    package Model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Film {
    private String id;
    private String titolo;
    private String trama;
    private int i=0;
    private ArrayList<String> puntate = new ArrayList<String>();
    private ArrayList<Integer> idPuntate = new ArrayList<Integer>();
    protected String DRIVER = "com.mysql.jdbc.Driver";
    protected String url = "jdbc:mysql://localhost/SitoWeb";
    protected String user = "root";
    protected String psw = "";
    private Connection con;
    public String execute(){
    Connessione();
    Query();
    return "success";
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    public String getId() {return id;}
    public void setId(String id) {this.id = id;}
    public String getTitolo(){return titolo;}
    public void setTitolo(String titolo)  {this.titolo=titolo;}
    public String getTrama(){return trama;}

    public void Connessione(){
        try{
        con=null;
        Class.forName(DRIVER);
        con = DriverManager.getConnection(url,user,psw);
        }catch(Exception e){}
    }   
    public void Query(){
        try {
            PreparedStatement cmd = con.prepareStatement("SELECT Film.Titolo, Film.Trama, Episodi.Nome, Episodi.idEpisodio FROM Film INNER JOIN Episodi ON Film.Id = Episodi.id_Film WHERE id = ?");
            cmd.setString(1, id);
            ResultSet rs = cmd.executeQuery();
            while(rs.next()){
                titolo = rs.getString("titolo");
                trama = rs.getString("trama");
                idPuntate.add(i, rs.getInt("idEpisodio"));
                puntate.add(i,rs.getString("Nome"));
                i++;
            }
            rs.close();
            cmd.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public ArrayList<Integer> getIdPuntate() {
        return idPuntate;
    }

    public void setIdPuntate(ArrayList<Integer> idPuntate) {
        this.idPuntate = idPuntate;
    }

    public ArrayList<String> getPuntate() {
        return puntate;
    }

    public void setPuntate(ArrayList<String> puntate) {
        this.puntate = puntate;
    }
}

Now I want to show these data in my JSP page. I tried something like this:

<s:url action="episodi" var="episodi">
<s:param name="id"></s:param> // I don't know what to put here...
</s:url>
<ol>
<s:iterator value="puntate">
  <li><a href="<s:property value='#episodi'/>"><s:property/></a></li>
</s:iterator>
</ol>

I don't know what to put in <s:param name="id"></s:param> because the param is an ArrayList. I tried to put <s:iterator value="idPuntate"> but it returns 0..

Upvotes: 2

Views: 1798

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50203

There are two problems:

  1. If you are generating an url for each element of a Collection, then you need to put the url generation inside the iteration of that Collection, otherwise you will have always the same url:

    <ol>
    <s:iterator value="puntate">
        <s:url action="episodi" var="episodio">
            <s:param name="id">something here</s:param>
        </s:url>
    
        <li><a href="<s:property value='#episodio'/>"><s:property/></a></li>
    </s:iterator>
    </ol>
    
  2. You have not used OOP, that would suggest to create a bean named Movie (or Film), with a List<Episode>, and Episode should be another bean with for example Long id, Integer season, Integer number, and String description.

    Since you've used two different lists, one for the episode description and another one for the episode id, and assuming they're aligned (it is not guaranteed, so I strongly suggest you to refactor your code the OOP way), you can access the IDs by their index basing on the episode description index, during the iteration:

    <ol>
    <s:iterator value="puntate" status="ctr">
        <s:url action="episodi" var="episodio">
            <s:param name="id">
                <s:property value="idPuntate[%{#ctr.index}]"/>
            </s:param>
        </s:url>
    
        <li><a href="<s:property value='#episodio'/>"><s:property/></a></li>
    </s:iterator>
    </ol>
    

P.S: Film should be a bean declared in your action, not the action itself... otherwise it is not portable... what if you need the Film class in other actions ?

Upvotes: 2

Related Questions