JSP页面无法显示数据库信息???求助啊!有图有代码,求大神雪中送炭

JSP页面显示这样:图片说明
JSP代码:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ include file="../base.jsp"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新闻类型列表</title>

    <style type="text/css">
    body{
        background-image: url("images/bg_newsDetailList1.jpg");
        background-position:-100px -300px;
    }
    .enableStyle{
        background-color: ;
        color:black;
    }
    .disableStyle{
        background-color: red;
        color: white;
    }
</style>
<script type="text/javascript">
    function checkSelected(){
        var flag=false;
        var ids=document.getElementsByName("ids");
        for(var i=0;i<ids.length;i++){
            if(ids[i].checked){
                flag=true;
                break;
            }
        }
        if(!flag){
            alert("请选择需要删除的数据!");
        }else{
            if(confirm("确定要删除选中的数据吗?")){
                document.myform.submit();
            }
        }

    }

</script>
</head>
<body>
    <form name="myform" method="post" action="newstypeservlet?op=batchDelete">
        <table border="1">
            <tr>
                <th>编号</th>
                <th>类型名</th>
                <th>编辑</th>
                <th>删除</th>
            </tr>

            <c:forEach items="${requestScope.newsTypeList }"  var="newsType" varStatus="status">
            <tr style="background-color: ${status.index%2==0?'red':'green' } ">
                <td>
                    <input type="checkbox" name="ids" value="${newsType.id }"/>
                    ${newsType.id }
                </td>
                <td>${newsType.typeName }</td>
                <td><a href="newstypeservlet?op=toEdit&id=${newsType.id }">编辑</a></td>
                <td><a href="newstypeservlet?op=delete&id=${newsType.id }">删除</a></td>
            </tr>
        </c:forEach>

        <tr>
            <td colspan="4" >
                <input type="button" value="批量删除" onclick="checkSelected()"/>
            </td>
        </tr>
        </table>
    </form>
</body>
</html>

其中base.jsp代码如下:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath %>" />

Servlet代码:

 protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        // System.out.println("进入sevlet");
        request.setCharacterEncoding("UTF-8");// 解决Post提交时的乱码问题
        String op = request.getParameter("op");// 注意:此处的JSP页面不放在根目录下,Servlet就无法接收到页面传来的数据,因为web.xml配置信息无法覆盖
        if ("add".equals(op)) {
            addNewsType(request, response);
        } else if ("list".equals(op)) {
            listNewsType(request, response);
        } else if ("batchDelete".equals(op)) {
            batchDelete(request, response);
        } else if ("delete".equals(op)) {
            deleteById(request, response);
        }
    }

    // 新闻列表
    private void listNewsType(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        NewsTypeService newsTypeService = new NewsTypeService();
        List<NewsType> newsTypeList = newsTypeService.findAll();
        request.setAttribute("newsTypeList", newsTypeList);
        request.getRequestDispatcher("../listNewsType.jsp").forward(request,
                response);
    }

    private void deleteById(HttpServletRequest request,
            HttpServletResponse response) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        String idStr = request.getParameter("id");
        if (idStr == null || "".equals(idStr)) {
            out.write("<script>alert('密码不能为空');history.back();</script>");
            return;
        }
        int id = Integer.parseInt(idStr);
        NewsTypeService newsTypeService = new NewsTypeService();
        newsTypeService.delete(id);
        // 删除数据后,返回列表
        listNewsType(request, response);
    }

    private void batchDelete(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String[] ids = request.getParameterValues("ids");
        NewsTypeService newsTypeService = new NewsTypeService();
        newsTypeService.batchDelete(ids); // 删除数据后,返回列表
        listNewsType(request, response);

    }

service代码:

 package com.syf.service;

import java.util.List;

import com.syf.dao.NewsTypeDao;
import com.syf.entity.NewsType;

public class NewsTypeService {

    public int save(NewsType newsType) {

        return new NewsTypeDao().save(newsType);
    }

    public int batchDelete(String[] ids) {
        return new NewsTypeDao().batchDelete(ids);
    }

    public List<NewsType> findAll() {
        return new NewsTypeDao().findAll();
    }

    public int delete(int id) {
        return new NewsTypeDao().delete(id);
    }

}


DAO层代码:


package com.syf.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.syf.entity.NewsType;
import com.syf.util.ArrayUtil;
import com.syf.util.DBUtil;

public class NewsTypeDao {

    DBUtil dbUtil = new DBUtil();

    // 保存新闻类型
    public int save(NewsType newsType) {
        String sql = "insert into newstype(typeName) values (?)";
        Object[] params = { newsType.getTypeName() };
        return dbUtil.executeUpdate(sql, params);
        // executeUpdate的返回值是一个整数,指示受影响的行数(即更新计数)
    }

    public List<NewsType> findAll() {

        List<NewsType> newsTypeList = new ArrayList<NewsType>();

        String sql = "select * from newstype";
        ResultSet rs = dbUtil.executeQuery(sql, null);
        try {
            while (rs.next()) {
                NewsType newsType = new NewsType(rs.getInt("id"),
                        rs.getString("typeName"));
                newsTypeList.add(newsType);// 将对象放入到集合
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return newsTypeList;
        } finally {
            dbUtil.closeAll();
        }
        return newsTypeList;
    }

    /**
     * 批量删除
     * 
     * @param ids
     *            指定的ID集合
     * @return
     */
    public int batchDelete(String[] ids) {
        // TODO Auto-generated method stub
        String sql = "delete from newstype where id in(?)";
        Object[] params = { ArrayUtil.array2String(ids, ",") };

        return dbUtil.executeUpdate(sql, params);
    }

    /**
     * 根据Id进行删除
     * 
     * @param id
     * @return
     */
    public int delete(int id) {
        String sql = "delete from newsType where id=?";
        Object[] params = { id };
        return dbUtil.executeUpdate(sql, params);
    }

}

entity实体类代码:

 package com.syf.entity;

public class NewsType {

    private int id;
    private String typeName;

    public NewsType() {
        super();
    }

    public NewsType(String typeName) {
        super();
        this.typeName = typeName;
    }

    public NewsType(int id, String typeName) {
        super();
        this.id = id;
        this.typeName = typeName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

}

3个回答

找了好久,没找到问题出在哪,求救各位老师,谢谢

首先排查是页面问题还是后台java代码问题,先把界限弄清了。
1、确定${requestScope.newsTypeList }有没有值,写法有没有问题,最后的空格去掉,都写规范了。
2、如果没有,直接request.getAttribute的方式能不能获取到。
3、如果都不行,后台list放到session中试试,看前端能不能取到
最终再看哪个环节出问题,再来针对性的解决。

后台查询一下有没有得到数据库的数据,

weixin_32621387
weixin_32621387 回复wunaidepao: 但是我不知道密码
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问