Spring MVC - 在单个jsp中处理多个表单

时间:2015-12-02 16:54:55

标签: java spring jsp spring-mvc

我一直在寻找错误Neither BindingResult nor plain target object for bean name 'addItemForm' available as request attribute但我无法理解的原因。

页面正常加载但是如果我点击Add new item按钮,会出现一个包含表单的灯箱,我点击Add item我得到了与上面相同的错误,但是bean名称是{{1} }。

控制器看起来很好,我认为视图和表单之间存在绑定问题。有人可以解释一下吗?

此外,它能够将新项添加到数据库,因此POST方法正常工作,但视图返回错误..

JSP:

searchForm

这是控制器:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Admin View</title>
    <style type="text/css">
        body{margin:0, padding:0}
        #body{
            text-align: center;
            top: 3%;
        }           

        .table2{
            border-spacing: 5%;
        }

        .table1 {
            border-collapse: collapse;          
            width: 80%;
        }

        .table1 tr:nth-child(odd) td{
            background-color: #ffffff;
        }
        .table1 tr:nth-child(even) td{
            background-color: #4da6ff;
        }   

        .table1 td, th {
            text-align: left;
            border: 1px solid green;
        }

        .black_overlay{
            display: none;
            position: absolute;
            top: 0%;
            left: 0%;
            width: 100%;
            height: 100%;
            background-color: black;
            z-index:1001;
            -moz-opacity: 0.8;
            opacity:.80;
            filter: alpha(opacity=80);
        }
        .white_content {
            display: none;
            position: absolute;
            top: 25%;
            left: 25%;
            width: 50%;
            height: 50%;
            padding: 16px;
            border: 16px solid orange;
            background-color: white;
            z-index:1002;
            overflow: auto;
        }           
    </style>

</head>
<body>
    <div id="body">
        <h3>This is the Administrator View</h3>
        <p>From here, you can search for items in the database, view, add new items and delete items from database</p>

        <table align="center">
            <tr>
                <td>
                    <form:form action="AdminView" method="POST" commandName="searchForm">
                        <form:input path="KeyWordSearch" size="40"/>
                        <input type="submit" name="search" value="Search Store"/>                                       
                    </form:form>
                </td>
                <td><button onclick = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">Add new item</button></td>             
            </tr>           
        </table>                
        <br>
        <c:if test="${not empty itemList}">
            <table class="table1" align="center">
                <tr>
                    <th>Item Name</th>
                    <th>Date added</th>                 
                    <th>Item Description</th>
                    <th>View Details</th>
                </tr>

                <c:forEach var="item" items="${itemList}">
                    <tr>
                        <td>${item.itemName}</td>
                        <td>${item.itemAdded}</td>
                        <td>${item.itemDescription}</td>
                        <td><a href="/myapp/DisplayItemDetailsForAdmin?itemId=${item.itemId}">View</a></td>
                    </tr>
                </c:forEach>                
            </table>
        </c:if>
    </div>  

    <div id="light" class="white_content">
        <h2 align="center">Add new item to Store</h2>
        <form:form action="AdminView" method="POST" commandName="addItemForm">
            <table align="left">
                <tr>
                    <th style="border:0">Item name</th>
                    <td><form:input path="ItemName"/></td>
                </tr>
                <tr>
                    <th style="border:0">Item location</th>
                    <td><form:input path="ItemLocation"/></td>
                </tr>
                <tr>
                    <th style="border:0">Item Description</th>
                    <td><form:textarea path="ItemDescription"/></td>
                </tr>
                <tr>
                    <td><input type="submit" name="add" value="Add Item"/></td>
                </tr>
            </table>
        </form:form> 
        <a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'">Close</a>
    </div>
    <div id="fade" class="black_overlay"></div>     
</body>
</html

1 个答案:

答案 0 :(得分:-1)

BindingResult参数之后的post个方法中,您需要@ModelAttribute作为参数,如下所示:

public String processingSearchStore(@ModelAttribute("searchForm") SearchForm searchForm, BindingResult result, Model model){