Announcement Announcement Module
Collapse
No announcement yet.
can I use HTML form tag with SimpleFormController ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • can I use HTML form tag with SimpleFormController ?

    I want to check if I can use html form tag (instead of the form tag from spring tag-lib) with SimpleFormController. I want to know what will be the difference. Will I still get the form as a commandobject in controller's onSubmit method ?

  • #2
    I want to check if I can use html form tag (instead of the form tag from spring tag-lib) with SimpleFormController.
    Yes you can still use the html form tag with the SFC, you would end up coding more, ultimately the spring form tags output html tags.

    I want to know what will be the difference. Will I still get the form as a commandobject in controller's onSubmit method ?
    the difference will be a lot, for example with spring form tags:
    Code:
    <form:form commandName="myCommandName">
    <form:input path="firstName"/>
    </form:form>
    without spring tags
    Code:
    <form method="post">
    <input name="myCommandName.firstName"/>
    </form>
    you would have to do more programming to display any errors which otherwise can be easily handled by the form:errors tag

    Comment


    • #3
      command object in the onSubmit() has not data

      Thanks Sami for your reply. I tried changing my view html form fields to include the command name as you told. But I still don't get any data in command object of the onsubmit method of my controller.

      If I try to do request.getParameter(anyFormfieldname) I see that the values are there but don't know why the commad object does not have this data ? Am I missing something ?

      here's my configuration -

      <entry key="/**/MyTest.do">
      <ref local="MyController"/>
      </entry>

      <bean id="MyController" class="com.MyController">
      <property name="dataService">
      <ref bean="myDataService"/></property>
      <property name= "commandClass">
      <value>com.myFormBean</value></property>
      <property name="commandName" value="myForm"/>
      <property name="successView"><value>MyViewInfo</value></property>
      </bean>

      Here's the view --

      <form id="myForm" name="myForm" action="MyTest.do" method="post">
      FirstName :<input type="text" name="myForm.firstName" value="AAA"/>
      LastName :<input type="text" name="myForm.lastName" value="BBB"/>
      <input type="submit" name="myForm.submitButton" id="myForm.submitButton" value="submit" />
      </form>


      my controller overrides the onSubmit method -
      public class TestController extends SimpleFormController {
      protected ModelAndView onSubmit(HttpServletRequest request,
      HttpServletResponse response,
      Object commandObject, BindException exception) {
      ..
      }
      }

      Thanks.

      Comment


      • #4
        vaishu74,
        One of the main reasons for using the spring form tag is that it maps the input fields with command fields. If you insist on using a plain html form, then you have to provide this mapping for spring on your own.
        Here is your original form:

        Code:
        <form id="myForm" name="myForm" action="MyTest.do" method="post">
        FirstName :<input type="text" name="myForm.firstName" value="AAA"/>
        LastName :<input type="text" name="myForm.lastName" value="BBB"/>
        <input type="submit" name="myForm.submitButton" id="myForm.submitButton" value="submit" />
        </form>
        If you want the command object to bind with your inputs, then you will have to wrap the inputs with the bind tag like so:
        Code:
        <form id="myForm" name="myForm" action="MyTest.do" method="post">
        <spring:bind path="myForm.firstName">FirstName :<input type="text" name="myForm.firstName" value="AAA"/></spring:bind>
        <spring:bind path="myForm.lastName">LastName :<input type="text" name="myForm.lastName" value="BBB"/></spring:bind>
        <input type="submit" name="myForm.submitButton" id="myForm.submitButton" value="submit" />
        </form>
        My example assumes you included the tag library and set the prefix to 'spring'.


        So you see, you can manually bind input elements to your command object using the bind tags, but I think you will find that the form tag (with its associated group of input tags) can do everything you can do with a plain old html form.

        I hope this helps.

        Comment

        Working...
        X