从豆类型的数组列表填充在JSP中的下拉列表 [英] Populating a drop down list in JSP from a Array List of Bean type

查看:142
本文介绍了从豆类型的数组列表填充在JSP中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java EE项目,我有以下的Servlet:

In my Java EE project I have the following Servlet:

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

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

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TitlePopulatorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("Inside doGet()!");

        ArrayList<BeanTitle> bt=new ArrayList<BeanTitle>();

        java.io.PrintWriter out = response.getWriter();
        response.setContentType("text/html"); 

        Connection conn=null;


        try {
            /* get the DataSource from using the JNDI name */
            Class.forName("com.mysql.jdbc.Driver");
            InitialContext ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Test");

            //Create connection and then continue as usual other JDBC calls 
            conn=ds.getConnection();
            System.out.println("\nConnection Successful in TitlePopulatorServlet !");
            Statement s= conn.createStatement();
            ResultSet rs=s.executeQuery("SELECT * FROM story");

            while (rs.next() ) 
            {
                BeanTitle b = new BeanTitle();
                b.setBtitle(rs.getString(1));
                bt.add(b);
            }

        } catch (Exception e){
            out.println("Failed!"+ e);
        }


            request.setAttribute("bt", bt);                                                         
            request.getRequestDispatcher("/StoryTitlePage.jsp").forward(request,response);


    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



    }
}

的BeanTitle如下:

The BeanTitle is as follows:

public class BeanTitle 
{
    private String btitle;

    public String getBtitle() {
        return btitle;
    }

    public void setBtitle(String btitle) {
        this.btitle = btitle;
    }
}

和JSP页面(StoryTitlePage.jsp)如下:

And the JSP page ("StoryTitlePage.jsp") is as follows:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="storylist" class="serv.TitlePopulatorServlet" scope="request" />

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Story Title Page</title>
</head>
<body>

<form action="/ReportData/DisplayReport" method="post" >

        Please select an element: 

        <select id="selectedRecord" name="selectedRecord">

            <c:forEach var="item" items=${storylist.bt} >
                <option>${item.tarr}</option>
            </c:forEach>
        </select>

        <input type="submit" value="Submit"> 

    </form>

</body>
</html>

但是,当我启动服务器上TitlePopulatorServlet这是行不通的。
我在哪里在迭代脚麻/ EL?

But when I launch the TitlePopulatorServlet on the server it doesn't work. Where am I going wrong in the iteration/EL?

推荐答案

该servlet存储列表中的要求,命名为BT的属性下。因此,它已经存在,在请求中。没有必要做

The servlet store the list in the request, under the attribute named "bt". So it's already there, in the request. There is no need to do

<jsp:useBean id="storylist" class="serv.TitlePopulatorServlet" scope="request" />

这只会造成你的servlet,它没有任何意义的新实例。和BT是一个请求属性。这不是在servlet的属性。因此,使用 $ {storylist.bt} 没有任何意义无论是。

That will only create a new instance of your servlet, which doesn't make any sense. and bt is a request attribute. It's not a property of the servlet. So using ${storylist.bt} doesn't make any sense either.

您想遍历的BT 的元素,因此,所有你需要的是

You want to iterate over the elements of bt, so all you need is

<c:forEach var="item" items="${bt}">

最后,BT 的每一个项目的类型为 BeanTitle 。而你正在做

${item.tarr}

这是等同于调用

beanTitle.getTarr()

和没有在BeanTitle没有塔尔财产。唯一的属性是 BTITLE (因为类的唯一吸气剂被命名为 getBTitle())。

And there is no tarr property in BeanTitle. The only property is btitle (since the only getter of the class is named getBTitle()).

所以,你必须使用的是

${item.btitle}

您的命名是可怕的,那只有让你更糊涂了。你为什么不使用真正的单词来命名你的类和属性?例如像

Your naming is awful, and that only makes you more confused. Why don't you use real words to name your classes and properties? Like for example

public class Book {
    private String title;

    public String getTitle() 

    ...
}

这篇关于从豆类型的数组列表填充在JSP中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆