hetsketch.
hetsketch.

Reputation: 89

How to bind data from velocity template to spring controller?

I have a velocity template with tag form.

<div class="row">
<div class="container">
    <div class="col-lg-12">
        <table class=" table table-striped">
            <thead>
            <th>#</th>
            <th>username</th>
            <th>role</th>
            <th>password</th>
            </thead>
            <tbody>
                #foreach( $user in $users )
                <tr>
                    <td>$user.id</td>
                    <td>$user.username</td>
                    <td>$user.role</td>
                    <td>$user.password</td>
                    <td><a href="/user/delete/$user.id">Delete</a></td>
                    <td><a href="/user/edit/$user.id">Edit</a></td></tr>
                #end
            </tbody>
        </table>
        <form method="post" action="/user">
            <div class="form-group">
                <label for="username">Username:</label>
                <input type="username" class="form-control" id="username">
            </div>
            <div class="form-group">
                <label for="pwd">Password:</label>
                <input type="password" class="form-control" id="pwd">
            </div>
            <div class="form-group">
                <label for="role">Role:</label>
                <input type="role" class="form-control" id="role">
            </div>
            <button type="submit" class="btn btn-default">Add</button>
        </form>
    </div>
</div>

And I need to pass entered data to the controller. Here is controller code.

@RequestMapping(value = "/user", method = RequestMethod.POST)
public String addUser(@ModelAttribute User newUser) {
    userService.save(newUser);
    return "redirect:/users";
}

I'm newbie in velocity and I haven't figured out in this framework yet. I've google a long time but unsuccessful. Please help velocity guru!

Upvotes: 1

Views: 1496

Answers (1)

Olantobi
Olantobi

Reputation: 879

The problem I see here is not just about velocity or Spring. Your form inputs does not have names, it seems you misplaced name for type in your form. It's the input names that's sent to the controller. What you want to do is create a User model and make sure it has the same variable names as the form input names.

 public class User {
    private String username;
    private String password;
    private String role;

   // Add getter and setter methods

   // Add toString method
}

And your form should be like

<form method="post" action="/user">
            <div class="form-group">
                <label for="username">Username:</label>
                <input type="text" class="form-control" id="username" name="username">
            </div>
            <div class="form-group">
                <label for="pwd">Password:</label>
                <input type="password" name="password" class="form-control" id="pwd">
            </div>
            <div class="form-group">
                <label for="role">Role:</label>
                <input type="text" class="form-control" id="role" name="role">
            </div>
            <button type="submit" class="btn btn-default">Add</button>
        </form>

Your controller method should get the user object like that. I just did a simple spring boot app to test and it worked.

Upvotes: 2

Related Questions