Java bean name';的BindingResult或普通目标对象;类别选项';可用作请求属性

Java bean name';的BindingResult或普通目标对象;类别选项';可用作请求属性,java,mongodb,spring-mvc,Java,Mongodb,Spring Mvc,我知道在这个主题上有很多线程,我已经尝试了大多数线程,但仍然无法解决我的问题。我正在使用SpringMVC和MongoDB我试图实现的是,我将在数据库中存储一些数据,然后将其从数据库检索回所选选项。这是我的密码 Jsp页面.. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http:/

我知道在这个主题上有很多线程,我已经尝试了大多数线程,但仍然无法解决我的问题。我正在使用
SpringMVC
MongoDB
我试图实现的是,我将在数据库中存储一些数据,然后将其从数据库检索回所选选项。这是我的密码

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);
    }
}
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
路径为[/LoginMavenSpringMVC]的上下文中Servlet[spring dispatcher]的
Servlet.service()引发异常[处理第366行的JSP页面/WEB-INF/views/referralPage.JSP时发生异常]
363:             
364:              
365:              
366:              
367:                
368:              

369:您缺少向模型添加引用对象

    @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;
    }
出现异常是因为
,您在路径中提到了
类别选项
,但没有提到您将
返回
jsp
类别选项

更新:因此,无论何时加载
参考jsp
,都必须使用
类别选项bean加载

在下面几行中,使用
model.addObject()
将列表添加到模型中,但缺少路径变量
categoryOptions
。因此,在
model.addObject(“list”,list);
add
model.addAttribute(“categoryOptions”,new references());

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


请参见此“/saveReferral”现在也没有被调用。有一个创建按钮,它将向数据库添加一些名称,选择选项应在其选项中显示添加的名称。根据什么请求,您将获得此异常?
create
?当显示此页面中的管理员日志时,这一点仅给出此异常的解释。