Assassin_GYN
Assassin_GYN
2019-04-22 01:30
采纳率: 50%
浏览 415
已采纳

一个关于使用JavaBean的问题(新手qiu'zhu)

要求:

在Servlet中创建一个List<用户JavaBean>对象,在List集合对象中存放多个用户JavaBean对象,并将该List集合对象存放在request对象中,最后跳转到users页面;在users页面使用JSTL和EL将List对象取出来,并以表格的形式展示出List中的每个用户信息。

Servlet.java

package cuit;

import java.util.List;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Servlet() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        List<bean> list = new ArrayList<bean>();
        bean a= new bean();
        a.setNumBer("1");
        a.setUserName("gyn");
        a.setPassWord("123");
        list.add(a);
        request.getSession().setAttribute("users", list);
        request.getRequestDispatcher("/users.jsp").forward(request, response);
    }
public void doPost(HttpServletRequest req,HttpServletResponse resp) 
        throws ServletException,IOException{
    this.doGet(req,resp) ;
    }
}

bean.java

package cuit;

public class bean implements java.io.Serializable
{
    private static final long serialVersionUID = 1L;
    private String numBer ;
    private String userName;
    private String passWord ;
    public String getNumBer() {
        return numBer;
    }
    public void setNumBer(String numBer) {
        this.numBer = numBer;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassWord() {
        return passWord;
    }
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
   }

users.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page contentType="text/html;charset=gbk" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta charset="utf-8">
        <title>首页</title>

        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link href="css/bootstrap.min.css" rel="stylesheet">

        <!--[if lt IE 9]>
            <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <link href="css/main1.css" rel="stylesheet">
    </head>
    <body>
<!-- Wrap all page content here -->
<div id="wrap">

  <!-- Fixed navbar -->
  <!-- 导航栏 开始-->
  <div class="navbar navbar-default navbar-fixed-top">
    <div class="container">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="#">Project</a>
      </div>
      <div class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">首页</a></li>
          <li><a href="admin.jsp">管理页面</a></li>
          <li><a href="#">退出</a></li>
        </ul>
      </div><!--/.nav-collapse -->
    </div>
  </div>
   <!-- 导航栏 结束-->
  <!-- Begin page content -->
  <div class="container">
    <div class="page-header">
      <h1>用户列表</h1>
    </div>
    <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>序号</th>
                  <th>用户名</th>
                  <th>密码</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
              <c:forEach var="a" items="${users} ">          
                <tr>
                  <td>${a.numBer }</td>
                  <td>${a.userName }</td>
                  <td>${a.passWord }</td>
                  <td><a href="#">编辑</a>&nbsp;&nbsp;<a href="#">删除</a></td>
                </tr>
               </c:forEach>
              </tbody>
            </table>
          </div>
  </div>
</div>

<div id="footer">
  <div class="container">
    <p class="text-muted credit">网站访问次数:</p>
  </div>
</div>

    <!-- script references -->
        <script type='text/javascript' src="js/jquery-1.9.1.min.js"></script>
        <script type='text/javascript' src="js/bootstrap.min.js"></script>
    </body>
</html>

在myeclipse上运行时,会报错,错误提示为:
Property 'numBer' not found on type java.lang.String
经过数次尝试都没有改对,请问要怎么修改呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • wojiushiwo945you
    毕小宝 2019-04-22 09:33
    已采纳

    JSTL中的foreach标签并用EL表达式读取items时,EL表达式的后大括号与引号之间有空格,会导致无法正确处理表达式的,你这里就存在这个问题:

    <c:forEach var="a" items="${users} ">          
    

    items的后面引号有个空格,去掉:

    <c:forEach var="a" items="${users}">
    
    点赞 评论

相关推荐