2016-04-02 21 views
1

Bu sorunun farklı yollarla sorulmuş ve yanıtlandığını biliyorum, ancak Bahar ile her şey farklı. BindingResult ne de fasulye adı için düz hedef nesne Ne ':Yay 4/Ne Bağlama Sonuç ne de düz hedef nesne

java.lang.IllegalStateException hatayı MerhabaDünya uygulama ve got -

Ben Bahar 4 ile yeniyim - MVS ve sadece benim ilk MVC oluşturmak istiyorum istek özniteliği org.springframework.web.servlet.support.BindStatus olarak kullanılabilen komut'. (BindStatus.java:141) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.get

My web .xml benziyor Bu:

Archetype düzenlendi Web Uygulaması MerhabaDünya org.springframework.web.servlet.DispatcherServlet MerhabaDünya / contextConfigLocation /WEB-INF/HelloWorld- servlet.xml Böyle org.springframework.web.context.ContextLoaderListener Benim 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> 

Benim student.jsp şöyle görünür:

 <%@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> 

ve aşağıdaki gibi benim StudentController.java: Hep denemek için benim Tutulma için örnek ve transfer örnek kodları çok okumak

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

ama için Eski bir örneklemden alınmış olan bu öğrenci vakası (Bahar 2.4).

Sorunları çözmemde bana yardımcı olduğunuz için teşekkür ederiz.

cevap

1

benim sistemde kodunuzu idam (Bahar-dokümantasyon hakkında bilmek)

siz "bu yüzden

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

eşlemek var Denetleyiciniz sınıfından dönüyor command2 "komutu ile öğrenci formunda.Projenizin aşağıda

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

tam kaynak kodu gibi student.jsp içinde jsp

CommandName = "command2":

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> 

DefaultServlet-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> 

dizin yapısı:

directory structure

yürütme sonucu

execution result

+0

hi ... i bu kodu yürütülür ve aynı istisna var java.lang.IllegalStateException benim cevap downvote vermek neden: BindingResult ne de CommandName = "Command2'ı ekledikten sonra fasulye adı 'komutuyla' için düz hedef nesne Ne "student.jsp çalıştığı iyi her xml ve java sınıf –

+0

Hi Kunal tam kaynak kod yapıştırabilirsiniz, ben herhangi bir downvote vermedim. Yardımın için minnettarım. İşe yarıyor. Teşekkürler – Timo

+0

Benim zevkim :-) Size yardımcı olursa bu cevabı kabul edebilirsiniz. Teşekkürler :) –

İlgili konular