Announcement Announcement Module
Collapse
No announcement yet.
Form Not Returning Values Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Form Not Returning Values

    Hi all,
    I am very new to spring and I am having an issue that I hope will be easy to resolve. I am building my first spring application and I am having trouble getting values from my commandClass into my jsp page. It is probably as simple as something being configured wrong but I have been spinning my wheels for a day now. Any help would be appreciated. My custom code is below and what I attempting to do is return a custom object type AvailableShows in my formBackingObject function of my controller. I then want to be able to pull values from AvailableShows into my jsp. I am not getting an error what I am seeing is the the browser will display "${availableShows.stringForTest}". Thanks again for any help. Craig

    Controller

    Code:
    package jamfree.music.controller;
    
    import jamfree.music.form.AvailableShows;
    import jamfree.music.form.MusicForm;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.web.servlet.mvc.SimpleFormController;
    
    public class musicController extends SimpleFormController {
    
    	// SortedMap<Integer, ArrayList<String>> sortedMapObject = null;
    	MusicForm form = null;
    
    	@Override
    	protected Object formBackingObject(HttpServletRequest request)
    			throws Exception {
    		// TODO Auto-generated method stu
    		System.out.println("musicController.FormBackingObject...");
    		AvailableShows testForm = new AvailableShows();
    
    //		populateMusicForm();
    		System.out.println("RETURNING FORM");
    		System.out.println("CommandClass:  " +this.getCommandClass().getName());
    		return testForm;
    		// return super.formBackingObject(request);
    
    	}
    	
    }
    CommandClass (AvailableShows)
    Code:
    package jamfree.music.form;
    
    import java.util.ArrayList;
    
    public class AvailableShows {
    
    	ArrayList<String> availableShows = null;
    
    	public AvailableShows() {
    		try {
    			availableShows = new ArrayList<String>();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public ArrayList<String> getAvailableShows() {
    		ArrayList<String> orderedReports = new ArrayList<String>();
    		for (int i = availableShows.size() - 1; i > -1; i--) {
    			orderedReports.add(availableShows.get(i));
    		}
    		return orderedReports;
    	}
    
    	public void setReport(String value) {
    		if (availableShows == null) {
    			availableShows = new ArrayList<String>();
    		}
    		availableShows.add(value);
    	}
    
    	public int getSize() {
    		return availableShows.size();
    	}
    
    	public String getStringForTest() {
    		return "SAMPLE STRING";
    	}
    
    }
    JSP

    Code:
    <%@ taglib prefix="c"        uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn"       uri="http://java.sun.com/jsp/jstl/functions" %>
    <%@ taglib prefix="fmt"      uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@ taglib prefix="spring"   uri="http://www.springframework.org/tags" %>
    <%@ taglib prefix="form"     uri="http://www.springframework.org/tags/form" %>
    
    <form:form name="shows" commandName="availableShows">
    <html>
    <head>
      <title>Available Shows</title>
      <link rel="stylesheet" href="/styles/style.css">
    </head>
    <body bgcolor="Silver">
    <table>
    <tr>
    <td>
    ${availableShows.stringForTest}
    </td>
    </tr>
    </body>
    </html>
    </form:form>
    Servlet
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
         <bean id="urlMapping" 
              class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
              <property name="urlMap">
                   <map>
                        <entry key="/search.do">
                             <ref bean="searchController" />
                        </entry>
                        <entry key="/music.do">
                             <ref bean="musicController" />
                        </entry>
                   </map>
              </property>
         </bean>
    <bean id="searchController" class="jamfree.music.controller.searchController">
        <property name="sessionForm"><value>true</value></property>
        <property name="commandName"><value>search</value></property>
        <property name="commandClass"><value>jamfree.music.form.searchForm</value></property>
        <property name="formView"><value>jsp/search.jsp</value></property>
    </bean>
    
    <bean id="musicController" class="jamfree.music.controller.musicController">
        <property name="sessionForm"><value>true</value></property>
        <property name="commandName"><value>availableShows</value></property>
        <property name="commandClass"><value>jamfree.music.form.AvailableShows</value></property>
        <property name="formView"><value>jsp/music.jsp</value></property>
    </bean>
    </beans>
    Last edited by mornindew; Apr 24th, 2009, 01:13 AM.

  • #2
    Hi Craig,
    I think the problem you are facing is that you are accessing the object(in your case the list) in a wrong manner.Here's how you can access it in the jsp:
    Code:
    <c:forEach var="availableShows" items="${availableShows}">
    <c:out value="${availableShows.stringForTest}"></c:out>
    </c:forEach>

    My only concern was you are accessing availableShows.stringForTest but actually in the command class which you have posted , there seems to be no instance variable name stringForTest.You need to add this instance variable and then the get method will be accessible using the above mentioned solution.
    Since the tag-lib only allows accessing list items using instance variables.It would be great ,if you could define a variable called "stringForTest".I beleieve it should work then perfectly fine.
    Hope the above mentioned solution helps you solve the problem.
    Cheers,
    Sushant

    Comment


    • #3
      Sushant,

      Thanks for the quick reply, unfortunately it doesn't seem to have worked. I am still getting the same behavior. Just to make sure we are on the same page here is my updated jsp and update java class. Any other suggestions would be very helpful. Thanks, Craig

      Code:
      <%@ taglib prefix="c"        uri="http://java.sun.com/jsp/jstl/core" %>
      <%@ taglib prefix="fn"       uri="http://java.sun.com/jsp/jstl/functions" %>
      <%@ taglib prefix="fmt"      uri="http://java.sun.com/jsp/jstl/fmt" %>
      <%@ taglib prefix="spring"   uri="http://www.springframework.org/tags" %>
      <%@ taglib prefix="form"     uri="http://www.springframework.org/tags/form" %>
      
      <form:form name="shows" commandName="availableShows">
      <html>
      <head>
        <title>Available Shows</title>
        <link rel="stylesheet" href="/styles/style.css">
      </head>
      <body bgcolor="Silver">
      <table>
      <tr>
      <td>
      ${availableShows.stringForTest}
      </td>
      </tr>
      <tr>
      <c:forEach var="availableShows" items="${availableShows}">
      <td>
      <c:out value="${availableShows.stringForTest}"></c:out>
      </td>
      </c:forEach>
      </tr>
      </body>
      </html>
      </form:form>

      Code:
      package jamfree.music.form;
      
      import java.util.ArrayList;
      
      public class AvailableShows {
      
      	ArrayList<String> availableShows = null;
      	public String testMessage = null;
      
      	public AvailableShows() {
      		try {
      			availableShows = new ArrayList<String>();
      			testMessage = "SAMPLE STRING";
      		} catch (Exception e) {
      			e.printStackTrace();
      		}
      	}
      
      	public ArrayList<String> getAvailableShows() {
      		ArrayList<String> orderedReports = new ArrayList<String>();
      		for (int i = availableShows.size() - 1; i > -1; i--) {
      			orderedReports.add(availableShows.get(i));
      		}
      		return orderedReports;
      	}
      
      	public void setReport(String value) {
      		if (availableShows == null) {
      			availableShows = new ArrayList<String>();
      		}
      		availableShows.add(value);
      	}
      
      	public int getSize() {
      		return availableShows.size();
      	}
      
      	public String getStringForTest() {
      		return testMessage;
      	}
      
      }

      Comment


      • #4
        try putting this at the top of your jsp

        <%@ page isELIgnored="false" %>

        Comment


        • #5
          Knoxor,
          That did it, thank you very much. I haven't researched the line yet "<%@ page isELIgnored="false" %> " but what does that do. Thanks again for your help

          Comment

          Working...
          X