Why I'm not able to print the value of list in JSP page? I'm able to print the value of list in console but not in JSP page using Struts2
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
configuration file<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>
The variable list
is not available in the valueStack
. To add a variable to the valueStack
you can use an action getter for the property list
and initialize it before the result.
Since you already have a getter in another class you can remap your action with moving execute
to the action class.
Note, that you never create your action class with new
, but you can let Struts build the action via 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;
}