为什么我无法在 jsp 页面中打印列表的值?

Why I'm not able to print the value of list in jsp page?

为什么我无法在 JSP 页面中打印列表的值?我可以使用 Struts2

在控制台中打印列表的值,但不能在 JSP 页面中打印

这是我的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>

我的classviewdetails.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;
    }

我的classgetAction.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";  
    }  
}

还有我的最后一个jsp: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>

变量 listvalueStack 中不可用。要向 valueStack 添加变量,您可以对 属性 list 使用操作 getter 并在结果之前对其进行初始化。

由于您已经在另一个 class 中有一个 getter,您可以通过将 execute 移动到 class.

来重新映射您的操作

请注意,您永远不会使用 new 创建操作 class,但您可以让 Struts 通过 ObjectFactory 构建操作。


struts.xml

<package name="getdetails" extends="struts-default">
    <action name="details" class="com.getAction">
        <result name="success">/viewdetails.jsp</result>

    </action>
</package>

行动class

public class getAction extends ActionSupport{
  ...

  public String execute() {
    String a=getresult(null);


    System.out.println("the value of a is:"+a);
    return a;

  }