我创建了以下Controller类:
package com.bilitutor.cct.control;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;
import com.bilitutor.cct.bean.*;
import com.bilitutor.cct.service.*;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
@Autowired
UserService userService;
@ModelAttribute("user")
public User getUserObect() {
return new User();
}
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Landing page. Just return the login.jsp
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(ModelMap model) {
logger.info("home() called");
model.addAttribute("emailErrorMessage", "Please enter Email");
return "login";
}
/**
* New User signup. If user already exists, send back the error, or send an email and forward to the email validation page
*/
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String signup(ModelMap model, @ModelAttribute("user")User user, BindingResult result) {
logger.info("signup() : email="+user.getEmail()+" pass="+user.getPassword()+" accessCode="+user.getAccessCode());
try {
userService.addUser(user);
} catch (DataIntegrityViolationException e) {
logger.info("Email already exists");
model.addAttribute("emailErrorMessage", "Email already exists");
}
return "login";
}
}
以下JSP
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<body>
<form:form id="mainForm" method="post" commandName="user">
<table id="mainTable">
<tr id="emailRow"><td rowspan="2" class="lrPadding">Email</td><td class="lrPadding"><form:input id="email" path="email" class="editable"/></td></tr>
<tr id="emailErrorRow"><td id="emailError" class="errorMessage">${emailErrorMessage}</td></tr>
</table>
</form:form>
</body>
</html>
当我第一次加载页面时,我可以在login.jsp上看到“请输入电子邮件”的消息。在使用现有电子邮件点击/ signup方法后,我可以在返回的登录页面上看到“电子邮件已存在”日志消息,但保留“请输入电子邮件”消息。有人可以找出原因。是否有可能login.jsp只编译一次,并在我第一次调用时被缓存到某个地方,而我看到的因为命中/注册而导致的是缓存页面。如果是这样,我该如何解决这个问题。
点击signup()方法后出现的登录页面的html为:
<html style="background-image:url('/cct/resources/images/wallpaper.jpg')">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="/cct/resources/css/master.css" type="text/css" media="screen" />
<script type="text/javascript" src="/cct/resources/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/cct/resources/js/login.js"></script>
<title>BiliTutor Content Creator Toolkit</title>
</head>
<body>
<h3> emailErrorMessage = Please enter Email</h3>
<form id="mainForm" action="/cct/" method="post">
<table id="mainTable" class="contents roundBox">
<tr id="logoRow"><td colspan="2" style="text-align:center;padding:20px;width:300px;height:100px"><img id="imgLogo" src="/cct/resources/images/logo.png"></td></tr>
<tr id="emailRow"><td rowspan="2" class="lrPadding">Email </td><td class="lrPadding"><input id="email" name="email" class="editable" type="text" value=""/></td></tr>
<tr id="emailErrorRow"><td id="emailError" class="errorMessage">Please enter Email</td></tr>
<tr id="passwordRow"><td rowspan="2" class="lrPadding">Password</td><td class="lrPadding"><input id="password" name="password" class="editable" type="password" value=""/></td></tr>
<tr id="passwordErrorRow"><td id="passwordError" class="errorMessage">Password should be atleast 8 char long</td></tr>
<tr id="confirmPasswordRow"><td rowspan="2" class="lrPadding">Confirm<br/>Password</td><td class="lrPadding"><input id="confirmPassword" type="password" class="editable"/></td></tr>
<tr id="confirmPasswordErrorRow"><td id="confirmPasswordError" class="errorMessage">Passwords dont match</td></tr>
<tr id="accessCodeRow"><td rowspan="2" class="lrPadding">Access<br/>Code</td><td class="lrPadding"><input id="accessCode" name="accessCode" class="editable" type="text" value=""/></td></tr>
<tr id="accessCodeErrorRow"><td id="accessCodeError" class="errorMessage">Access Code must be atleast 8 char long</td></tr>
<tr id="buttonsRow"><td colspan="2"><table style="margin:0px;margin-top:1em;padding:0px;width:100%"><tr>
<td><div id="btnNewUser" class="button" style="float:left">New Tutor<br/>Signup</div></td>
<td><div id="btnForgotPassword" class="button">Forgot<br/>Password</div></td>
<td><div id="btnLogin" class="button" style="float:right">Tutor<br/>Login</div></td>
</tr></table></td></tr>
</table>
</form>
</body>
</html>
我的IDE看起来像:
答案 0 :(得分:0)
我会将方法签名更改为
public String signup(ModelMap model, @ModelAttribute("user")User user, BindingResult result) {
将您的错误直接添加到模型
} catch (DataIntegrityViolationException e) {
logger.info("Email already exists");
model.addAttribute("emailErrorMessage", "Email already exists");
}
return "login";
然后在jsp
中像这样访问它<tr id="emailErrorRow"><td id="emailError" class="errorMessage">${emailErrorMessage}</td></tr>
答案 1 :(得分:0)
我建议使用BindingResult result
而不是自己的错误地图:
bindingResult.rejectValue("email", "errorMessageCode", "Email already exists");