Java Struts2:JSP页面无法从数据库获取值
JSP页面无法从数据库获取数据。我正在使用SQL Server FetchNotices.javaJava Struts2:JSP页面无法从数据库获取值,java,sql-server,jsp,struts2,struts,Java,Sql Server,Jsp,Struts2,Struts,JSP页面无法从数据库获取数据。我正在使用SQL Server FetchNotices.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;
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";
}
}
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";}
}
}
noticelist.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> |
</c:if>
<c:if test="${checkSession!=null }">
<a href="#">수정(관리자 전용)</a> |
</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>
struts.xml的一部分
<action name="noticelist" class="tom.st2.dao.FetchNotices">
<result name="success">notice/noticelist.jsp</result>
</action>
notice/noticelist.jsp
我不明白为什么它不能获取数据。因为没有任何错误消息,所以我更难找到原因
如果您的
FetchNotice
类具有属性nlist
,那么根据JavaBeans表示法,它的getter应该命名为getNlist()
,而不是getList()
。这可能会有帮助。@Ivan我将getter和setter的名字改为getNList
和setNList
。但它仍然不起作用;(应该有小写的l.getNlist()
notgetNlist()
<action name="noticelist" class="tom.st2.dao.FetchNotices">
<result name="success">notice/noticelist.jsp</result>
</action>