Results 1 to 9 of 9

Thread: Problem to print form attributes in jsp

  1. #1
    Join Date
    Apr 2008
    Posts
    5

    Default Problem to print form attributes in jsp

    Hello,
    I begin to learn spring, i'm working on a simple form but a have a problem(stupid but i'v just begun with spring)!
    i'll give an example of my code, and some questions, ans thank you a lot for any helps!

    i create a class for the form:
    Code:
    public class Form{
    
    	String Test;
    
            public Form(){
            this.settest("valeur_test");
            }
    
            public String getTest() {
    		return this.Test;
    	}	
    	public void setTest(String test_value) {
    		this.Test=test_value;
    	}
    i'v a controller:
    Code:
    public class RequestController extends SimpleFormController {
    //just a simple submit which does nothing
     public ModelAndView onSubmit(Object command)
         throws ServletException {
    
    		 //Processing user Post request
    		 //Doing somth ...
    		 
          return new ModelAndView(new RedirectView(getSuccessView()));
    	 }
    }
    and this config file for the controller
    Code:
    <beans>
    	
    	<bean     class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="mappings">
    			<props>
    				<prop key="/hello">RequestController</prop>
    			</props>
    		</property>
    	</bean>
    <bean id="RequestController"
    		class="RequestController">
    		
    		<property name="sessionForm">
    			<value>true</value>
    		</property>
    		
    		<property name="commandName">
    			<value>form</value>
    		</property>
    		
    		<property name="commandClass">
    			<value>Form</value>
    		</property>
    		
    		<property name="formView">
    			<value>Request</value>
    		</property>
    		
    		<property name="successView">
    			<value>Requestsucc</value>
    		</property>
    	
    		
    	</bean>
    and in my first jsp, i want just to have my attribute 'test' from the form class, and even i want to add some attributes that Form class has not, for example add attributes in the controller class for the model and view or smth like that, but i don't know how ! i can't also have may 'test' in the jsp, i've:
    Code:
    ...
    <c:out value="${form.Test}"/>
    ...
    i have the page in my browser, but with $Form.test (no value)
    perhaps a syntax error, but ??
    many thanks for your helps!
    Last edited by deklog; Apr 8th, 2008 at 03:05 AM.

  2. #2
    Join Date
    Jan 2008
    Location
    Spain - Espaņa
    Posts
    37

    Default

    Hi,
    you must rename your bean methods as for example "getTest", having "test " as your property bean, getter's and setter's method's must have the get or set method whit the first letter of the property name in capitals,because "Test" is refer to a method access and "test" is refer to a property. your source code will be:
    Code:
    public class Form{
    
    	String test;
    
            public Form(){
            this.settest("valeur_test");
            }
    
            public String getTest() {
    		return this.requestType;
    	}	
    	public void setTest(String requestType) {
    		this.requestType=requestType;
    	}
    Regards
    Last edited by alfviles; Apr 7th, 2008 at 09:01 AM.

  3. #3
    Join Date
    Apr 2008
    Posts
    5

    Default

    Thanks for your answer, yes i'v corrected the code, (getters setters ...), but i still don't have my value in the browser
    i still have ${form.Test} like if the appli can't know what's Form.test ??? is the syntax is correct?or is there any problem with my very simple Spring Appli ???
    the problem is for the first GET, i want to have values from Form class for the GET
    Thanks for your helps
    Last edited by deklog; Apr 7th, 2008 at 09:23 AM.

  4. #4
    Join Date
    Aug 2007
    Posts
    29

    Default

    halo, deklog...


    By look into your RequestController declaration
    Code:
    <bean id="RequestController"
    		class="org.springframework.web.servlet.mvc.SimpleFormController">
    ...
    i think it should

    Code:
    <bean id="RequestController"
    		class="your.package.RequestController">
    ...
    And you can access test property of Form object by ${form.test} - and it's case sensitive.


    Best regards

    pancara

  5. #5
    Join Date
    Aug 2007
    Posts
    29

    Default

    Hi, alviles..

    Code:
    ...
    public 
    
    void setTest(String requestType) {
    		this.requestType=requestType;
    	}
    ...
    you mean
    Code:
    ...
    public 
    
    void setTest(String requestType) {
    		this.test=requestType;
    	}
    ...
    Thanks..

  6. #6
    Join Date
    Jan 2008
    Location
    Spain - Espaņa
    Posts
    37

    Default

    Quote Originally Posted by pancara View Post
    Hi, alviles..

    Code:
    ...
    public 
    
    void setTest(String requestType) {
    		this.requestType=requestType;
    	}
    ...
    you mean
    Code:
    ...
    public 
    
    void setTest(String requestType) {
    		this.test=requestType;
    	}
    ...
    Thanks..
    yes of course , it was cut/paste problem
    regards

  7. #7
    Join Date
    Apr 2008
    Posts
    5

    Default

    Thanks for your helps,
    it was just a copy/past error, i'v now the correct name for controller, correct form methods, but i still don't have nothing in the page.

    Can we use attributes values out of the form ?

  8. #8
    Join Date
    Aug 2007
    Posts
    29

    Default session attribute name for form object

    hi deklog,

    i think if we set sessionForm property with true the form object will be saved in session.
    But the problem is the attribute name in session. I have checked AbstractFormController code and find getFormSessionAttributeName method :

    Code:
    protected String getFormSessionAttributeName() {
      return getClass().getName() + ".FORM." + getCommandName();
    }
    May it helps..

    best regards..

    pancara.

  9. #9
    Join Date
    Apr 2008
    Posts
    5

    Default

    Thanks Pancara for your help,

    I'v checked some tutorials and i found that i must have a formBacking object in my controller, it's used for the first GET for example to have some initial data to pu tin the form.

    also i had some problems with capital letters ...

    In jsp we must not have the first letter of the attribute name in capital !

    so now it ok for my little app

    Thanks a lot for your helps alfviles & Pancara!

    regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •