如何在JSP中迭代集合并在操作类中的对象中设置值

时间:2016-02-28 17:47:30

标签: java jsp struts2 ognl indexed-properties

我有一个JSP,它从一个动作类中接收一个集合列表。我正在遍历该列表,我希望通过表单请求将该列表的值设置为另一个动作类中的另一个对象。当我在迭代器中使用<s:property标记时使用displayMovies.jsp时,它会显示在集合中的不同对象上。我想将集合中的每个对象保存或传递给另一个动作类。

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;
    }
}

1 个答案:

答案 0 :(得分:0)

您应该在迭代器标记上使用 java.lang.NullPointerException at samuel.apps.UsersList.populateUsersList(UsersList.java:100) at samuel.apps.UsersList.$MarkUser.onPostExecute(TabOne.java:370) at samuel.apps.UsersList.$MarkUser.onPostExecute(TabOne.java:345) at android.os.AsyncTask.finish(AsyncTask.java:631) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 变量。

status

<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>

movieDetailedInformationList

您不需要初始化private List<Movie> movieDetailedInformationList; public List<Movie> getMovieDetailedInformationList() { return movieDetailedInformationList; } ,因为Struts2使用post请求中的参数填充它。

movieDetailedInformationList类应该是公共的并且具有默认构造函数,Movie的公共setter是必需的。