【问题标题】:Neither BindingResult nor plain target object for bean name 'categoryOptions' available as request attributeBean 名称“categoryOptions”的 BindingResult 和普通目标对象都不能用作请求属性
【发布时间】:2016-09-26 06:04:11
【问题描述】:

我知道这个主题有很多线程我已经尝试了大部分,但仍然无法解决我的问题。我正在使用SpringMVCMongoDB 我想要实现的是,我将在数据库中存储一些数据然后我会将它从数据库中检索到一个选择选项。这是我的代码。

Jsp页面..

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="f"%>
    <%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!DOCTYPE html>
<html lang="en">
    <title>Master Referral</title>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width= device-width, initial-scale=1">
        <link type="text/css" rel="stylesheet" href='<spring:url value="/resources/css/bootstrap.min.css" />' >
        <link type="text/css" rel="stylesheet" href='<spring:url value="/resources/css/stylesvitalbeat.css" />' >


    </head>
    <body>
 <div class="container-fluid">
<div class="row">
                 <form action="http://localhost:8080/LoginMavenSpringMVC/admin/create" method="post">
                 <div class="col-md-2 col-sm-3">
                     <label class="control-label">Create Category:</label></div>
                     <div class="col-md-2 col-sm-4">
                   <input type="text" class="form-control input-sm field_color"  name="categoryName" placeholder="Name of the Category">
                         </div>
                     <div class="col-md-1 col-sm-3">
                <input type="submit" class="btn btn-primary btn-sm   height_margin"  name="create" value="Create">
                     </div>
                      </form>
<div>
<form class="form-horizontal" action="http://localhost:8080/LoginMavenSpringMVC/admin/saveReferral" method="post">
         <div class="row margin_div">
            <div class="col-sm-3 col-md-2">
                <label class="control-label">Select Category</label>
            </div>
             <div class="col-sm-5 col-md-4">
             <f:select path="categoryOptions">
            <f:options items="${list}"/>
         </f:select>
                              </div>

                </div>

</div>

控制器类

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
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.java.Dao.services.RegisterDao;
import com.java.Dao.services.RegisterDaoImplementaion;

@Controller
@RequestMapping("/admin")
public class ReferralController {

    @Autowired
    RegisterDao registerDao;

    @RequestMapping(value="/create", method=RequestMethod.POST)
    public ModelAndView create(@ModelAttribute("create") Category create){
        ModelAndView model =new ModelAndView("referralPage");
        System.out.println("Referral Controller.");     
    System.out.println( create.getCategoryName());
    if((StringUtils.hasText(create.getId()))) {
        registerDao.UpdateCategory(create);
    } else {
        registerDao.addCategory(create);
    }
    List<Category> list= registerDao.categoryList();
    model.addObject("list", list);
   return model;
    }

    @RequestMapping(value="/saveReferral", method=RequestMethod.POST)
    public ModelAndView save(@ModelAttribute("saveReferral") Referrals referral){
        ModelAndView model=new ModelAndView("referralPage");        
        return model;
    }
}  

道服务

dao class...
package com.java.Dao.services;

import java.util.List;
import com.java.Package.Login.Category;


public interface RegisterDao {
public void addCategory(Category createCategory);
    public void UpdateCategory(Category createCategory);
    public List<Category> categoryList();
}  

道实现

import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;
import com.java.Package.Login.Category;
import com.mongodb.DBCollection;


@Repository
public class RegisterDaoImplementaion implements RegisterDao {

    @Autowired
    private MongoTemplate mongoTemplate;
    public static final String Collection_Category="CategoryList";
public void addCategory(Category createCategory) {
        // TODO Auto-generated method stub

        createCategory.setId(UUID.randomUUID().toString());
        System.out.println("Object in Repos::"+createCategory);
        mongoTemplate.insert(createCategory, Collection_Category);
    }
    public void UpdateCategory(Category createCategory) {
        // TODO Auto-generated method stub
        mongoTemplate.insert(createCategory, Collection_Category);      
    }
    @Override
    public List<Category> categoryList() {      
        return mongoTemplate.findAll(Category.class, Collection_Category);
    }
}

类映射 categoryOptions

public class Referrals {
    private String categoryOptions;

    public String getCategoryOptions() {
        return categoryOptions;
    }

    public void setCategoryOptions(String categoryOptions) {
        this.categoryOptions = categoryOptions;
    }
}

我收到了这个错误日志

Servlet.service() for servlet [spring-dispatcher] in context with path [/LoginMavenSpringMVC] threw exception [An exception occurred processing JSP page /WEB-INF/views/referralPage.jsp at line 366

363:             </div>
364:              <div class="col-sm-5 col-md-4">
365:              
366:              <f:select path="categoryOptions">
367:                <f:options items="${list}"/>
368:              </f:select>
369:             <!--  <select class="form-control input-sm" name="categoryOptions" >


Stacktrace:] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'categoryOptions' available as request attribute

我哪里出错了?我尝试了不同问题的解决方案,但无法解决。

【问题讨论】:

    标签: java mongodb spring-mvc


    【解决方案1】:

    您缺少将引用对象添加到模型。

        @RequestMapping(value="/saveReferral", method=RequestMethod.POST)
        public ModelAndView save(@ModelAttribute("saveReferral") Referrals referral){
            ModelAndView model=new ModelAndView("referralPage");     
            model.addAttribute("categoryOptions",new Referrals());   //or referral
            return model;
        }
    

    由于&lt;f:select path="categoryOptions"&gt; 而发生异常,您在路径中提到了categoryOptions,但在jspcategoryOptions 中没有提到returning

    更新:所以这意味着每当你加载referral jsp,你必须加载categoryOptions bean

    在下面的行中,使用model.addObject() 将列表添加到模型中,但缺少路径变量categoryOptions。所以在model.addObject("list", list);之后添加model.addAttribute("categoryOptions", new Referrals());

    <f:select path="categoryOptions">
       <f:options items="${list}"/>
    </f:select>
    

    【讨论】:

    • 看到这个“/saveReferral”现在也没有被调用,有一个创建按钮,它将向数据库添加一些名称,并且选择选项应该在其选项中显示添加的名称。
    • 您收到此异常的请求是什么? create??
    • 当这个页面的管理员日志会显示出来的时候,只有这点给出了这个异常
    • 感谢您的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 2015-08-31
    • 2013-04-04
    • 2011-11-25
    • 2019-10-29
    • 2013-08-15
    相关资源
    最近更新 更多