JSP:如何使用JSTL更新bean

时间:2018-07-07 13:16:31

标签: jsp jstl javabeans

我正在尝试创建圣诞节购物清单jsp页面。

我需要检查是否已经存在PresentBean项,是否需要使用表单中的参数更新现有的PresentBean项。这样,就不应有重复的PresentBean

所以基本上,问题是如何更新我的bean?

1。PresentBean-存储项目,价格和人的bean。

 /*
 *
 * PresentBean.java
 * 
 */
package win.net;



/**
 *
 * @author aubrey - 
 */
public class PresentBean {

    private String item;
    private String person;
    private String price;

    //constructor
    public PresentBean(){
    item = new String();
    person = new String();
    price = new String();

    }

    /**
     * @return the item
     */
    public String getItem() {
        return item;
    }

    /**
     * @param item the item to set
     */
    public void setItem(String item) {
        this.item = item;
    }

    /**
     * @return the person
     */
    public String getPerson() {
        return person;
    }

    /**
     * @param person the person to set
     */
    public void setPerson(String person) {
        this.person = person;
    }

    /**
     * @return the price
     */
    public String getPrice() {
        return price;
    }

    /**
     * @param price the price to set
     */
    public void setPrice(String price) {
        this.price = price;
    }

}

2。ListBean-存储多个PresentBean的bean:

package win.net;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author aubrey
 */
public class ListBean {

private List<Object> list = new ArrayList<Object>();

public void setChild(Object object){
    list.add(object);
}
public List<Object> getList(){
    return list;
}

}

3。jsp页-收集表单参数并将其放入PresentBean中。然后将PresentBean放入列表中。最后,我们打印PresentBeans的列表:

<%-- 
    Document   : index
    Created on : 04-Jul-2018, 23:32:58
    Author     : aubrey
--%>

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Christmas</title>

    </head>
    <body>
        <div class="input">

        <h1>Christmas</h1>

        <jsp:useBean id="chris" class="win.net.PresentBean" />

                 <%-- Create the Bean --%>   
                 <%--jsp:useBean id='listBean' class='win.net.SetBean' scope='session'/--%>                 
                 <jsp:useBean id='listBean' class='win.net.ListBean' scope='session' />

                 <%-- Search for duplicates --%>
                 <c:forEach var="v" begin="0" items="${listBean.list}">
                     <c:if test="${v.item ne param.item}">
                         <% System.out.println("Not equal"); %>
                                <jsp:setProperty name="chris" property="item"/>
                                <jsp:setProperty name="chris" property="price"/>
                                <jsp:setProperty name="chris" property="person"/>


                     </c:if>

                     <c:if test="${v.item eq param.item}">
                         <% System.out.println("Equal"); %>

                     </c:if>
                 </c:forEach>


        <c:set target='${listBean}' property='child' value='${chris}'/>




        <form action="index.jsp" method="get">
            <table>
                <tr>
                    <td>Item:</td>
                    <td><input type="text" name="item" value="${param.item}" /></td>
                </tr>
                <tr>
                    <td>Price:</td>
                    <td><input type="text" name="price" value="${param.price}" /></td>
                </tr>
                <tr>
                    <td>Person:</td>
                    <td><input type="text" name="person" value="${param.person}" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Submit" /></td>
                </tr>
            </table>
                <br/>
        </form>
        </div>




                <div class="panel">



                 <%-- Print out the box links --%>
                 <c:forEach var="v" begin="0" items="${listBean.list}">
                     <c:if test="${not empty v.item}">

                <a href="index.jsp?item=${v.item}&person=${v.person}&price=${v.price}">         
                <div class="box">        
                 Item: <c:out value="${v.item}" /><br/>
                 Person: <c:out value="${v.person}" /><br/>
                 Price: &euro; <c:out value="${v.price}" /><br/>
                 <br/>
                </div>
                </a>
                     </c:if>
                 </c:forEach >


                </div>
    </body>
</html>
  1. 这是我要开始工作的代码。基本上,我进行测试以查看重复项是否存在。如果是这样,它应该更新bean。如果不是,则应将其添加到列表中。

首先,我创建两个bean。 PresentBean被称为“ chris”,ListBean被称为“ listBean”。但是无法从循环内部访问它们。

<jsp:useBean id="chris" class="win.net.PresentBean" />   


                 <jsp:useBean id='listBean' class='win.net.ListBean' scope='session' />

                 <%-- Search for duplicates --%>
                 <c:forEach var="v" begin="0" items="${listBean.list}">
                     <c:if test="${v.item ne param.item}">
                         <% System.out.println("Not equal"); %>
                                <jsp:setProperty name="chris" property="item"/>
                                <jsp:setProperty name="chris" property="price"/>
                                <jsp:setProperty name="chris" property="person"/>


                     </c:if>

                     <c:if test="${v.item eq param.item}">
                         <% System.out.println("Equal"); %>

                     </c:if>
                 </c:forEach>


        <c:set target='${listBean}' property='child' value='${chris}'/>

例如,如果我移动行:

<c:set target='${listBean}' property='child' value='${chris}'/>

在循环内失败。我认为问题在于我正试图添加到当前正在迭代的列表中。也可能是变量超出范围!那么如何在循环的不相等部分内调用此行呢?

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案。看来我正在遍历一个空列表。