简体   繁体   中英

Spring MVC — Handle multiple forms in a single jsp

I have been searching around for reason why I have error Neither BindingResult nor plain target object for bean name 'addItemForm' available as request attribute but can't understand.

The page is loading fine but if I click on Add new item button, a lightbox containing a form appears, I click on Add item and I got same error as above but bean name is searchForm .

The controller looks fine, I think there is a binding problem between the view and the form. Can someone explain please?

Also, it is able to add new items to database, so the POST method is working, but the view returns errors ..

JSP:

<%@ 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

This is the Controller:

package com.test.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.test.forms.AddNewItemForm;
import com.test.forms.SearchForm;
import com.test.models.items.ItemIF;
import com.test.transaction.TransactionFactory;

/**
 * Handles requests for Admin View page
 * @author Trung
 *
 */
@Controller
@RequestMapping (value = "/AdminView")
public class AdminViewController {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView adminForm(Model model){
        SearchForm searchForm = new SearchForm();
        model.addAttribute("searchForm", searchForm);

        AddNewItemForm addItemForm = new AddNewItemForm();
        model.addAttribute("addItemForm", addItemForm);

        return new ModelAndView("AdminView");
    }

    @RequestMapping (params = "search", method = RequestMethod.POST)
    public String processingSearchStore(@ModelAttribute("searchForm") SearchForm searchForm, Model model){
        List<ItemIF> relatedItems = null;
        TransactionFactory transaction = new TransactionFactory();
        relatedItems = transaction.retrieveItemByName(searchForm.getKeyWordSearch());

        if (relatedItems.isEmpty()){
            System.out.println("okay, there isn't any item that is matched your criteria");
        } else {
            model.addAttribute("itemList", relatedItems);
        }

        return "AdminView";
    }

    @RequestMapping(params = "add", method = RequestMethod.POST)
    public ModelAndView addNewItemForm(@ModelAttribute("addItemForm") AddNewItemForm addItemForm){
        TransactionFactory transaction = new TransactionFactory();
        String itemName = addItemForm.getItemName();
        String itemLocation = addItemForm.getItemLocation();
        String itemDescription = addItemForm.getItemDescription();

        transaction.insertItem(itemName, itemLocation, itemDescription);

        return new ModelAndView("AdminView");
    }
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM