3

I made this way more confusing than it needed to be. Here is the simplified version.

1 - I have a drop down and a submit button on every page in the upper right hand corner. The drop down is a list of languages. The user can change the language and press the submit button to go to the language controller and update the current language.

    public class LanguageController {

     @ModelAttribute("languageList")
       public LanguageList populateLanguageList() {
             return LanguageDAO.all();
       }

        @RequestMapping("/setLanguage.mvc")
        public ModelAndView setLanguage(
            @ModelAttribute("languageForm") LanguageForm languageForm,
                HttpServletRequest request, 
                HttpServletResponse response
                ){
            //do stuff
        }
    }


<form:form modelAttribute="languageForm">
    <form:select path="acctGrpId" >
        <form:options items="${languageList}"/>
    </form:select>
        <input type="submit"/>
</form:form>

How would I make that available on every page? Given that the page might be a page where you are editing/creating a user so the "Controller" of that page is actually a UserController, or RoleController, or DepartmentController, not the LanguageController. I need this particular jsp to hit a specific controller regardless of what the page is doing.

samwise
  • 451
  • 5
  • 15

4 Answers4

3

You just specify different action attribute in different forms. There's no problem in having multiple forms on one page.

In your case it would be:

<form action="Context/adduser.mvc">
</form>

and

<form action="Context/addBookmark.mvc">
</form>

etc

soulcheck
  • 36,297
  • 6
  • 91
  • 90
  • I just updated my post, hopefully it makes more sense now, my real problem is having multiple controllers on the same jsp. How do I tell a jsp page "Use this controller" regardless of what the page controller is set to be. – samwise Nov 09 '11 at 17:27
  • Maybe i still don't get it or what i have written still stands. Just specify different action attributes in form tag. That way form which has to be served by LanguageController will be served there and the form on the actual page will be served by page's controller. – soulcheck Nov 09 '11 at 17:40
0

I would love to help you on this, but I am unsure of your requirements.

Are you talking about BookMarkController having various methods and add/upload calling the methods?

or you want BookMarkController to have more than one form?

Could you please elaborate more on this?

I believe, you want all the jsp pages to post data to a controller which can save the form when a link is clicked. This is quite simple, you can add a savePage method in all the controllers and map the uri accordingly and from the savePage method you can reedirect the request to the specific controller which you have written to save the pages.

Hope this helps.

dharam
  • 7,882
  • 15
  • 65
  • 93
  • How about this: Disregard the whole upload users thing - If on any page you could include a jsp (jsp:include path="bookmark.jsp") and that bookmark.jsp page had a spring form and that form connected with a Bookmark controller... what would that look like? What would that JSP look like, how would you tell that JSP to hit the bookmark controller instead of the controller defined on the "current" page. – samwise Nov 09 '11 at 16:41
0

Your JSP is the view, which is separate from your controller. That's the beauty of MVC, the separation of the Model, View, and Controller. You shouldn't necessarily think of the view as being tied to a controller, or a page as having a "current" controller. Yes, we often make that association pretty strong because the controller handles populating the model attributes to render the view and processing the form post as a result of an action in the view, but really any controller can return any view as long as it populates the model correctly, and any view can submit to any controller. A view can even submit to different controllers at different times depending on the url that is followed.

So, in your case, as soulcheck says, you simply change the URL that the form submits to. Because the controllers are mapped by the @RequestMapping, changing the form's action will change the controller class and/or method that is called when the form is submitted as long as Spring MVC can find a suitable RequestMapping.

digitaljoel
  • 26,265
  • 15
  • 89
  • 115
  • Ah! It's the population of the model that has me hung up, not necessarily RequestMapping. The ${languageList} for example, that's not going to be available on the UserController. So how do I get the ${languageList} value from the LanguageController... at some point I have to say "Get me the language controller" so I can get the language list and build the form. From there I can post to the LanguageController just fine, but getting the data is where I'm stuck – samwise Nov 09 '11 at 18:14
  • When I do it, I try to use the controller as simply an aggregator of the data needed for a view. They don't do any of the work to get the data. So in your case, you would have a service or utility class that would have a method to get the languageList (from the database, properties, wherever it is), then both controllers could call that method to populate the model correctly. I try to keep controllers as autonomous as possible, not wanting one controller to depend on another, but that's a personal preference. – digitaljoel Nov 09 '11 at 18:22
  • Or just put the languageList into a session attribute. – GriffeyDog Nov 09 '11 at 21:40
  • Yep, I guess the session would qualify as a "utility class" or would be part of "wherever it is" ;) My point is that wherever it is stored, and the logic of getting the list would be somewhere that the two controllers could share. – digitaljoel Nov 09 '11 at 22:10
0

Is this what you are looking for?

What are the best practices around setting global model attributes in Spring MVC?

implement a HandlerInterceptor, and expose the data to every request

Community
  • 1
  • 1
tewe
  • 2,707
  • 3
  • 22
  • 18