如何遍历 JSP 中的集合并在操作 class 中设置对象中的值
How to iterate through collection in JSP and set values in an object in an action class
我有一个 JSP 从操作 class 接收集合列表。我正在遍历该列表,我希望通过表单请求将该列表的值设置为另一个操作 class 内的另一个对象。当我在迭代器中使用 <s:property
标签时使用 displayMovies.jsp 时,它会显示在集合中的不同对象上。我想将集合中的每个对象保存或传递给不同的操作 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;
}
}
您应该在迭代器标记上使用 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; }
您不需要初始化 movieDetailedInformationList
,因为 Struts2 使用来自 post 请求的参数填充它。
Movie
class 应该是 public 并且有默认构造函数, public setter for title
是必需的。
我有一个 JSP 从操作 class 接收集合列表。我正在遍历该列表,我希望通过表单请求将该列表的值设置为另一个操作 class 内的另一个对象。当我在迭代器中使用 <s:property
标签时使用 displayMovies.jsp 时,它会显示在集合中的不同对象上。我想将集合中的每个对象保存或传递给不同的操作 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;
}
}
您应该在迭代器标记上使用 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; }
您不需要初始化 movieDetailedInformationList
,因为 Struts2 使用来自 post 请求的参数填充它。
Movie
class 应该是 public 并且有默认构造函数, public setter for title
是必需的。