为什么我无法在JSP页面中打印list的值?我能够在控制台中打印list的值,但不能在使用Struts2的JSP页面中打印
getdetails.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ 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>
<s:form action="details.action">
<s:submit value="getresponse" align="center" />
</s:form>
</body>
</html>
Struts.xml
配置文件<package name="getdetails" extends="struts-default">
<action name="details" class="com.viewdetails">
<result name="success">/viewdetails.jsp</result>
</action>
viewdetails.java
package com;
import java.util.ArrayList;
public class viewdetails{
public String firstname,lastname,teamname,reply;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getTeamname() {
return teamname;
}
public void setTeamname(String teamname) {
this.teamname = teamname;
}
public String getReply() {
return reply;
}
public void setReply(String reply) {
this.reply = reply;
}
public String execute() {
getAction n1=new getAction();
String a=n1.getresult(this);
System.out.println("the value of a is:"+a);
return a;
}
getAction.java
package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.opensymphony.xwork2.ActionSupport;
public class getAction extends ActionSupport{
public ArrayList<viewdetails> list=new ArrayList<viewdetails>();
public ArrayList<viewdetails> getList() {
return list;
}
public void setList1(ArrayList<viewdetails> list) {
this.list = list;
}
public String getresult(viewdetails r)
{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","sanjeev","sanjeev");
PreparedStatement ps=con.prepareStatement("select d.firstname, d.lastname,d.teamname,e.reply from newuser d INNER JOIN teamdetails e ON d.emailid = e.emailid ");
ResultSet rs=ps.executeQuery();
while(rs.next()){
viewdetails view=new viewdetails();
view.setTeamname(rs.getString("teamname"));
view.setFirstname(rs.getString("firstname"));
view.setLastname(rs.getString("lastname"));
view.setReply(rs.getString("reply"));
list.add(view);
System.out.println(rs.getString("teamname"));
System.out.println(rs.getString("firstname"));
System.out.println(rs.getString("lastname"));
System.out.println(rs.getString("reply"));
}
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return "success";
}
}
viewdetails.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ 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>
<center><h3>get response:</h3> </center>
Welcome to see the response
<table>
<s:iterator var="i" step="1" value="list" >
<tr>
<td width="10%"><s:property value="%{teamname}"/></td>
<td width="10%"><s:property value="%{firstname}" /></td>
<td width="10%"><s:property value="%{lastname}" /></td>
<td width="20%"><s:property value="%{emailid}" /></td>
<td width="20%"><s:property value="%{reply}"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
答案 0 :(得分:0)
变量list
在valueStack
中不可用。要向valueStack
添加变量,您可以为属性list
使用动作获取器,并在结果之前对其进行初始化。
由于您已在另一个类中使用了getter,因此可以通过将execute
移动到操作类来重新映射您的操作。
请注意,您永远不会使用new
创建操作类,但您可以让Struts通过ObjectFactory
构建操作。
<package name="getdetails" extends="struts-default">
<action name="details" class="com.getAction">
<result name="success">/viewdetails.jsp</result>
</action>
</package>
public class getAction extends ActionSupport{
...
public String execute() {
String a=getresult(null);
System.out.println("the value of a is:"+a);
return a;
}