1. 首先在src文件夹下面建立如:ValidationMessages.properties的文件,其文件的扩展名为properties。在其中定义要验证的信息。比如':
errors.required={field}为必须输入项目
errors.email=email格式不正确
errors.qq=qq格式不正确
errors.zip=价格必须为数字格式
2. 然后建立一个包建立一java文件,比如:
包名为:cn.agriculture.web.form
文件名为:ExpressListForm.java,所编写的代码可为:
package cn.agriculture.web.form;
import cn.agriculture.common.validator.constraints.NotEmpty;
import lombok.Data;
@Data
public class ExpressListForm {
private String storage;
@NotEmpty(field="快递单号开始", message="{errors.required}")
private String expressListIdStart;
@NotEmpty(field="快递单号结束", message="{errors.required}")
private String expressListIdEnd;
}
3. 具体验证的代码可以这样写(在controller中):
@RequestMapping(value = "addExpressList", method = RequestMethod.POST)
public String executeAddExpressList(Model model, HttpSession session, @Valid @ModelAttribute("expressListForm") ExpressListForm expressListForm, BindingResult results) throws SQLException, IOException {
if (results.hasErrors()) {
log.info("内容验证出错");
List<Item> expressList = itemListComponent.getExpressList();
model.addAttribute("expressList", expressList);
//初始化ExpressListForm
return "manager/expressList/addExpressList";
}
4.前台页面.
<form:form modelAttribute="expressListForm" action="addExpressList" method="post">
<div class="alert alert-info">${message}<form:errors path="*"></form:errors></div>
<table class="table table-bordered responsive">
<tr>
<td style="background-color: #f9f9f9;">快递单号开始</td>
<td><form:input path="expressListIdStart" cssClass="form-control" cssErrorClass="form-control error"/></td>
<td style="background-color: #f9f9f9;">快递单号结束</td>
<td><form:input path="expressListIdEnd" cssClass="form-control" cssErrorClass="form-control error"/></td>
<td style="background-color: #f9f9f9;"></td>
<td></td>
</tr>
</table>
</form:form>
5. 可能会用到一些jar包
6。以下文件的配置
sqlMapConfig.xml,springMVC-servlet.xml,web.xml, context.xml
具体配置可以查看项目
7.还需的文件