fetching records from database and displaying on struts webpage

2.6k views Asked by At

My query is how to fetch records from a database table and display it on struts webpage. I have pasted my code below. When I run this code I didn't get any error but the table is not displayed even though the table contains many records. I googled it, but couldn't find the answer. Even the SO's similar solution didn't help me.. Thanks in advance.

//POJO Class

package example;

public class SplitConfig {

    private String file_id;
    private String category;

    public String getFile_id() {
        return file_id;
    }

    public void setFile_id(String file_id) {
        this.file_id = file_id;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

}

//Action Class

package example;

import com.opensymphony.xwork2.ActionSupport;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class SplitConfigAction extends ActionSupport {

    private ArrayList<SplitConfig> list = new ArrayList<SplitConfig>();

    public ArrayList<SplitConfig> getList() {
        return list;
    }

    public void setList(ArrayList<SplitConfig> list) {
        this.list = list;
    }

    public SplitConfigAction() {
    }

    public String execute() throws Exception {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection("jdbc:sqlserver://192.168.100.25:1433;databaseName=db_h2h;user=sa;password=123");
            PreparedStatement ps = con.prepareStatement("select * from reporttracking.dbo.FILE_MASTER");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                SplitConfig sc = new SplitConfig();
                sc.setFile_id(rs.getString(1));
                sc.setCategory(rs.getString(2));
                list.add(sc);

            }
            con.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return "success";
    }

}

//JSP Page

<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <jsp:include page="header.jsp"/>
        <jsp:include page="menu.jsp"/>
        <h3>All Records:</h3>  
        <s:iterator  value="list">  
            <fieldset> 

                <s:property value="file_id"/><br/>  
                <s:property value="category"/><br/>  

            </fieldset>  
        </s:iterator>  
        <jsp:include page="footer.jsp"/>
    </body>
</html>

EDIT: //struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="subin" namespace="" extends="struts-default">

        <action name="login" class="example.ShowLoginAction">
            <result name="success">/index.jsp</result>
        </action>

        <action name="pass" class="example.ShowPassAction">
            <result name="success">/changepass.jsp</result>
        </action>

        <action name="config" class="example.ShowConfigAction">
            <result name="success">/fileconfig.jsp</result>
        </action>

        <action name="splitcon" class="example.ShowSplitAction">
            <result name="success">/filesplit.jsp</result>
        </action>


        <action name="dashboard" class="example.ShowDashboardAction">
            <result name="success">/dashboard.jsp</result>
        </action>

        <action name="verify" class="example.LoginAction">
            <result name="success">/dash.jsp</result>
            <result name="fail">/fail.jsp</result>
        </action>

        <action name="changep" class="example.PassAction">
            <result name="success">/dash.jsp</result>            
            <result name="fail">/fail.jsp</result>
        </action>

        <action name="filecon">
            <result name="success">/fileconfig.jsp</result>
            <result name="fail">/fileconfig.jsp</result>
        </action>


    </package>



</struts>

//web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
1

There are 1 answers

0
NewBee Developer On BEST ANSWER

I wrote a different class name for splitcon action... Instead of ShowSplitAction, the class should be SplitConfigAction...