0
當我嘗試使用彈簧驗證時,不顯示錯誤消息。你能幫忙嗎?彈簧驗證錯誤消息不顯示
我的jsp:
<form:form method="POST" modelAttribute="userInfo">
<form:label path="redirectURL">Hybris URL</form:label>:
<form:input path="redirectURL" placeholder="請填寫Hybris管理平臺地址" />
<form:errors path="redirectURL" class="error" /><br />
<form:label path="email" >Last Name</form:label>:
<form:input path="email" placeholder="請填寫郵箱地址" />
<form:errors path="email" class="error" /><br/>
<form:input type="hidden" path="taobaoUserId" value= "${taobaouserid}" />
<input type="submit" class="button" value="提交" style="margin-right: 15px;" />
我的驗證:
import javax.annotation.Resource;
import org.apache.commons.validator.EmailValidator;
import org.apache.commons.validator.UrlValidator;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.hybris.integration.model.UserInfo;
@Component
public class UserInfoValidator implements Validator {
EmailValidator emailvalidator = EmailValidator.getInstance();
@Override
public boolean supports(Class<?> arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public void validate(Object target, Errors errors) {
UserInfo userInfo = (UserInfo) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "redirectURL",
"NotEmpty.userInfoForm.redirectURL");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "NotEmpty.userInfoForm.email", "Input info is wrong");
if (!emailvalidator.isValid(userInfo.getEmail())) {
errors.rejectValue("email", "Pattern.userInfoForm.email","Input info is wrong");
}
if (!userInfo.getRedirectURL().startsWith("http")) {
errors.rejectValue("redirectURL", "StartWithHttp.userInfoForm.url","Input info is wrong");
}
if(validateUrl(userInfo.getRedirectURL())){
errors.rejectValue("redirectURL", "StartWithHttp.userInfoForm.url","Input info is wrong");
}
}
private boolean validateUrl(String url) {
String[] schemes = { "http", "https" };
UrlValidator urlValidator = new UrlValidator(schemes);
return urlValidator.isValid(url);
}
}
我的控制器:
@RequestMapping(value = "redirecttohybris", method = RequestMethod.POST)
public String redirectToInput(Model model,@ModelAttribute("userInfo")
UserInfo userInfo, BindingResult result)
{
userInfoValidator.validate(userInfo, result);
LOGGER.info(userInfo.getTaobaoUserId().toString());
if (result.hasErrors()) {
model.addAttribute("userInfo", new UserInfo());
return "redirectInfo";
}
return "redirect:" + userInfo.getRedirectURL();
}
當我調試,我可以看到錯誤的相關信息在控制中的「結果」中LER。但是,該消息未在前端顯示。我想有一些問題。任何人都可以幫忙嗎?
因爲要重定向....模式沒有生存的重定向。 –
我只在沒有錯誤時重定向。如果發現錯誤,只需返回一個頁面名稱即可。 – molly
錯過了。刪除'Model'屬性。 'BindingResult'用於模型,您不需要重新分配'userInfo',因爲它已經是模型的一部分。 –