您好我到处寻找但找不到解决方案。 我在Jquery函数中进行ajax调用,并从spring输入表单提交数据:input 到控制器。 Controller通过JSR 303 Hibernate Validation验证日期,并通过JSON将错误返回给相同的Jquery函数(由Json接收数据)并在jsp中显示错误。 一切正常但显示的错误消息只是dafault或来自验证注释的消息参数。我想从ValidationMessages.properties文件中获取错误消息,我有该文件包含就绪消息,但显示消息是dafault而不是ValidationMessages.properties。我没有使用form:error标签,因为我想显示Json收到的错误。问题是坏消息不是来自文件而是显示为dafault。
我将在执行正常的JSR 303验证(没有Ajax和Json)时按照表单显示错误消息:错误标记一切正常,消息来自ValidationMessages.properties文件。
我的.jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Users using ajax</title>
<script src="<%=request.getContextPath() %>/js/jquery.js"></script>
<script type="text/javascript">
var contexPath = "<%=request.getContextPath() %>";
</script>
<script src="<%=request.getContextPath() %>/js/user.js"></script>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/style/app.css">
</head>
<body>
<h1>Add Users using Ajax ........</h1>
<form:form method="post" modelAttribute="user">
<table>
<tr><td colspan="2"><div id="error" class="error"></div></td></tr>
<tr><td>Name:</td> <td><form:input path="name" /></td>
<tr><td>Education</td> <td><form:input path="education" /></td>
<tr><td colspan="2"><input type="button" value="Add Users" onclick="doAjaxPost()"><br/></td></tr>
<tr><td colspan="2"><div id="info" class="success"></div></td></tr>
</table>
</form:form>
</body>
</html>
我在user.js中的doAjaxPost函数
function doAjaxPost() {
// get the form values
var name = $('#name').val();
var education = $('#education').val();
$.ajax({
type: "POST",
url: contexPath + "/AddUser.htm",
data: "name=" + name + "&education=" + education,
success: function(response){
// we have the response
if(response.status == "SUCCESS"){
userInfo = "<ol>";
for(i =0 ; i < response.result.length ; i++){
userInfo += "<br><li><b>Name</b> : " + response.result[i].name +
";<b> Education</b> : " + response.result[i].education;
}
userInfo += "</ol>";
$('#info').html("User has been added to the list successfully. " + userInfo);
$('#name').val('');
$('#education').val('');
$('#error').hide('slow');
$('#info').show('slow');
}else{
errorInfo = "";
for(i =0 ; i < response.result.length ; i++){
errorInfo += "<br>" + (i + 1) +". " + response.result[i].defaultMessage;
}
$('#error').html("Please correct following errors: " + errorInfo);
$('#info').hide('slow');
$('#error').show('slow');
}
},
error: function(e){
alert('Error: ' + e);
}
});
}
这是一个控制器
package com.raistudies.controllers;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.raistudies.domain.JsonResponse;
import com.raistudies.domain.User;
@Controller
public class UserController {
private List<User> userList = new ArrayList<User>();
@RequestMapping(value="/AddUser.htm",method=RequestMethod.GET)
public String showForm(Map model){
User user = new User();
model.put("user", user);
return "AddUser";
}
@RequestMapping(value="/AddUser.htm",method=RequestMethod.POST)
public @ResponseBody JsonResponse addUser(@ModelAttribute(value="user") @Valid User user, BindingResult result ){
JsonResponse res = new JsonResponse();
if(!result.hasErrors()){
userList.add(user);
res.setStatus("SUCCESS");
res.setResult(userList);
}else{
res.setStatus("FAIL");
res.setResult(result.getAllErrors());
}
return res;
}
}
用户类
package com.raistudies.domain;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.Size;
public class User {
@NotEmpty
private String name = null;
@NotEmpty
@Size(min=6, max=20)
private String education = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
}
这是ValidationMessage.properties文件
NotEmpty.user.name=Name of user cant be empty
NotEmpty.user.education = User education cant be empty
Size.education=education must hava between 6 and 20 digits
这是app-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/ValidationMessages" />
</bean>
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.raistudies" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven/>
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
这是一个截图 https://www.dropbox.com/s/1xsmupm1cgtv4x7/image.png?v=0mcns
如您所见,显示的默认错误消息不是来自ValidationMessages.properties文件。怎么解决?
答案 0 :(得分:1)
我知道这是一个非常古老的问题,但我一直试图解决这个问题。由于我刚刚发现解决了这个问题,我想分享它以帮助其他人可能坚持同样的事情。
请注意我使用 java config 执行此操作,因此我使用注释。
首先,在控制器上,向环境添加自动连接的链接。这是为了能够读取属性文件:
@Autowired
private Environment environment;
OP已经通过XML定义了messageSource,但在java配置中,它将在扩展WebMvcConfigurerAdapter
的配置类上。就我而言,它适用于:
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource msg= new ResourceBundleMessageSource();
msg.setUseCodeAsDefaultMessage(true);
msg.setBasename("PATH_TO_YOUR_MESSAGE_FILE");
msg.setDefaultEncoding("UTF-8");
return msg;
}
确保使用文件路径替换该部分。无论如何,现在要解决这个问题。回到控制器上,替换这一行:
res.setResult(result.getAllErrors());
对于这个,并调用一个新函数:
createErrorMessages(result.getFieldErrors());
现在,这是转换消息的新方法:
private LinkedList<CustomErrorMsg> createErrorMessages(List<FieldError> originalErrors) {
// Create a new list of error message to be returned
LinkedList<CustomErrorMsg> newErrors = new LinkedList<>();
// Iterate through the original errors
for(FieldError fe: originalErrors) {
// If the codes are null, then put the default message,
// if not, then get the first code from the property file.
String msg = (fe.getCodes() == null) ? fe.getDefaultMessage()
: environment.getProperty(fe.getCodes()[0]);
// Create the new error and add it to the list
newErrors.add(new CustomErrorMsg(fe.getField(), msg));
}
// Return the message
return newErrors;
}
if部分使其适用于没有代码的情况,以及在控制器上执行业务逻辑验证检查时,以及手动添加新字段错误。
此解决方法不适用于海关消息的参数。另一方面,如果碰巧有其他页面使用hibernate验证但不需要AJAX回调,则不需要使用此策略。这些只是默认工作。
我非常清楚这根本不是优雅,它可能会添加比预期更多的代码,但正如我之前所说的那样,它是一种“解决方法”。
答案 1 :(得分:0)
你可以试试这个:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="messageInterpolator" ref="messageSource"/>
</bean>
<mvc:annotation-driven validator="validator"/>
这个想法是明确告诉Validator使用哪个消息源。我自己没试过,但它可能值得一试。