Announcement Announcement Module
Collapse
No announcement yet.
Problems loading data into a SimpleFormController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems loading data into a SimpleFormController

    Hi,

    I'm having trouble with loading values into a form with a SimpleFormController. New to Spring, and just trying to work this out. I've tried copying the methods used in the MVC tutorial and the Equinox codethat seem to have no trouble doing what I'm trying to do, but for some reason (probably silly) I cannot get default values from my backing object into the form fields.

    Here is the flow as I see it:

    I request the page editStatus.htm with some params:

    Code:
    	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="mappings">
    			<props>
    				<prop key="/editStatus.htm">editPlayerStatusFormController</prop>
    			</props>
    		</property>
    	</bean>
    This invokes the editPlayerStatusFormController bean (tried sessionForm=true as well):

    Code:
    	<bean id="playerStatusValidator" class="bus.PlayerStatusValidator"/>
    	<bean id="editPlayerStatusFormController" class="web.EditPlayerStatusFormController">
    		<property name="sessionForm"><value>false</value></property>
    		<property name="commandName"><value>editStatus</value></property>
    		<property name="commandClass"><value>bus.PlayerStatus</value></property>
    		<property name="validator"><ref bean ="playerStatusValidator"/></property>
    		<property name="formView"><value>editPlayerStatus</value></property>
    		<property name="successView"><value>redirect&#58;playerDetail.htm</value></property>
    		<property name="playerStatusManager"><ref bean="playerStatusMan"/></property>
    	</bean>
    which should then call at least one method within the EditPlayerStatusFormController class:

    Code:
    package web;
    
    import java.io.*;
    import java.util.*;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    import org.apache.commons.logging.*;
    
    import org.springframework.web.servlet.*;
    import org.springframework.web.servlet.mvc.*;
    import org.springframework.web.servlet.view.*;
    import org.springframework.validation.*;
    
    import bus.*;
    
    public class EditPlayerStatusFormController extends SimpleFormController &#123;
    
        // Logger for this class and subclasses
        protected final Log logger = LogFactory.getLog&#40;getClass&#40;&#41;&#41;;
        
        private PlayerStatusManager playerStatusMan;
    	private int playerId = -1; 
    	
        public ModelAndView processFormSubmission&#40;HttpServletRequest request, HttpServletResponse response, Object command, BindException errors&#41;
    		throws Exception &#123;
    		
    		System.out.println&#40;"1"&#41;;
    		
    			logger.info&#40;"in processFormSubmission"&#41;;
    		
    			if &#40;request.getParameter&#40;"cancel"&#41; != null&#41; &#123;
    					return new ModelAndView&#40;getSuccessView&#40;&#41;&#41;;
    			&#125;
    
    			return super.processFormSubmission&#40;request, response, command, errors&#41;;
    	&#125;	
        
        public ModelAndView onSubmit&#40;Object command, BindException errors&#41; throws ServletException &#123;
    		
    		System.out.println&#40;"1"&#41;;
    		
    		logger.info&#40;"in onSubmit..."&#41;;
    		
            PlayerStatus updatedStatus = &#40;&#40;PlayerStatus&#41; command&#41;;
            
            playerStatusMan.updatePlayerStatus&#40;updatedStatus&#41;;
            
            return new ModelAndView&#40;new RedirectView&#40;"playerDetail.htm?playerId=" + playerId&#41;, errors.getModel&#40;&#41;&#41;;
            
        &#125;
        
        protected Object formBackingObject&#40;HttpServletRequest request, HttpServletResponse response&#41; throws ServletException &#123;
            
    		System.out.println&#40;"1"&#41;;
    		
    		logger.info&#40;"formBackingObject..."&#41;;
    		
    		int playerId = new Integer&#40;request.getParameter&#40;"playerId"&#41;&#41;.intValue&#40;&#41;;	
    		int statusId = new Integer&#40;request.getParameter&#40;"statusId"&#41;&#41;.intValue&#40;&#41;;	
    		
    		PlayerStatus playerStatus = playerStatusMan.getPlayerStatusByStatusId&#40;playerId, statusId&#41;;
    		
    		logger.info&#40;"formBackingObject...playerId = " + playerId&#41;;
    		
            return playerStatus;
            
        &#125;
        
        public void setPlayerStatusManager&#40;PlayerStatusManager pm&#41; &#123;
    		System.out.println&#40;"1"&#41;;
            playerStatusMan = pm;
        &#125;
        
        public PlayerStatusManager getProductManager&#40;&#41; &#123;
    		System.out.println&#40;"1"&#41;;
            return playerStatusMan;
        &#125;
        
    &#125;
    And then populate my view - just jstl and only one of the possible fields at the moment:

    Code:
    <%@ include file="/WEB-INF/jsp/include.jsp" %>
    <%@ taglib prefix="spring" uri="/spring" %>
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>JJJFC - Edit Player Status</title>
    </head>
    
    <body>
    
    <h1>Player Status</h1>
    
    <form method=post name="editPlayerStatus">
    
    	<table>
    		
    		<tr>
    			<td><h3>Team</h3></td>
    				<spring&#58;bind path="editStatus.team">	
    					<td><input type="text" name="team" value="<c&#58;out value="$&#123;status.value&#125;"/>">
    				</spring&#58;bind>
    			</td>	
    		</tr>							
    	
    	</table>
    
    </form>
    
    </body>
    </html>
    This field isn't filled, and I don't see any of the logger and system.outs from the Controller class. All I get from tomcat for this request is the following two lines:

    Code:
    2005-04-16 21&#58;55&#58;34,151 INFO &#91;org.springframework.web.servlet.view.InternalResourceViewResolver&#93; - <Cached view 'editPlayerStatus'>
    2005-04-16 21&#58;55&#58;36,885 INFO &#91;org.springframework.ui.context.support.ResourceBundleThemeSource&#93; - <Theme created&#58; name=theme, baseName=theme>
    Am I doing something really wrong? I doesn't even look like my controller is being called! Any help here is really appreciated.

    Thanks,

    Bones

  • #2
    Is it possible that you're accessing editPlayerStatus.jsp directly instead of editPlayer.htm?

    Comment


    • #3
      Yes,

      I had that thought, but I'm pretty sure that it's spring thats making the call. I have a mapping from .htm to the spring DispatcherServlet, and when I remove the urlMapping from the spring-servlet.xml for the editStatus.htm I get a resource not found from tomcat. I also cleared the tomcat work cache with no effect.

      Comment


      • #4
        <table>

        <tr>
        <td><h3>Team</h3></td>
        <spring:bind path="editStatus.team">
        <td><input type="text" name="team" value="<c:out value="${status.value}"/>">
        </spring:bind>
        </td>
        </tr>

        </table>
        2 things I would try:
        first, <c:out> does not seem to be needed here, you can do it like:
        Code:
              <tr>
                 <td><h3>Team</h3></td>
                    <spring&#58;bind path="editStatus.team">   
                       <td><input type="text" name="team" value="$&#123;status.value&#125;"/>
                    </spring&#58;bind>
                 </td>   
              </tr>                     
           
           </table>
        if you want to use <c:out> the try using single quotes like this:
        Code:
           <table>
              
              <tr>
                 <td><h3>Team</h3></td>
                    <spring&#58;bind path="editStatus.team">   
                       <td><input type="text" name="team" value="<c&#58;out value='$&#123;status.value&#125;'/>">
                    </spring&#58;bind>
                 </td>   
              </tr>                     
           
           </table>

        Comment


        • #5
          Thanks, doing as you suggest in the first case I get the text "${status.value}" in the text field, and in the second case I get nothing (the same result as above). This tells me that the controller class isn't being called, though I can't see why? Is there a way of increasing the log level for spring?

          I know as I've said above that the form is being called through spring, it just doesn't seem to be invoking the SimpleFormController class.

          Comment


          • #6
            Found the damn thing. I was using:

            Code:
            protected Object formBackingObject&#40;HttpServletRequest request, HttpServletResponse response&#41; throws ServletException &#123;
            instead of

            Code:
            protected Object formBackingObject&#40;HttpServletRequest request&#41; throws ServletException &#123;
            no HttpServletResponse parameter. What an idiot. For those that offered suggestions thanks very much.

            Comment

            Working...
            X