Matt
Matt

Reputation: 6953

Java: Spring MVC - How do I access the updated model in controller action?

I am learning Spring MVC 3 and am not a Java expert in general. I have a few questions

  1. From what I can tell ModelAndView is not used anymore. I also see these two:
    • org.springframework.ui.Model
    • org.springframework.ui.ModelMap

What's the difference between ModelMap and Model? Is one of them old style like ModelAndView?

  1. How can I pass form data back to the controller? Here's what I have so far:

VIEW

<form action="/KSC/users/update" method="POST" class="form-horizontal" id="fEdit">

                            <input type="hidden" id="id" name="id" value="${record.id}" />

                            <div class="control-group">
                                <label for="userName" class="control-label"></label>
                                <div class="controls">
                                    <input type="text" id="userName" name="userName" value="${record.userName}" data-validation-engine="validate[required]" />
                                </div>
                            </div>

                            <div class="control-group">
                                <label for="email" class="control-label"></label>
                                <div class="controls">
                                    <input type="text" id="email" name="email" value="${record.email}" data-validation-engine="validate[required,custom[email]]" />
                                </div>
                            </div>

                            <div class="control-group">
                                <div class="controls">
                                    <input type="submit" value="Save" class="btn btn-primary" />
                                    <a href="/KSC/users" class="btn">Cancel</a>
                                </div>
                            </div>
                        </form>

EDIT ACTION

Here's what I originally passed to the above View from the Edit action:

@RequestMapping(value = "/users/edit/{id}")
    public String edit(ModelMap model, @PathVariable("id") int userId) {
        KCSUser user = service.find(userId);
        model.addAttribute("record", user);
        return "user/edit";
    }

CONTROLLER UPDATE ACTION

@RequestMapping(value = "/users/update")
    public String update(ModelMap model) {
        //TODO
    }

I need to access the updated model data so i can save it to my DB. Ideally if it could map directly to a KSCUser object that would be nice.. but if not, then a Model or ModelMap would be fine too. How can I do this?

Upvotes: 0

Views: 6978

Answers (2)

Yevgeniy
Yevgeniy

Reputation: 2694

How can I pass form data back to the controller?

just change the signature of you update method like this:

@RequestMapping(value = "/users/update")
public String update(KSCUser user, ModelMap model) {
    // ...
}

spring will create a instance of KSCUser and fill the properties based on the name attribute of the input field. so if you have a input field like <input type="text" name="username" />, spring will try to call setUsername on the instance of KSCUser.

you could also change the signature of you method like this:

@RequestMapping(value = "/users/update")
public String update(@RequestParam("username") final String username) {
    // ...
}

spring then would inject the value of the input field with name username.

or you could change the signature of your method like this:

@RequestMapping(value = "/users/update")
public String update(Map<String, String> params) {
    // ...
}

spring then would inject all request parameters in the params map. you can access the username like this: params.get("username").

for the sake of completeness... you could change the signature of you method like this:

@RequestMapping(value = "/users/update")
public String update(HttpServletRequest request) {
    // ...
}

spring then would just inject a instance of HttpServletRequest and you could access you request parameters like you would do with vanila servlet api.

Ideally if it could map directly to a KSCUser object that would be nice.. but if not, then a Model or ModelMap would be fine too

when you inject ModelMap only (like you do in your update method), it will not contain the request parameters (except you use @SeessionAttributes) and you will not be able to access them this way.

What's the difference between ModelMap and Model

good question. the javadoc for ModelMap says:

Check out the Model interface for a Java-5-based interface variant that serves the same purpose

imho: the Model interface looks like it was intended only for adding objects to the model (not reading from model)... just like it is the case in your edit method. But then there is a asMap method, which grant you access to the content of the model.

i have never used the Model interface myself. Always inject the ModelMap.

Upvotes: 4

Matt
Matt

Reputation: 6953

This seems to work:

@RequestMapping(value = "/users/update")
    public String update(ModelMap model, @ModelAttribute("record") KCSUser record) {

        if (record.getId() == 0) {
            service.insert(record);
        }
        else {
            KCSUser existing = service.find(record.getId());
            existing.setUserName(record.getUserName());
            existing.setEmail(record.getEmail());
            //etc...
            service.update(existing);
        }

        return index(model);
    }

Upvotes: 1

Related Questions