2017-12-28 15 views
2

私はデータベースに新しい役割エンティティを追加するためのフォームを作成していて、私は次のようなエラーに遭遇しています:春|休止状態| Thymeleaf:BindingResultもBean名要求属性として利用できる「ID」のためのプレーンなターゲットオブジェクトどちら

Neither BindingResult nor plain target object for bean name 'id' available as request attribute 

IをStackOverflowの同じエラーのためのいくつかのソリューションを試しましたが、どれもうまくいきませんでした。

Role.java

@Entity 
public class Role { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id; 

    @NotNull 
    private String name; 

    @OneToMany(mappedBy = "role") 
    private List<Collaborator> collaborators = new ArrayList<>(); 

    @ManyToMany(mappedBy = "roles") 
    private List<Project> projects = new ArrayList<>(); 

    public Role() { 
    } 

    public Long getId() { 
    return id; 
    } 

    public void setId(Long id) { 
    this.id = id; 
    } 

    public String getName() { 
    return name; 
    } 

    public void setName(String name) { 
    this.name = name; 
    } 

    public List<Collaborator> getCollaborators() { 
    return collaborators; 
    } 

    public void setCollaborators(List<Collaborator> collaborators) { 
    this.collaborators = collaborators; 
    } 

    public List<Project> getProjects() { 
    return projects; 
    } 

    public void setProjects(List<Project> projects) { 
    this.projects = projects; 
    } 
} 

RoleController.java

@Controller 
public class RoleController { 
    @Autowired 
    private RoleService roleService; 

    @GetMapping("/roles") 
    public String listRoles(Model model) { 
    List<Role> roles = roleService.findAll(); 
    model.addAttribute("roles", roles); 
    return "role/roles"; 
    } 

    @PostMapping("/roles") 
    public String addCategory(@Valid Role role, BindingResult result, RedirectAttributes redirectAttributes) { 
    if(result.hasErrors()) { 
     redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.category", result); 
     redirectAttributes.addFlashAttribute("role", role); 
     return "redirect:/roles"; 
    } 
    roleService.save(role); 
    redirectAttributes.addFlashAttribute("flash", new FlashMessage("Role successfully added!", FlashMessage.Status.SUCCESS)); 
    return "redirect:/roles"; 
    } 
} 

roles.html

<!DOCTYPE html> 
<html> 
    <head th:replace="layout :: head('roles')"></head> 
    <body> 
     <header th:replace="layout :: header"></header> 
     <nav th:replace="layout :: nav"></nav> 
     <div th:replace="layout :: flash"></div> 
     <section> 
      <div class="container wrapper"> 
       <form> 
        <h2>Manage Roles</h2> 
        <div> 
         <ul class="checkbox-list"> 
          <li th:each="role : ${roles}"><span class="primary" th:text="${role.name}">Role Name</span></li> 
         </ul> 
        </div> 
        <form th:object="${role}" th:action="@{/roles}" method="post" class="actions add-new-role"> 
         <input type="hidden" th:field="*{id}"/> 
         <input type="text" th:field="*{name}" placeholder="Role Name"/> 
         <input type="submit" value="Submit" class="button"/> 
        </form> 
       </form> 
      </div> 
     </section> 
    </body> 
</html> 
:以下のように私の役割モデルとコントローラのクラスだけでなく、それに関連するテンプレートがあります

スタックトレース:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'id' available as request attribute 
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:401) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:328) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:294) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:98) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.Node.processNode(Node.java:972) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.dom.Document.process(Document.java:93) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1155) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] 
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131] 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.23.jar:8.5.23] 
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131] 

EDIT:問題の解決にご協力いただきありがとうございます。

+0

あなたがIDを削除すると、それはform.html役割/

<!DOCTYPE html> <html> <head th:replace="layout :: head('roles')"></head> <body> <header th:replace="layout :: header('Role', '/roles/add')"></header> <div th:replace="layout :: flash"></div> <nav th:replace="layout :: nav"></nav> <section> <div class="container wrapper"> <form> <h2>Manage Roles</h2> <div> <ul class="checkbox-list"> <li th:each="role : ${roles}"><span class="primary" th:text="${role.name}">Role Name</span></li> </ul> </div> <br> </form> </div> </section> <div th:replace="layout :: scripts"></div> </body> </html> 

index.htmlをワーキング? –

+0

th:field = "* {id}"でこのエラーが発生する可能性があります。 –

+0

IDフィールドを削除すると、名前フィールドが問題の原因になります。BindingResultもBean名 'name'また、私はそこにIDフィールドが必要ですが、自動生成された値として隠されています。 –

答えて

-1

2つのテンプレート、役割のインデックスと新しい役割を追加するためのフォームがありました。私は3つのコントローラメソッドも持っています:2つはメソッドを取得する(インデックスのために1つ、フォームのために1つ)、そして1つのポストメソッド(新しいロールを追加するため)です。

RoleController.java

@Controller 
public class RoleController { 
    @Autowired 
    private RoleService roleService; 

    // Index of roles 
    @GetMapping("/roles") 
    public String getRoles(Model model) { 
    List<Role> roles = roleService.findAll(); 
    model.addAttribute("roles", roles); 
    return "role/index"; 
    } 

    // New role form 
    @GetMapping("/roles/add") 
    public String newRoleForm(Model model) { 
    if(!model.containsAttribute("role")) { 
     model.addAttribute("role", new Role()); 
    } 
    model.addAttribute("action","/roles"); 
    model.addAttribute("heading","New Role"); 
    model.addAttribute("submit","Add"); 
    return "role/form"; 
    } 

    // Add new role 
    @PostMapping(value = "/roles") 
    public String addNewRole(@Valid Role role, BindingResult result, RedirectAttributes redirectAttributes) { 
    if(result.hasErrors()) { 
     redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.category",result); 
     redirectAttributes.addFlashAttribute("role", role); 
     return "redirect:/roles/add"; 
    } 
    roleService.save(role); 
    redirectAttributes.addFlashAttribute("flash", new FlashMessage("Role successfully added!", FlashMessage.Status.SUCCESS)); 
    return "redirect:/roles"; 
    } 
} 

役割/

<!DOCTYPE html> 
<html> 
<head th:replace="layout :: head('roles')"></head> 
<body> 
    <header> 
     <div class="container"> 
      <div class="site-header"> 
       <a class="logo" th:href="@{/}">InstaTeam</a> 
      </div> 
     </div> 
    </header> 
    <div th:replace="layout :: flash"></div> 
    <nav th:replace="layout :: nav"></nav> 

    <section> 
     <div class="container wrapper"> 
      <form th:action="@{${action}}" method="post" th:object="${role}"> 
       <h2 th:text="${heading}">New Role</h2> 
       <input type="hidden" th:field="*{id}"/> 
       <div class="actions add-new-role"> 
        <input type="text" th:field="*{name}" placeholder="Role Name"/> 
        <button th:text="${submit}" type="submit" class="button">Add</button> 
       </div> 
      </form> 
     </div> 
    </section> 

    <div th:replace="layout :: scripts"></div> 
</body> 
</html> 
-1

私はあなたがそれを使用する前にidがnullでなかったことを確認する必要があると思う、thymeleafは値がnullだったときに問題があります。

+0

こんにちはIDフィールドは生成された値です。それによって、@ NotNullアノテーションを追加する必要がありますか? –

関連する問題