Sunday, 15 February 2015

java - How do I get data from the form inputs in a Freemarker template into a spring MVC controller? -


I am trying to get simple text input from the form generated by the freemarker template. The correct method is being used, but the model is empty, where I hope it will be populated.

I am working in the context of a pre-existing application or I would have thrown this combination aside on a long time and went straight to the Spring MVC / JSP implementation.

I have gone through every tutorial on the web, and posting a lot on this topic in stack overflow, and I still have failed to understand something. Whatever I am missing can be so basic that nobody bothers to post it.

Not very useful in the examples of applications because they have been created with sufficient dilemma, whom I can not see how they work all.

Freemer Template

  & lt; #import "/spring.ftl" spring /> In form of. & Lt; @ Spring.bind "model" /> & Lt; Form name = "model" method = "post" & gt; & Lt; Input type = "text" name = "user" id = "user" value = "$ {model.user}" /> & Lt; Input type = "hidden" id = "submission" name = "submission" /> & Lt; Input type = "submit" name = "test" id = "test" value = "test" onclick = "document.getElementById ('submission'). Value = 'test'" /> & Lt; / Form & gt;  

Controller code:

  @RequestMapping (value = "/ config", method = RequestMethod.POST) Public ModelMap postConfig (@ModelAttribute ("Model" Modelmap model) {logger.debug ("user name was {}", model.get ("user")); // Why is the model empty here? Return model; Finally, I was unable to use ModelMap as an input  

My It is believed that the reason for this is that when Spring has millions of ways to accomplish any task, the freerker integration is not as strong as JSP integration.

There is a little known but widely used feature in Spring which resolves form elements based on names. Based on the comments of others, I do not believe it is well documented. But, since this is the main on the framework of Gretev Grovie, I believe it is safe to consider it being the main for spring.

I finally use this solution:

  @RequestMapping (value = "/ config", method = RequestMethod.POST) Public ModelMap postConfig (string user) { Logger.debug ("user name was {}", user); // Why is the model empty here? // ... cutting ... model Input ("user", user); Return model; }  

The spring controller method resolves the form bodies through parameter type and name. It's a bit inconvenient for my intentions, but it gets the data where I can work with it.


No comments:

Post a Comment