2012-12-21 11 views
6

sto ottenendo questo erroreNé BindingResult né oggetto di destinazione normale per il nome di fagioli disponibili come richiesta attributo - Spring MVC

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute 

Questa è l'analisi dello stack

org.apache.jasper.JasperException: java .lang.IllegalStateException: Né BindingResult né semplice oggetto di destinazione per il nome bean 'comando' disponibile come attributo di richiesta org.apache.jasper.servlet.JspServletWrapper.handleJspException (JspServletWrapper.java:534) org.apache.jasper.servlet.JspServletWrapper .Service (JspServletWrapper.jav a: 452) org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:389) org.apache.jasper.servlet.JspServlet.service (JspServlet.java:333) javax.servlet.http.HttpServlet .service (HttpServlet.java:722) cause java.lang.IllegalStateException: né BindingResult né oggetto target normale per nome bean 'comando' disponibile come richiesta attributo org.springframework.web.servlet.support.BindStatus . (BindStatus.java:141) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus (AbstractDataBoundFormElementTag.java:178) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPa esimo (AbstractDataBoundFormElementTag.java:198) org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor (LabelTag.java:129) org.springframework.web.servlet.tags.form.LabelTag.resolveFor ​​(LabelTag. java: 119) org.springframework.web.servset.tags.form.LabelTag.writeTagContent (LabelTag.java:89) org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal (AbstractFormTag.java:102) org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag (RequestContextAwareTag.java:79) org.apache.jsp.student_jsp._jspx_meth_form_005flabel_005f0 (student_jsp.java:182) org.apache.jsp.student_jsp._jspx_meth_form_005fform_005f0 (student_jsp .java: 117) org.apache.jsp.student_jsp._jspService (student_jsp.java:79) org.apache.jasper.runtime.HttpJspBase.service (HttpJspBase.java:70) javax.servlet.http.HttpServlet.service (HttpServlet.java:722) org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper .java: 419) org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:389) org.apache.jasper.servlet.JspServlet.service (JspServlet.java:333) javax.servlet.http. HttpServlet.service (HttpServlet.java:722)

Questo è il mio file web.xml

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

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 


    <display-name>Spring MVC Form Handling</display-name> 
    <welcome-file-list> 
    <welcome-file>student.jsp</welcome-file> 
    </welcome-file-list> 

    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 


    <servlet> 
     <servlet-name>HelloWeb</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/HelloWeb-Servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

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

</web-app> 

Questo è il mio file applicationContext.xml

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

    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <context:component-scan base-package="com.Prime" /> 
<mvc:annotation-driven/> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/JSP/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 



</beans> 

Questo è il mio file student.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<head> 
    <title>Spring MVC Form Handling</title> 
</head> 
<body> 

<h2>Student Information</h2> 
<form:form modelAttribute="SpringWeb" method="POST" action="/HelloWeb/addStudent" commandName="SpringWeb"> 
    <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> 

E questo è il mio StudentController.di file java

package com.Prime; 

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

@Controller 
public class StudentController { 

    @RequestMapping(value = "/student", method = RequestMethod.GET) 
    public ModelAndView student() { 
     return new ModelAndView("/student.jsp", "SpringWeb", new Student()); 
    } 

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST) 
    public String addStudent(@ModelAttribute("SpringWeb")Student student, 
    ModelMap model) { 
     model.addAttribute("name", student.getName()); 
     model.addAttribute("age", student.getAge()); 
     model.addAttribute("id", student.getId()); 

     return "result"; 
    } 
} 

Qualcuno mi può aiutare con questo errore .... Grazie in anticipo

risposta

3

Ho provato la configurazione e non ho ricevuto il tuo errore. Ma ho dovuto cambiare qualcosa.

viewResolver sta aggiungendo .jsp al nome della vista. Quindi secondo me si dovrebbe cambiare

return new ModelAndView("/student.jsp", "SpringWeb", new Student()); 

a

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

Quindi rimuovere jsp e la "/" prima di studente.

Quindi ha funzionato per me. Ma come ho detto mi hanno dato il vostro errore

+0

Lo stesso codice che sto usando ma l'errore non si sta risolvendo ... Sto anche ricevendo lo stesso errore. Qualcuno può aiutarmi con questo? – Brain

3

Edit:

It fratelli mi che i vostri eccezioni stati "nome del bean 'comando' ". Penso che tu abbia qualche problema di delusione, perché il codice che hai postato qui non corrisponde all'eccezione.


Rimuovere l'attributo commandName dal tag form, l'attributo modelAttribute è enought:

<form:form modelAttribute="SpringWeb" method="POST" action="/HelloWeb/addStudent" > 

Dopo aver letto questo article, cuciture che questa non è la soluzione al vostro problema, ma in ogni caso si fa Non è necessario specificarli due volte.

+0

Solo un'ipotesi, può essere a causa della presenza di entrambi i comandi 'commandName' e' modelAttribute' non è il binding a nessun oggetto e taglib assumendo il default modelAttribute denominato 'command'. – xyz

0

La posizione jsp la chiamata a /student sta cercando è

/WEB-INF/JSP//student.jsp.jsp 

Modificare la configurazione in modo che corrisponda all'URL corretto.

Problemi correlati