有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Struts2:JSP页面无法从数据库获取值

JSP页面无法从数据库获取数据。我正在使用SQL Server

获取通知。java

package tom.st2.dao;

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

import tom.st2.model.Notice;

public class FetchNotices {

    ArrayList<Notice> nlist=new ArrayList<Notice>();  

    public ArrayList<Notice> getList() {  
        return nlist;  
    }  
    public void setList(ArrayList<Notice> nlist) {  
        this.nlist = nlist;  
    }  

    public String execute(){  
     try{        
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         String url ="jdbc:sqlserver://localhost;database=db;";      
         Connection con = DriverManager.getConnection(url,"id","pw");

         PreparedStatement ps=con.prepareStatement("select * from notice");
         ResultSet rs=ps.executeQuery();

         while(rs.next()){           
             Notice notice=new Notice();
             System.out.println(rs.getInt(1));
             notice.setNoticeindex(rs.getInt(1)); //index
             System.out.println(rs.getString(2));
             notice.setId(rs.getString(2)); //id
             System.out.println(rs.getString(3)); //
             notice.setTitle(rs.getString(3));//제목
             System.out.println(rs.getString(4));
             notice.setContent(rs.getString(4));
             System.out.println(rs.getDate(5));
             notice.setDate(rs.getDate(5));          
             nlist.add(notice);
             }
         System.out.println(nlist);
         con.close();
         }catch(Exception e){e.printStackTrace();} 

     return "success";  
    }  

}

我使用System.out.println进行调试,发现它确实从数据库中获取值

下面是控制台的结果

1
duhan
일딸라는 너무 적소
사딸라쯤 합시다
1972-11-21
2
duhan
일딸라는 너무 적소
사딸라쯤 합시다
1972-11-21
3
duhan
일딸라는 너무 적소
사딸라쯤 합시다
1972-11-21
4
duhan
일딸라는 너무 적소
사딸라쯤 합시다
1972-11-21
5
duhan
일딸라는 너무 적소
사딸라쯤 합시다
1972-11-21
6
duhan
일딸라는 너무 적소
사딸라쯤 합시다
1972-11-21
[tom.st2.model.Notice@77013a0b, tom.st2.model.Notice@3756b281, tom.st2.model.Notice@74509331, tom.st2.model.Notice@73d8192a, tom.st2.model.Notice@d77b438, tom.st2.model.Notice@fb7ddad]

注意者。jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri ="http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>공지사항</title>
</head>
<body>
    <jsp:include page="../header.jsp"></jsp:include>
    <c:set var="checkSession" scope="session" value="${session.id}"/>
    <c:if test="${checkSession!=null }">
            <a href="noticewrite">등록(관리자 전용)</a>&nbsp;|
    </c:if>
    <c:if test="${checkSession!=null }">
            <a href="#">수정(관리자 전용)</a>&nbsp;|
    </c:if>
    <c:if test="${checkSession!=null }">
            <a href="#">삭제(관리자 전용)</a>
    </c:if><br><br>

    <h3>공지사항</h3>
    <s:iterator value="nlist">  
    <fieldset>
        INDEX: <s:property value="noticeindex"/><br>
        작성자 ID: <s:property value="id"/><br>
        제목: <s:property value="title"/><br>
        내용: <s:property value="content"/><br>  
        작성일시: <s:property value="date"/><br>
    </fieldset>  
    </s:iterator>
</body>
</html>

注意。java

package tom.st2.model;

import java.util.Date;

import tom.st2.dao.WriteDao;

public class Notice {

    private int noticeindex;
    private String id;
    private String title;
    private String content;
    private Date date;

    public Notice() {}

    public Notice(int noticeindex, String id, String title, String content, Date date) {
        this.noticeindex = noticeindex;
        this.id = id;
        this.title = title;
        this.content = content;
        this.date=date;
    }
    public int getNoticeindex() {
        return noticeindex;
    }
    public void setNoticeindex(int noticeindex) {
        this.noticeindex = noticeindex;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }

    public String execute() {   
        int i = WriteDao.save(this);        
        if(i>0) {return "success";}
        else {return "error";}      
    }
}

支柱的一部分。xml

<action name="noticelist" class="tom.st2.dao.FetchNotices">  
            <result name="success">notice/noticelist.jsp</result>  
        </action>

我不明白为什么它不能获取数据。因为没有任何错误消息,所以我更难找到原因

如果您有任何建议,我将不胜感激


共 (0) 个答案