javajspstruts2ognlindexed-properties

How to iterate through collection in JSP and set values in an object in an action class


I have a JSP that receives a collection list from an action class. I am iterating through that list and I wish to set the values of that list to another object inside another action class through a form request. When I use the displayMovies.jsp when I use the <s:propertytag in the iterator it displays on the different objects in the collection. I want to save or pass each of those objects in the collection to a different action class.

displayMovies.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="com.sans.model.Movie"  %>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Movie retrieval Page
<br />
<s:iterator value="movieRetrievedArray" var="movieS">

<s:form action="movieDetails.action" method="post" id="movieDetailsForm">
    <s:property value="title"/><br />
    <s:property value="releaseDate"/><br /> 

    <s:hidden name="movieDetailedInformation.title" value="%{title}" id="hiddenMovie" />

    <img  src="<s:property value="posterPath" />" onClick="test()">
</s:form>
    <br />
    <br />
</s:iterator>

<script type="text/javascript">

function test() {
    document.getElementById("movieDetailsForm").submit();
}

</script>

</body>
</html>

MovieDetailsActions.java

package com.esi.actions;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sans.model.Movie;

@SuppressWarnings("serial")
@Results({
    @Result(name="success", location="/RetrieveMovies/movieDetails.jsp"),
    @Result(name="input", location="/RetrieveMovies/movieError.jsp")
})
public class MovieDetailsAction extends ActionSupport {

    private Movie movieDetailedInformation = new Movie();

    @Action(value="movieDetails")
    public String execute() {


        System.out.println(movieDetailedInformation.getTitle());


        return SUCCESS;
    }

    public Movie getMovieDetailedInformation() {
        return movieDetailedInformation;
    }

    public void setMovieDetailedInformation(Movie movieDetailedInformation) {
        this.movieDetailedInformation = movieDetailedInformation;
    }
}

Solution

  • You should use status variable on iterator tag.

    <s:form action="movieDetails.action" method="post" id="movieDetailsForm">
    <s:iterator value="movieRetrievedArray" var="movieS" status="status">    
        <s:property value="title"/><br />
        <s:property value="releaseDate"/><br />     
        <s:hidden name="movieDetailedInformationList[%{#status.index}].title" value="%{title}" id="hiddenMovie" />
        <br />
        <br />    
    </s:iterator>
        <img  src="<s:property value="posterPath" />" onClick="test()">
    </s:form>
    

    The movieDetailedInformationList is

    private List<Movie> movieDetailedInformationList;
    
    public List<Movie> getMovieDetailedInformationList() { return movieDetailedInformationList; }
    

    You don't need to initialize movieDetailedInformationList, because Struts2 populate it with parameters from the post request.

    The Movie class should be public and have default constructor, the public setter for title is necessary.