初学者玩struts2 ,jsp页面显示不出来数据库内容,求解

DB_Connect.java
[code="java"]package com.util;

import java.sql.*;

public class DB_Connect {
// 数据库地址
private final String url = "jdbc:oracle:thin:@localhost:1521:orcl" ;
private final String username = "metrology";
private final String password = "password";
//数据库驱动地址
private final static String driver = "oracle.jdbc.OracleDriver";
private static Connection conn = null;
//加载数据库驱动
static {
try {
Class.forName(driver);
System.out.println("驱动加载成功!");
}
catch(Exception e) {
System.out.println(e);
}
}
//创建数据库连接
public boolean createConnection(){
try{
conn = DriverManager.getConnection("url","username","password");
conn.setAutoCommit(true);
}
catch (SQLException e){
System.out.println(e.getMessage());
}
return true;
}
public Connection getConnection(){
return this.conn;
}
public void close() throws SQLException{
this.conn.close();
}
//查询数据库
public ResultSet query(String sql){
ResultSet rs = null;
try {
if (conn == null){
createConnection();
}
Statement stmt = conn.createStatement();
try{
rs = stmt.executeQuery(sql);
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
catch(SQLException e){
System.out.println(e.getMessage());
}
return rs;
}
}
[/code]
newsVO.java
[code="java"]package com.vos;

public class newsVO {
private String title;
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;
}
private String content;
}
[/code]
newsDAO.java
[code="java"]package com.daos;

import java.sql.*;
import java.util.ArrayList;
import com.util.DB_Connect;
import com.vos.newsVO;

public class newsDAO {
private DB_Connect connection;
private newsVO vo;
public ArrayList queryNews(){
ArrayList list = new ArrayList();
String sql = "select * from news";
ResultSet rs = connection.query(sql);
try{
while(rs.next()){
vo.setTitle(rs.getString(1));
vo.setContent(rs.getString("content"));

        }
    }catch(SQLException e){
        e.printStackTrace();
    }
    return list;
}

}
[/code]
newsService.java
[code="java"]package services;

import java.util.ArrayList;

import com.daos.newsDAO;
import com.vos.newsVO;

public class NewsService {
private newsDAO dao;
public ArrayListqueryNews(){
return dao.queryNews();
}

}
[/code]
NewsAction.java
[code="java"]package com.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import services.NewsService;

import com.opensymphony.xwork2.ActionSupport;
import com.vos.newsVO;

public class NewsAction extends ActionSupport {
private NewsService service;
private static final long serialVersionUID = 1L;
private String title;
private String content;
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;
}
@Override
public String execute(){
try{
HttpServletRequest request = ServletActionContext.getRequest();
ArrayList list = service.queryNews();
request.setAttribute("news", list);
return SUCCESS;
}catch(Exception e)
{
e.printStackTrace();
}
return SUCCESS;
}
}
[/code]

JSP页面:
[code="java"]

/s:property
/s:push
/s:iterator[/code]

6个回答

dao和service貌似没注入吧

断点调试一下,你的service 或者 dao 是否注入成功,在action中返回的值是否有值

配置文件贴一下

哦,我错了

set一下,然后配置文件里面配置一下吧

:cry: 我发现我彻底错了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问