开发者

Creating multiple forms in one jsp file. ModelAttributes have to always be set?

开发者 https://www.devze.com 2022-12-31 18:51 出处:网络
I am new to Spring and have been reading about it the last few days. I haven\'t found an open source example like this so far and didn\'t see a clear way for it to be done.

I am new to Spring and have been reading about it the last few days. I haven't found an open source example like this so far and didn't see a clear way for it to be done.

Many sites have a login or search box that is on every page. If this is the case, how can you avoid sett开发者_StackOverflowing an attribute in a model on every page for that form? Similarly, if you have a form on page FooBar that should have little knowledge of the processing of the form, which is done in SearchFooBar, how can it create the form without adding the model attribute?

Right now I have been using <form:form modelAttribute="classname"...> with

   @RequestMapping(method=RequestMethod.GET)
    public String setupForm(Model model) {
        model.addAttribute("classnamehere", new ClassNameHere());
        return "pagename";
    }

Thanks

PS: On a related note, can you automatically include header and footer jsp files throughout a site without doing an <% include on every page?


I believe you do need to use the modelAttribute tag, In situations where I have multiple forms I usually go with Spring Webflow as it handles the scenario better.

If you're just trying to repeat yourself then use a jsp tag to render the form.

And the 2 forms you mention are very simple, search box or login. So don't use the spring binding/form stuff and just handle the form yourself, perhaps in a tag or something.

0

精彩评论

暂无评论...
验证码 换一张
取 消