2016-09-26 7 views
2

このトピックには多くのスレッドがありますが、私はそれらのほとんどを試しましたが、まだ問題は解決できません。私はSpringMVCMongoDBを使用しています達成しようとすると、私はデータベースにいくつかのデータを格納し、私はそれをデータベースから選択オプションに戻して取得します。ここに私のコードです。Bean名 'categoryOptions'のBindingResultもプレーンターゲットオブジェクトもリクエスト属性として利用できません

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 

を取得していますか?私は別の質問からの解決策を試しましたが、解決できませんでした。

答えて

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を言及しているがありません、あなたはcategoryOptionsこのjspreturningているところ。

更新:これは、あなたがreferral jspをロードされるたび、あなたはcategoryOptions bean

とし、以下の行リストにロードする必要が言うmodel.addObject()を使用してモデルに追加されますが、パス変数categoryOptionsがありません。だから、ラインmodel.addObject("list", list);後、この「/ saveReferral」であるにも今、その中に追加した名前を表示する必要があり、データベースおよび選択オプションにいくつかの名前が追加されます作成ボタンがあると呼ばれて参照model.addAttribute("categoryOptions", new Referrals());

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

を追加オプション。 –

+0

どのようなリクエストでこの例外が発生していますか? '作成'? –

+0

このページの管理者ログが表示されるとき、この点だけがこの例外を示しています –

関連する問題