Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将数据从velocity模板绑定到spring控制器?_Java_Spring_Spring Mvc_Velocity - Fatal编程技术网

Java 如何将数据从velocity模板绑定到spring控制器?

Java 如何将数据从velocity模板绑定到spring控制器?,java,spring,spring-mvc,velocity,Java,Spring,Spring Mvc,Velocity,我有一个带有标记形式的velocity模板 <div class="row"> <div class="container"> <div class="col-lg-12"> <table class=" table table-striped"> <thead> <th>#</th> <th>userna

我有一个带有标记形式的velocity模板

<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>

我是velocity方面的新手,还没有在这个框架中找到答案。我用谷歌搜索了很长时间,但没有成功。请帮助速度大师

我在这里看到的问题不仅仅是速度或弹簧。您的表单输入没有名称,似乎您在表单中错放了类型的名称。发送到控制器的是输入名称。您要做的是创建一个用户模型,并确保它具有与表单输入名称相同的变量名称

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

   // Add getter and setter methods

   // Add toString method
}
你的表格应该是

<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>

用户名:
密码:
角色:
添加

控制器方法应该像这样获取用户对象。我刚刚做了一个简单的spring boot应用程序来测试,它成功了。

非常感谢!你使我的一天)很高兴帮助。请接受并投票回答:)
<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>