Announcement Announcement Module
Collapse
No announcement yet.
Not able to pass data from jsp to controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Not able to pass data from jsp to controller

    Hello, I am starting on spring and have a basic application where some data needs to be passed from welcome page to controller. But I am getting an exception that the commandName is not binded. Here are my files. Can you please help?

    Welcome jsp pageindex.jsp)

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

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    import="com.tondigang.dao.impl.ListingDAOImpl"
    pageEncoding="ISO-8859-1"%>


    <html>
    <body>
    <h2>Search for a Rental</h2>
    <form:form action="displayListing.do" method="post" commandName="displayListing">
    Listing number: <form:input path="listingId" /><br />
    <input type="submit" value="Submit" />
    </form:form>
    </body>
    </html>
    Here is my controllerListingController.java)
    package com.tondigang;

    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;

    import javax.servlet.ServletException;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMap ping;
    import org.springframework.web.bind.annotation.RequestMet hod;
    import org.springframework.web.bind.annotation.RequestPar am;
    import org.springframework.web.servlet.ModelAndView;

    import com.tondigang.dao.impl.ListingDAOImpl;

    @Controller
    public class ListingController{

    @RequestMapping(value = "/displayListing.do", method = RequestMethod.POST)
    public ModelAndView displayListing(
    @RequestParam("displayListing") ListingDAOImpl listingDetails)
    throws ServletException, IOException {


    ListingDAOImpl listingDetailsDup = listingDetails;
    System.out.println(listingDetailsDup.listingId);
    return new ModelAndView("displayListing", "displayListing", listingDetailsDup);
    //return mav;
    }
    }
    Here is my spring file:

    <?xml version="1.0" encoding="UTF-8" ?>

    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

    <!--
    - DispatcherServlet application context for the Spring web MVC
    - implementation of JPetStore's web tier.
    -->

    <beans>
    <bean name="/displayListing.do" class="com.tondigang.ListingController">
    </bean>

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

    <bean name="displayListing" class="com.tondigang.dao.impl.ListingDAOImpl">
    </bean>

    </beans>

    and my pom file:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.tondigang.app</groupId>
    <artifactId>apna-spring</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>apna-spring Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.0.5.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    </dependency>

    <dependency>
    <groupId>taglibs</groupId>
    <artifactId>c</artifactId>
    <version>1.1.1</version>
    <scope>runtime</scope>
    <type>tld</type>
    </dependency>
    <dependency>
    <groupId>taglibs</groupId>
    <artifactId>fmt</artifactId>
    <version>1.1.1</version>
    <scope>runtime</scope>
    <type>tld</type>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    <scope>runtime</scope>
    </dependency>

    </dependencies>
    <build>
    <finalName>apna-spring</finalName>
    </build>
    </project>

    Here is the exception:

    HTTP Status 500 -

    --------------------------------------------------------------------------------

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'listingDetails' available as request attribute
    org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:465)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:722)


    root cause

    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'listingDetails' available as request attribute
    org.springframework.web.servlet.support.BindStatus .<init>(BindStatus.java:141)
    org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.getBindStatus(AbstractData BoundFormElementTag.java:174)
    org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.getPropertyPath(AbstractDa taBoundFormElementTag.java:194)
    org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.getName(AbstractDataBoundF ormElementTag.java:160)
    org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.autogenerateId(AbstractDat aBoundFormElementTag.java:147)
    org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.resolveId(AbstractDataBoun dFormElementTag.java:138)
    org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.writeDefaultAttributes(Abs tractDataBoundFormElementTag.java:122)
    org.springframework.web.servlet.tags.form.Abstract HtmlElementTag.writeDefaultAttributes(AbstractHtml ElementTag.java:408)
    org.springframework.web.servlet.tags.form.InputTag .writeTagContent(InputTag.java:140)
    org.springframework.web.servlet.tags.form.Abstract FormTag.doStartTagInternal(AbstractFormTag.java:10 2)
    org.springframework.web.servlet.tags.RequestContex tAwareTag.doStartTag(RequestContextAwareTag.java:7 9)
    org.apache.jsp.index_jsp._jspx_meth_form_005finput _005f0(index_jsp.java:153)
    org.apache.jsp.index_jsp._jspx_meth_form_005fform_ 005f0(index_jsp.java:117)
    org.apache.jsp.index_jsp._jspService(index_jsp.jav a:79)
    org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:722)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:722)


    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.23 logs.


    --------------------------------------------------------------------------------

    Apache Tomcat/7.0.23

    Can you please help???

  • #2
    Here are the relevant bits in your example:

    Code:
    <form:form action="displayListing.do" method="post" commandName="displayListing">
        Listing number: <form:input path="listingId" /><br />
        <input type="submit" value="Submit" />
    </form:form>
    Code:
    @RequestMapping(value = "/displayListing.do", method = RequestMethod.POST)
    public ModelAndView displayListing(@RequestParam("displayListing") ListingDAOImpl listingDetails)
                                                        throws ServletException, IOException {
        ListingDAOImpl listingDetailsDup = listingDetails;
        System.out.println(listingDetailsDup.listingId);
        return new ModelAndView("displayListing", "displayListing", listingDetailsDup);
        //return mav;
    }
    In your jsp, the form is using the POST HTTP method. In your controller handler method signature, you have @RequestParam("displayListing"). When using the POST method, form field values are not sent as request parameters, but in the body of the HTTP POST. So, the form values will not get bound correctly.

    The type of the listingDetails parameter is also strange. Expecting controller input to get bound to a class following the DAO pattern is not something you would typically want to do. Controller handler method parameters are typically classes that follow the Java bean conventions - properties with getters and setters (other methods are OK in these beans too).

    I would create a simple Java bean class named DisplayListing with the listingId property and change the controller handler method to look something like this:

    Code:
    @RequestMapping(value = "/displayListing.do", method = RequestMethod.POST)
    public ModelAndView displayListing(DisplayListing displayListing) {
        System.out.println(displayListing.getListingId());
        return new ModelAndView("displayListing", "displayListing", displayListing);
    }

    Comment


    • #3
      thank you

      Hey Thank you Fred. that worked great. I did two modifications overall. One change is to remove @RequestParam as you mentioned. I did one more mistake.

      I am not sending displayListing that JSP is using as a ModelAttribute. Previously my jsp didn't go through spring at all. I directly kept it as welcome page in web.xml. Now I changed it to go to spring and am passing that object as ModelAttribute. After both the changes, it started working.

      Thanks again Fred.
      --Krishna

      Comment


      • #4
        hi fred,

        can u tell me how to create the <form:input path="listingId" /> dynamically in javascript.

        Comment

        Working...
        X