0

I'm getting the following error when opening index.jsp: "java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute" Already read several forums and tried different solutions but issue still persists.

 package pizzapack;

 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.portlet.ModelAndView;
 import org.springframework.ui.ModelMap;

 @Controller
 public class PizzaController 
 {
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView pizza()
    {
       return new ModelAndView("index", "command", new Pizza());
    }

    @RequestMapping(value = "/addPizza", method = RequestMethod.POST)
    public String addPizza(@ModelAttribute("pizza")Pizza pizza, 
    ModelMap model)
    {
       model.addAttribute("size", pizza.getSize());

       return "result";
    }
 }

And index.jsp file:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Pizza welcome</title>
</head>
<body>

<h2>Pizza Information</h2>
<form:form method="POST" action="/Pizzaorder/addPizza">
   <table>
    <tr>
        <td><form:radiobutton path="meret" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>  
</form:form>
</body>
</html>

Thanks for the advices

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>Pizzaorder</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Pizzaorder</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

applicationContext.xml:

    <?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}" /-->

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

</beans>

Pizzaorder-servlet.xml:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd"

    <mvc:annotation-driven />    
    <context:component-scan base-package="pizzapack"/>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />


</beans>
Istvan
  • 73
  • 12
  • Show us your web.xml and Spring MVC configuration. Where is the JSP located? – Sotirios Delimanolis May 22 '15 at 20:54
  • Duplicate? http://stackoverflow.com/questions/21790656/java-lang-illegalstateexception-neither-bindingresult-nor-plain-target-object-f – hooknc May 22 '15 at 21:00
  • I also want to see your `Pizzaorder-servlet.xml` (or wherever you have your MVC configuration). – Sotirios Delimanolis May 22 '15 at 21:05
  • jsp files are in jsp folder under WEB-INF I simplified the code as much as possible just to see first how to resolve the index view – Istvan May 22 '15 at 21:08
  • Finally, which URL are you hitting? – Sotirios Delimanolis May 22 '15 at 21:11
  • http://localhost:8084/Pizzaorder/index – Istvan May 22 '15 at 21:14
  • Start by adding `` to your servlet.xml. – Sotirios Delimanolis May 22 '15 at 21:33
  • it resulted an error: "org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 18 in XML document from ServletContext resource [/WEB-INF/Pizzaorder-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 30; The prefix "mvc" for element "mvc:annotation-driven" is not bound." – Istvan May 22 '15 at 21:44
  • i tried to mimic the example on http://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm where basic code worked with the 'command' object – Istvan May 22 '15 at 21:45
  • You need the appropriate XML namespace declarations for `mvc`, just like you do for `context`. – Sotirios Delimanolis May 22 '15 at 21:57
  • sorry, didnt notice. already done. however another error comes: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 19 in XML document from ServletContext resource [/WEB-INF/Pizzaorder-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 5; Element type "beans" must be followed by either attribute specifications, ">" or "/>". – Istvan May 22 '15 at 22:04
  • updated servlet.xml (see in code snippet) – Istvan May 22 '15 at 22:04

0 Answers0