是否可以将HTML参数与列表中的Java Bean变量进行比较?

时间:2018-03-10 20:22:53

标签: java arraylist collections javabeans

我正在尝试访问HTML参数并将其与位于数组列表中的Java Bean变量进行比较。下面是我所拥有的,但是在转发JSP页面时它不起作用。返回JSP的框架,但没有从数组中收到任何信息。

itemBean item = new itemBean();
collection coll = new collection();
List<itemBean> songs= coll.getItems();

for(int i = 0; i < songs.size(); i++) {

    if(action.equals(songs.get(i).getItemCode())) {

        item.setItemCode(songs.get(i).getItemCode());
        item.setSong(songs.get(i).getSong());
        item.setArtist(songs.get(i).getArtist());
        item.setSongURL(songs.get(i).getSongURL());
        item.setDesc(songs.get(i).getDesc());
        request.setAttribute("itemBean", item);

    }

}

JSP页面:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@page import="my.pack.collection" %>
    <%@include file="/header.jsp"%>
    <%@include file="/user-navigation.jsp"%>
    <%@include file="/site-navigation.jsp"%>

    <div id="productList">
        <h5><a href="categories.jsp">back</a></h5>
        <h2>Lesson 1</h2>

        <% if ((request.getAttribute("itemCode")) != null ){ %>
            <jsp:include page="item.jsp" flush="true"/>
        <% }%>
        <ul>

            <li><a>Song: ${itemBean.song}</a></li><br>
            <li><b>Artist: ${itemBean.artist}</b></li><br>
            <li><img src=${itemBean.songURL}> </li><br>
            <p> Lyrics: ${itemBean.desc}</p><br>
            <form action="feedback.jsp" method="post">

                <input type="text" name="song"/>  <br />

                <input type="text" name="artist"/><br />
                    <button type="submit" id="save"> Save! </button>
                <button type="submit" id="rate"> Rate It</button>

            </form>

        </ul>

    </div>

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

Collection Java:Array包含所需信息的JavaBeans列表。

 package my.pack;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import java.util.*;
/**
 *
 * @author Anastasia
 */
public class collection{
     private List<itemBean> Song;
    public collection(){
    Song = new ArrayList<>();
    //String itemCode;

      //itemBean item = new itemBean();
     Song.add(new itemBean("S1", "View","SHINee","Song",
     "모두 할 말을 잃지 like you<br>\n,
        "","/image/viewShinee.jpg"));

     Song.add(new itemBean("S2","Pray","FTIsland","Song",
             "또 다른 현실 속에 속삭이던 많은 거짓말<br>\n",
             "","/image/pray_FTIsland.jsp"));

     Song.add(new itemBean("S3","X","Monsta X","Song",
     "Yeah ah yeah hey hey hey<br>\n" ,
     "","/image/X_MonstaX.jpg"));

      Song.add(new itemBean("OST3","Stay With Me", "Punch & Chanyeol(EXO)","OST"," 나의 두 눈을 감으면 <br>\n" ,
      "", "/image/GoblinOST.jpg"));

     Song.add(new itemBean("OST2","Before the Sunset--Goblin OST","Eric Name", "OST",
     "나즈막히 건넨 인사가<br>\n",
     "","/image/MadDogOST.jpg"));

    Song.add(new itemBean("OST3","You Are My Everything", "Gummy","OST",
     "처음부터 그대였죠<br>\n" ,
     "","/image/DOTS.jpg"));


}
    public List<itemBean> getItems(){
        return Song;
    }
}

1 个答案:

答案 0 :(得分:1)

我怀疑错误可能是您的EL中没有request.;你试过${request.itemBean.song}吗?

你应该真的:

  1. 使用小写字母
  2. 启动变量名称
  3. 以大写字母
  4. 启动班级名称
  5. 使用Map<String, ItemBean>来保存您的歌曲集
  6. 使用像Song这样有意义的名称而不是ItemBean