以这个表格为例:
<form id="login_form" method="post" action="/rest/auth/auth">
<div id="login_email" class="row margin-top-50">
<div class="col-md-12" id="input_container">
<input id="login_email_input" class="input_edit input_edit_img_width" type="email" name="email" th:placeholder="#{label.common_email}"></input>
<img class="input_edit_img" src="/img/ic_login_user.png"></img>
</div>
</div>
<div id="login_password" class="row margin-top-15">
<div class="col-md-12" id="input_container">
<input id="login_password_input" class="input_edit input_edit_img_width" type="password" name="password" th:placeholder="#{label.common_password}"></input>
<img class="input_edit_img" src="/img/ic_login_pw.png"></img>
</div>
</div>
<div class="row margin-top-15">
<div class="col-md-12">
<div class="checkbox_square">
<input id="check_password_show" type="checkbox" value=""></input>
<label for="check_password_show"></label>
<span th:text="#{label.common_password_show}"></span>
</div>
</div>
</div>
<div class="row margin-top-15">
<div class="col-md-12">
<button id="login_submit" type="submit" class="btn btn-default btn-blue" th:text="#{label.login}"></button>
</div>
</div>
</form>如您所见,im使用本地化消息(例如th:placeholder="#{label.common_email}" )。
现在我想使用jQuery验证,我确实需要像这样使用来自messages.properties的本地化字符串:
$("#login_form").validate({
rules: {
email: {
required: true,
email: true
},
password: {
required: true
/*minlength: 6,
maxlength: 20*/
}
},
messages: {
email: {
required: "#{label.input_missing_email}",
email: "#{label.input_missing_email_valid}"
},
password: {
required: "#{label.input_missing_password}",
minlength: "#{label.input_missing_password_valid}"
}
}
});有任何方法可以从客户端javascript访问我的spring本地化消息吗?或者,如果我必须使用本地化字符串,那么我应该使用不同的方法进行验证吗?
发布于 2016-06-17 12:16:50
您不需要在javascript级别从您的messages.properties中检索您的地区字符串,您可以很容易地做到这一点,如果您仅将您的地区字符串保存在HTML中,下面我将这样做:
HTML:
<span>或<div> (如果您想在该元素之后显示一些msg,一些验证类)<span>和<div>中。联署材料:
<span>验证时,只需显示/隐藏相应的<div>或jQuery。发布于 2016-06-17 12:23:05
jQuery.i18n.properties是一个轻量级的jQuery插件,用于从‘.properties’文件中提供javascript的国际化,就像在javascript中一样。它根据所提供的语言和国家代码加载和分析资源包(.properties)。更多
假设
label.delete.success =Successfully Deleted
label.delete.failure=Error in success是您的messages.properties属性文件。
然后jQuery.i18n.prop(label.delete.success)从javascript文件返回Successfully Deleted。
希望这能有所帮助。
https://stackoverflow.com/questions/37880937
复制相似问题