2016-09-26 52 views
2

我知道這個主題有很多線程,我已經嘗試了大部分線程,仍然無法修復我的問題。我正在使用SpringMVCMongoDB我是什麼試圖實現的是,我會將一些數據存儲在數據庫中,然後我將從數據庫中將其恢復爲選擇選項。這是我的代碼。BindingResult和bean name'categoryOptions'的無格式目標對象都不作爲請求屬性

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(); 
} 

DAO實現

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 

當我收到錯了嗎?我嘗試過不同問題的解決方案,但無法解決問題。

回答

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; 
    } 

例外是因爲<f:select path="categoryOptions">存在的,你提到的路徑categoryOptions但沒有你在哪裏returning這個jspcategoryOptions

更新:所以此說,每當你加載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> 
+0

看到這個「/ saveReferral」是也被稱爲現在有一個創建按鈕,將添加一些名稱數據庫和選擇選項應顯示在加名字的選項。 –

+0

你是什麼要求得到這個異常? 'create'? –

+0

當管理員登錄這個頁面時將會顯示,這一點只是它給這個例外 –

相關問題