Page 1 of 1

Form Post not supported

Posted: Thu Sep 17, 2015 4:27 am
by incre2012
I have been able to add a new section to the left nav panel . Now I have also added a simple form to this section which works fine . Now I need to post the data filled up in the form to the server .
This are the steps that I followed : -

    Created a controller named MyController with this code

Code: Select all

@Controller
@RequestMapping("/" + MyController.SECTION_KEY)
@Secured("PERMISSION_OTHER_DEFAULT")
public class MyController extends AdminAbstractController {
   protected static final String SECTION_KEY = "test";

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String test(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        // This is expected by the modules/emptyContainer template, this is a custom template that gets included into the body
        model.addAttribute("customView", "test");
       
       // Test custom thymeleaf form display and field binding to back end custom pojo class
       model.addAttribute("greeting", new Greeting());
       
        // ensure navigation gets set up correctly
        setModelAttributes(model, SECTION_KEY);

        // gets the scaffolding set up to display the template from the customView attribute above
        return "modules/emptyContainer";

    }
   
    @RequestMapping(value="/greeting", method = RequestMethod.POST)
    public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) {
       
      model.addAttribute("customView", "result");
      
       // Test custom thymeleaf form display and field binding to back end custom pojo class
      model.addAttribute("greeting", greeting);
       
        // Ensure navigation gets set up correctly
        setModelAttributes(model, SECTION_KEY);

        // gets the scaffolding set up to display the template from the customView attribute above
        return "modules/emptyContainer";
    }
}

Made this addition to the applicationContext-servlet-admin.xml file

Code: Select all

 <context:component-scan base-package="com.myco.admin.web" />


So that spring finds my controller class

Next I created 2 fragment html files name test.html with content

Code: Select all

<div class="row">
    <div class="twelve columns">
        <h1>Form</h1>
        <blc:form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
          <p>Id: <input type="text" th:field="*{id}" /></p>
           <p>Message: <input type="text" th:field="*{content}" /></p>
           <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
       </blc:form>
    </div>
</div>



and result.html with content

Code: Select all

<div class="row">
    <div class="twelve columns">
      <h1>Result</h1>
          <p th:text="'id: ' + ${greeting.id}" />
          <p th:text="'content: ' + ${greeting.content}" />
          <a href="/greeting">Submit another message</a>
   </div>
</div>         


in the following locations

Code: Select all

WEB-INF/templates/layout/partials/test.html
WEB-INF/templates/layout/partials/result.html


of the admin module

I have this entry for the view resolver

Code: Select all

<bean id="blAdminWebTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
       <property name="prefix" value="/WEB-INF/templates/layout/partials/" />
       <property name="suffix" value=".html" />
       <property name="templateMode" value="HTML5" />
       <property name="cacheable" value="${cache.page.templates}"/>
       <property name="cacheTTLMs" value="${cache.page.templates.ttl}" />
       <property name="characterEncoding" value="UTF-8" />
       <property name="order" value="200"/>         
   </bean>     
   


Now when I do a post request to the server I get this error

Code: Select all

PageNotFound - Request method 'POST' not supported


Preciously when I was not using the blc tag in the view pages I was getting a CSRF Filter exception which was solved by using the blc thymeleaf processor tag but now I get this error . All the changes have been made to the admin module .
I have checked so many times but could not find a solution . Please help

Re: Form Post not supported

Posted: Fri Sep 18, 2015 5:14 am
by incre2012
Very sorry friends its gross negligence on my part . The class is annotated with

Code: Select all

@RequestMapping("/" + MyController.SECTION_KEY)


where MyController.SECTION_KEY value is initialised as test co the class's root url becomes /test

the method that handles http post requests has

Code: Select all

 @RequestMapping(value="/greeting", method = RequestMethod.POST)


which actually maps to the url /test/greeting where as test.html had action attribute of html form element set as /greeting .

So the post url from the html form resulted in /greeting where as the controller method for handling the post request had a mapping of /test/greeting . It was unble to match the post url from the html file to the controller's request mapping

Now when I change the html file's post url to /test/greeting the issue is resolved . I agree it was a very silly mistake on my part . Hope it helps others who may have commited the same mistake .