0

I'm using SpringMVC and wonderingn if it is posible to create form in Controller. What I have in mind is to create a form in Controller layer then pass it to View via ${form} or anything like that then the <form> will automatically displayed in the View.

If you don't understand what I meant or any questions, feel free to comment. Thank you.

4
  • Why do you want to do this? Commented Aug 5, 2014 at 4:13
  • cause I need to create a form with <select> and dynamic options value from DAO. Commented Aug 5, 2014 at 5:03
  • Why not put those values in model attributes and use them in a JSP or other template? Commented Aug 5, 2014 at 5:04
  • Actually I know other ways to achieve what I wanted but if there is a way to make this happen, I want to know it. :D Commented Aug 5, 2014 at 5:06

1 Answer 1

1

It's as simple as what you describe. Create a String

String form = "<form> ... </form>";

Add it to model/request attributes

model.addAttribute("form", form);

And use it anywhere in your JSP (or other view) by retrieving it from the request attributes.

${form}
Sign up to request clarification or add additional context in comments.

3 Comments

this is a great idea but what I expect is like new Form() then form.addOption(). something like that,.
@kenicky Spring doesn't provide such built in functionality. I really suggest using a JSP view or some other templating languages. You can make the form generation be part of one file and import it everywhere else.
Ohh so, Spring doesn't provide that. Thanks for the answer man.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.