Gerald Baretto
Gerald Baretto

Reputation: 399

Struts 2 Iterator tag not working in jsp?

I am trying to display some values from a table. I am passing the values to an object of "user" class which contains the getters and setters after which am passing the object in a list. However am not able to view the values from a jsp file using iterator tag. UserDetail class contains the getters and setters for test1 and test2 variables.

Please find the below code.

success.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>  
<html>
<head>
<title></title>
</head>
<body>      
    <h3>All Records:</h3>  
    <s:iterator value="list">  
    <fieldset>  
    <s:property value="test1"/><br/> 
    <s:property value="test2"/><br/>  
</fieldset>
    </s:iterator>  
</body>
</html>

test.java

package com.abc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class test {

    private String test1;
    private String test2;
    ArrayList<UserDetails> list=new ArrayList<UserDetails>();  

    public String execute() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test3", "test","test");
            PreparedStatement ps=conn.prepareStatement("SELECT * from testtable where data = "+test1);  
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                UserDetails user = new UserDetails();
                user.setTest1(rs.getInt(1));
                user.setTest2(rs.getString(2));

                list.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "SUCCESS";
    }


}

Please Guide.

Upvotes: 0

Views: 1465

Answers (3)

Sreedhar
Sreedhar

Reputation: 1

The object UserDetails should be mapped in JSP with the JAVA code. In JSP you can refer the values like ${user.test1} and ${user.test2}.

Upvotes: -1

从此醉
从此醉

Reputation: 28

no set get method for list in your class.

public void setList(){}
public void getList(){}

Upvotes: 1

Sameer Sawla
Sameer Sawla

Reputation: 729

Element in the jsp is not bind to the object in the java layer.

If I need to display the contents from the Java object back to the JSP, i need to make a
reference between those two. Bind them.

Upvotes: 1

Related Questions