从数据库中获取记录并在struts网页上显示

时间:2015-06-19 10:06:23

标签: java database jsp struts2 struts-tags

我的查询是如何从数据库表中获取记录并将其显示在struts网页上。我在下面粘贴了我的代码。当我运行此代码时,我没有收到任何错误,但即使表包含许多记录,也不显示表。我用Google搜索,但找不到答案。即使是SO的类似解决方案也没有帮助我......提前感谢。

// 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;
    }

}

//动作类

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页面

<%@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>

编辑: //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 个答案:

答案 0 :(得分:0)

我为splitcon操作写了一个不同的类名...而不是ShowSplitAction,这个类应该是SplitConfigAction ...

相关问题