2016-04-02 4 views
1

この質問は尋ねられていて、さまざまな答えがありますが、Springではすべてが異なっています。 BindingResultもBean名用プレーン ターゲットオブジェクトのいずれも ':Spring 4/BindingResultでもプレーンターゲットオブジェクトでもない

java.lang.IllegalStateExceptionエラーにHelloWorldの実装となった -

私は春4で新しいです - MVSとちょうど私の最初のMVCを作成したいです要求属性 org.springframework.web.servlet.support.BindStatusとして使用可能なコマンド」。(BindStatus.java:141) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.get

マイウェブ.xmlは次のようになりますこの:

原型作成したWebアプリケーション HelloWorldの org.springframework.web.servlet.DispatcherServlet のHelloWorld / contextConfigLocation /WEB-INF/HelloWorld- servlet.xml org.springframework.web.context.ContextLoaderListener このような私のHelloWorld-servlet.xml:
<context:component-scan base-package="com.programcreek.helloworld.controller" /> 

<bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

私student.jspは、次のようになります。

 <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<head> 
    <title>Spring MVC Form Handling</title> 
</head> 
<body> 

<h2>2 Student Information 2</h2> 


<form:form method="POST" action="/HelloWorld/addStudent" > 
    <table> 
    <tr> 
     <td><form:label path="name">Name</form:label></td> 
     <td><form:input path="name" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="age">Age</form:label></td> 
     <td><form:input path="age" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="id">id</form:label></td> 
     <td><form:input path="id" /></td> 
    </tr> 
    <tr> 
     <td colspan="2"> 
      <input type="submit" value="Submit"/> 
     </td> 
    </tr> 
</table> 
</form:form> 
</body> 
</html> 

と次のように私のStudentController.java:私はいつも試すために私の日食に例と転送サンプルコードをたくさん読ん

package com.programcreek.helloworld.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.validation.BindingResult; 
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; 

@Controller 
@RequestMapping("/") 
public class StudentController { 

    @RequestMapping(value = "/student", method = RequestMethod.GET) 
    public ModelAndView student() { 

     return new ModelAndView("student", "command2", new Student()); 
    } 



    @RequestMapping(value = "/addStudent", method = RequestMethod.POST) 
    public String addStudent(@ModelAttribute("HelloWorld")Student student, 
    ModelMap model) { 

     System.out.println("Controller2"); 
     model.addAttribute("name", student.getName()); 
     model.addAttribute("age", student.getAge()); 
     model.addAttribute("id", student.getId()); 

     return "result"; 
    } 
} 

が、用古い例(Spring 2.4)から取り上げられたこのケースの学生。

問題を把握していただきありがとうございます。

答えて

1

は、私は私のシステム上でコードを実行している(私は春-ドキュメントについて知っている)

あなたは「そう

return new ModelAndView("student", "command2", new Student()); 

あなたがマップする必要があり、あなたのコントローラクラスから戻ってきていますcommand2 "をcommandNameに という形で入力します。JSP

コマンド名、プロジェクトの

<form:form method="POST" commandName="command2" action="/HelloWorld/addStudent" > 

完全なソースコードの下のようなあなたのstudent.jsp中= "コマンド2":

student.java

package com.programcreek.helloworld.controller; 

public class Student { 
     private Integer age; 
     private String name=""; 
     private Integer id; 
     public Student(){ 

     } 
     public void setAge(Integer age) { 
      this.age = age; 
     } 
     public Integer getAge() { 
      return age; 
     } 

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

     public void setId(Integer id) { 
      this.id = id; 
     } 
     public Integer getId() { 
      return id; 
     } 
    } 

StudentController.java

package com.programcreek.helloworld.controller; 

    import org.springframework.stereotype.Controller; 
    import org.springframework.ui.ModelMap; 
    import org.springframework.validation.BindingResult; 
    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; 

    @Controller 
    @RequestMapping("/") 
    public class StudentController { 

     @RequestMapping(value = "/student", method = RequestMethod.GET) 
      public ModelAndView student() { 

       return new ModelAndView("student", "command2", new Student()); 
      } 



      @RequestMapping(value = "/addStudent", method = RequestMethod.POST) 
      public ModelAndView addStudent(@ModelAttribute("HelloWorld")Student student) { 
       ModelAndView modelAndView = new ModelAndView(); 

       System.out.println("Controller2"); 
       modelAndView.addObject("name", student.getName()); 
       modelAndView.addObject("age", student.getAge()); 
       modelAndView.addObject("id", student.getId()); 
       modelAndView.setViewName("result"); 


       return modelAndView; 
      } 
     } 

のweb.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0"> 



    <servlet> 
     <servlet-name>DefaultServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>DefaultServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 


</web-app> 

デフォルトサーブレット-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:component-scan base-package="com.programcreek.helloworld.controller" /> 
    <!-- <context:component-scan base-package="com.packt.webstore.domain.repository.impl" /> 
    <context:component-scan base-package="com.packt.webstore.service.impl" /> --> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 


</beans> 

student.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 

<html> 
<head> 
    <title>Spring MVC Form Handling</title> 
</head> 
<body> 

<h2>2 Student Information 2</h2> 


<form:form method="POST" commandName="command2" action="addStudent" > 
    <table> 
    <tr> 
     <td><form:label path="name">Name</form:label></td> 
     <td><form:input path="name" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="age">Age</form:label></td> 
     <td><form:input path="age" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="id">id</form:label></td> 
     <td><form:input path="id" /></td> 
    </tr> 
    <tr> 
     <td colspan="2"> 
      <input type="submit" value="Submit"/> 
     </td> 
    </tr> 
</table> 
</form:form> 
</body> 
</html> 

ディレクトリ構造:

directory structure

実行結果

execution result

+0

こんにちは...あなたが私の答えにdownvoteを与え、なぜ私はこのコードを実行し、同じ例外 java.lang.IllegalStateExceptionを得た:Bean名「コマンド」のBindingResultもプレーンなターゲットオブジェクトどちら をコマンド名= "コマンド2を追加した後"student.jspでうまく動作しました すべてのXMLとJavaクラスの完全なソースコードを貼り付けることができます –

+0

こんにちはKunal、私はあなたにdownvoteを与えませんでした。私はあなたの助けに感謝しています。できます。ありがとう – Timo

+0

私の喜び:-)それがあなたを助けた場合は、この回答を受け入れることができます。ありがとう:) –

関連する問題