当我们点击百里香的重置按钮时,如何清除错误信息

时间:2016-10-24 15:08:26

标签: spring-boot thymeleaf

我创建了一个显示用户信息的网页并注册了一个新用户。如果我们尝试输入现有用户,则会显示错误消息。当我刷新页面时,错误仍然存​​在。如何在刷新或单击重置按钮时清除错误消息。我正在使用spring Boot这是我的代码。

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>User Registration </title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
    th:href="@{/webjars/bootstrap/3.3.6/css/bootstrap.min.css}"
    rel="stylesheet" media="screen" />

</head>
<body>
    <div class="container">
        <div class="page-header" id="banner">
            <div class="row">
            <div class="col-lg-8 col-md-7 col-sm-6">
                <h1>Users</h1>
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>UserId</th>
                            <th>USerName</th>
                        </tr>
                    </thead>
                    <tbody border="0">
                        <tr  th:each="user :${users>
                            <td th:text="${user.userId}"></td>
                            <td th:text="${user.userName}" > </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            </div>
            <div class="row">
                <div class="col-lg-6 col-md-7 col-sm-6">
                    <h1>Register USer</h1>

                    <form th:action="@{/registerUser}" method="post" enctype="multipart/form-data" class="form-horizontal">
                        <fieldset>
                            <div class="form-group">
                                <label class="col-lg-3 control-label">Enter UserId</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="userId" value=""/>
                                </div>
                            </div>  
                            <div class="form-group">
                                <label class="col-lg-3 control-label">Enter UserName</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="userName" value=""/>
                                </div>
                            </div>  

                            <div class="form-group">
                                <div class="col-lg-12 col-lg-offset-3">
                                    <button type="submit" class="btn btn-primary" name="button">registerUSer</button>
                                    <button  type="reset" class="btn">Reset</button>
                                </div>
                            </div>
                             <div th:if="*{errorMessage != null}" class="alert alert-danger" th:text="*{errorMessage}">
                             </div>  
                        </fieldset>
                </form>
            </div>
        </div>

      </div>
   </div>

</body>
</html>

这是我的控制器:

 @RequestMapping(value = "/registerUser", method = RequestMethod.POST)
public String registrationForNewUSer(@RequestParam Long userId, @RequestParam String userName, Model model)
        throws Exception {
        //checking whether user already exist
    UserModel userInfo = userInfo.findOne(userId);
    if (userInfo != null) {
        String str = "user already exist" + userId;         
        model.addAttribute("errorMessage", str);
        return usersList(model);
    } else {
        // 
        return "redirect:/registerUser/" + userId + "/" + userName;
    }
}



 @RequestMapping(value = "/usersList", method = RequestMethod.GET)
    public String usersList(Model model) {
        model.addAttribute("users", userInfo.findAll());
        return "usersview";
    }
    @RequestMapping("/registerUser/{userId}/{userName}")
    public ResponseEntity<Object> authentication{
    // it does authenticationprocess and add user user in database and redirect to usersview.
    }

0 个答案:

没有答案