java - How to bind data from velocity template to spring controller? -


i have velocity template 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> 

, need pass entered data controller. here 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 , haven't figured out in framework yet. i've google long time unsuccessful. please velocity guru!

the problem see here not velocity or spring. form inputs not have names, seems misplaced name type in form. it's input names that's sent controller. want create user model , make sure has same variable names form input names.

 public class user {     private string username;     private string password;     private string role;     // add getter , setter methods     // add tostring method } 

and form should 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 user object that. did simple spring boot app test , worked.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -