java.lang.IllegalStateException:BindingResult和bean名称'command'的普通目标对象都不能作为请求属性使用

时间:2013-10-23 20:32:13

标签: java spring spring-mvc shopping-cart cart

我正在学习Spring MVC,我正在尝试将列表中的项目添加到我的购物车中。我网站上的其他所有内容似乎都运行正常,但自从我在程序中添加了一个购物车后,它一直在给我以下错误消息:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute

我得到了上述莫名其妙的错误1)我没有名为“command”的请求属性,2)我不确定BindingResult是什么或做什么,说实话。我在Spring MVC上购买的书太过密集,无法让我完全掌握(Pro Spring MVC:With Web Flow)。

任何帮助将不胜感激。提前谢谢。

SearchFilmsController.java

@Controller
@SessionAttributes({ "categoryMap", "ratingMap", "customer" })
public class SearchFilmsController
{
    private static final Logger logger = LoggerFactory.getLogger(SearchFilmsController.class);

    @InitBinder
    public void initListBinder(WebDataBinder binder)
    {
    binder.setAutoGrowCollectionLimit(1000);
    }

    LoginValidator loginValidator;

    @Autowired
    public void setHomeController(LoginValidator loginValidator)
    {
    this.loginValidator = loginValidator;
    }

    @Autowired
    IUserBO userBO;

    @Autowired
    ICustomerBO    customerBO;

    @Autowired
    IFilmBO filmBO;

    @Autowired
    ICategoryBO    categoryBO;

    @Autowired
    SessionFactory sessionFactory;

    @Autowired
    IFilmInfoBO    filmInfoBO;

    /**
     * Simply selects the home view to render by returning its name.
     */

    @RequestMapping(value = "displaySearchFilms", method = RequestMethod.GET)
    public String displaySearchFilms(Model model, @ModelAttribute("categoryMap") Map<Short, String> categoryMap, @ModelAttribute("ratingMap") Map<String, String> ratingMap)
    {
    List<FilmInfo> filmInfos = filmInfoBO.getAll("title");
    FilmSearchFormModel filmSearchFormModel = new FilmSearchFormModel(filmInfos);
    filmSearchFormModel.setCategoryMap(categoryMap);
    filmSearchFormModel.setRatingMap(ratingMap);
    model.addAttribute("filmSearchFormModel", filmSearchFormModel);
    logger.info("\t" + filmInfos.size());
    return "searchFilms";
    }

    @RequestMapping(value = "returnSearchFilms", method = RequestMethod.POST)
    public String returnSearchFilms(@ModelAttribute("filmSearchFormModel") FilmSearchFormModel filmSearchFormModel, @ModelAttribute("categoryMap") Map<Short, String> categoryMap,
        @ModelAttribute("ratingMap") Map<String, String> ratingMap, @ModelAttribute("selectableFilm") SelectableFilm selectableFilm, Model model, BindingResult result)
    {
    logger.warn("I'm in search films - you know any good ones?");
    Map<String, Object> searchCriteria = new LinkedHashMap<>();

    searchCriteria.put("title", filmSearchFormModel.getTitle());
    searchCriteria.put("category", filmSearchFormModel.getCategory());
    searchCriteria.put("rating", filmSearchFormModel.getRating());
    List<FilmInfo> filmsReturned = filmInfoBO.searchFilmInfo(searchCriteria);
    ArrayList<SelectableFilm> selectableFilms = new ArrayList<>();
    for (FilmInfo filmInfo : filmsReturned)
    {
        selectableFilms.add(new SelectableFilm(filmBO.findById(filmInfo.getFilmId())));
    }

    FilmSearchFormModel filmSearchFormModel2 = new FilmSearchFormModel(filmsReturned);

    // FilmSearchFormModel filmSearchFormModel2 = new
    // FilmSearchFormModel(selectableFilms);
    filmSearchFormModel2.setTitle(filmSearchFormModel.getTitle());
    filmSearchFormModel2.setCategory(filmSearchFormModel.getCategory());
    filmSearchFormModel2.setRating(filmSearchFormModel.getRating());
    filmSearchFormModel2.setCategoryMap(categoryMap);
    filmSearchFormModel2.setRatingMap(ratingMap);

    // filmSearchFormModel.setSelectableFilms(filmInfoBO.searchFilmInfo(searchCriteria));
    model.addAttribute("filmSearchFormModel", filmSearchFormModel2);

    logger.info("\t" + filmsReturned.size());
    return "searchFilms";
    }
}

searchFilms.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"   pageEncoding="ISO-8859-1"%>
<%@ page errorPage="noFilmsAvailable.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>            <!--    Required in order to show images -->
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">      -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
    <title>Search Films</title>
    <link rel="stylesheet" type="text/css" href="./resources/include/menu.css"/>
    <style>

</style>    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>

        $("checkbox").toggle(function(){$("td").css("bgcolor","green")});

        function changeFormAction(submitButton)
        {
            if(submitButton == "checkout") {
                // The window.alert can be used to determine if the if/else logic
                // is being performed properly in a call to this function.
/*              window.alert (submitButton) */
                document.forms[0].action='return';
            }
            else if(submitButton == "returnSearchFilms") 
/*              window.alert (submitButton) */
                document.forms[0].action='returnSearchFilms';
            else if(submitButton == "addToCart")
                document.forms[0].action='addToCart';
            else if(submitButton == "goHome")
                    document.forms[0].action="displayHomepage";
            else if(submitButton == "viewCart")
                document.forms[0].action='displayCart';

        }       

        var x=true;
        var a = [];

        function toggleIntoCart(filmId)
        {
                    if($.inArray(filmId,a) != -1)
                    {
                        var c = a.indexOf(filmId)
                        a.splice(c,1);
                        $("#infoOnCart").text("asdf");
                    }
                    else
                        a.push(filmId);

                    $("#infoOnCart").append(a);
        }

        function checkOut()
        {
            document.findElementById().InnerHTML()
        }
    </script>
</head>

<body>  
    <div id = "MainLayer">      
        <div id="Layer-Content">
        <h1>Search Films</h1>
        <h2 id="infoOnCart" background-color="blue" color="white">Items in cart: </h2>
        <br/><br/>  
        <form:form modelAttribute="filmSearchFormModel" method="POST" action="/sakila/returnSearchFilms">   
            <div>                   
                <form:label path="title" background-color="blue" color="white">Title:</form:label>
                <form:input path="title" />             
                <form:label path="category">Category:</form:label>
                <form:select path="category">
                    <form:options items="${filmSearchFormModel.categoryMap}" />
                </form:select>
                <form:label path="rating">Rating:</form:label>
                <form:select path="rating" >
                    <form:options items="${filmSearchFormModel.ratingMap}" />
                </form:select>
            </div>
            <br />
            <div>
                <input type="submit" value="Search" onclick="changeFormAction('returnSearchFilms')" />
                <input type="submit" value="Add Selected Items to Cart" onclick="changeFormAction('addToCart')"/>
                <input type="submit" value="View Cart" onclick="changeFormAction('viewCart')"/>
            </div>
            <table>                 
            <th>Title</th><th>Length (minutes)</th><th>Rating</th><th>Price ($)</th>
                <c:forEach items="${filmSearchFormModel.filmInfos}" var="filmInfo" varStatus="sfStatus">
                    <tr>
                        <td><form:input path="selectableFilms[${sfStatus.index}].film.title" readonly="true"/></td>                     
                        <td><form:input path="selectableFilms[${sfStatus.index}].film.length" readonly="true"/></td>                        
                        <td><form:input path="selectableFilms[${sfStatus.index}].film.rating" readonly="true"/></td>                        
                        <td><form:input path="selectableFilms[${sfStatus.index}].film.price" readonly="true"/></td> 
                        <td><form:checkbox path="selectableFilms[${sfStatus.index}].selected" /></td>
                        <td><form:hidden path="selectableFilms[${sfStatus.index}].film.filmId"/></td>
                    </tr>
                </c:forEach>
            </table>

            <div>
                <input type="button" type="reset" value="Reset"/>
                <input type="button" value="Return to Home Page" type="submit"  onclick="changeFormAction('goHome')" />
            </div>

        </form:form>
        </div>          
    </div>          

</body>
</html>

哦......作为P.S.,如果有人知道更清洁,更简单的方法来完成所有这些添加物品到购物车的业务,请成为我的客人并告诉我。

0 个答案:

没有答案