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



我正在尝试访问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:包含所需信息的 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;
    }
}

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

你真的应该:

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

最新更新