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

标签 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" %>

Java 集合:包含所需信息的 JavaBean 数组列表。

 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;
    }
}

最佳答案

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

你真的应该:

  1. 变量名以小写字母开头
  2. 类名以大写字母开头
  3. 使用 Map<String, ItemBean>保存您的歌曲 Collection
  4. 使用有意义的名称,例如 Song,而不是 ItemBean

关于java - 是否可以将 HTML 参数与列表中的 Java Bean 变量进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49213651/

相关文章:

java - 检查一个数字是否是两个数字的幂的函数的时间复杂度

java - java中未排序数组列表中的最小元素

java - Arraylist 分为 -

arraylist - 将 ArrayList 转换为 JavaFX 程序的可观察列表?

组合两个 Set 时出现 java.lang.UnsupportedOperationException

java - 这两种将集合转换为数组对象的方法的区别

java - 遍历 lazyDataModel

java - 如何避免 Java 反射中的魔术字符串

java - Math rand 的这种改组是如何进行的?

java - RDD另存为文本文件