摘要:利用Struts來驗證前後台的資料
先前在ASP.NET MVC打滾了一段時間
後來公司的技術以Java-base為主
所以對於.NET在表單資料的防呆情有獨鍾
因此好奇在Java中有什麼方式可以像.NET用Validation Attribute一樣呢
後來發現原來Struts 有Validation功能
這次是用Validation Annotation以及Client Validation 來實作
Model是用Hibernate產出的
public class Company implements java.io.Serializable {
private Integer id;
private String name;
private String address;
private String contacter;
private String telephone;
private String email;
public Company() {
}
public Company(String name, String address, String contacter, String telephone, String email) {
this.name = name;
this.address = address;
this.contacter = contacter;
this.telephone = telephone;
this.email = email;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public String getContacter() {
return this.contacter;
}
public void setContacter(String contacter) {
this.contacter = contacter;
}
public String getTelephone() {
return this.telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
}
Jsp部分為
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<s:form action="company_finish" method="POST" validate="true" namespace="/action">
<div>
<input type="submit" value="完成">
</div>
<s:hidden name="company.id">
<div>
<s:label for="name" value="公司名稱">
<s:textfield id="name" name="company.name">
</s:textfield></s:label></div>
<div>
<s:label for="contacter" value="聯絡人">
<s:textfield id="contacter" name="company.contacter">
</s:textfield></s:label></div>
<div>
<s:label for="telephone" value="聯絡電話">
<s:textfield id="telephone" name="company.telephone">
</s:textfield></s:label></div>
<div>
<s:label for="email" value="email">
<s:textfield id="email" name="company.email" type="email">
</s:textfield></s:label></div>
<div>
<s:label for="address" value="地址">
<s:textfield id="address" name="company.address">
</s:textfield></s:label></div>
</s:hidden></s:form>
運用Annotation
第一種驗證是設定Validation在Method上
@Validation()
public class CompanyAction extends ActionSupport {
private CompanyDao companyDao = CompanyDao.getInstance();
private Company company = new Company();
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
@Validations(requiredStrings = {
@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "company.name", message = "公司名稱尚未填寫"),
@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "company.address", message = "地址尚未填寫"),
@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "company.contacter", message = "聯絡人尚未填寫"),
@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "company.telephone", message="聯絡電話尚未填寫"),
@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "company.email", message="Email尚未填寫")})
public String finish() {
if (company.getId() != null && company.getId() != 0) {
companyDao.updateCompany(company);
addActionMessage("修改完成");
} else {
companyDao.saveCompany(company);
addActionMessage("新增完成");
}
return SUCCESS;
}
}
結果輸出
第二種驗證設定將Validation的屬性設定在set 物件上
若不要驗證的方法則需設定SkipValidation屬性就不會進行驗證了
@Validation()
public class CompanyAction extends ActionSupport {
private CompanyDao companyDao = CompanyDao.getInstance();
private Company company = new Company();
private int id;
public int getId() {
return id;
}
@RequiredStringValidator(message = "ID不可為空")
public void setId(int id) {
this.id = id;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public List getCompanies() {
return companies;
}
@SkipValidation()
public String edit() {
if (id != 0)
company = companyDao.getCompany(id);
return "edit";
}
public String finish() {
if (company.getId() != null && company.getId() != 0) {
companyDao.updateCompany(company);
addActionMessage("修改完成");
} else {
companyDao.saveCompany(company);
addActionMessage("新增完成");
}
return SUCCESS;
}
}
Jsp的部分顯示驗證後訊息需要加上<s:fielderror/>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<s:form action="company_finish" method="POST" validate="true" namespace="/action" >
<div>
<input type="submit" value="完成" />
<s:fielderror/>
</div>
<s:hidden name="company.id" />
<div>
<s:label for="name" value="公司名稱" />
<s:textfield id="name" name="company.name" />
</div>
<div>
<s:label for="contacter" value="聯絡人" />
<s:textfield id="contacter" name="company.contacter" />
</div>
<div>
<s:label for="telephone" value="聯絡電話" />
<s:textfield id="telephone" name="company.telephone" />
</div>
<div>
<s:label for="email" value="email" />
<s:textfield id="email" name="company.email" type="email" />
</div>
<div>
<s:label for="address" value="地址" />
<s:textfield id="address" name="company.address" />
</div>
</s:form>
結果輸出
另一種Validation方法
用Client Validation來驗證需新增一個validation.xml
CompanyAction-company_finish-validation.xml內容
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="company.name">
<field-validator type="requiredstring">
<message>You must enter a name</message>
</field-validator>
</field>
</validators>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<s:form action="company_finish" method="POST" validate="true" >
<div>
<input type="submit" value="完成" />
<s:fielderror/>
</div>
<s:hidden name="company.id" />
<div>
<s:label for="name" value="公司名稱" />
<s:textfield id="name" name="company.name" />
</div>
<div>
<s:label for="contacter" value="聯絡人" />
<s:textfield id="contacter" name="company.contacter" />
</div>
<div>
<s:label for="telephone" value="聯絡電話" />
<s:textfield id="telephone" name="company.telephone" />
</div>
<div>
<s:label for="email" value="email" />
<s:textfield id="email" name="company.email" type="email" />
</div>
<div>
<s:label for="address" value="地址" />
<s:textfield id="address" name="company.address" />
</div>
</s:form>
Action的部分不需要任何設定
public class CompanyAction extends ActionSupport {
private CompanyDao companyDao = CompanyDao.getInstance();
private Company company = new Company();
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public String edit() {
if (id != 0)
company = companyDao.getCompany(id);
return "edit";
}
public String finish() {
if (company.getId() != null && company.getId() != 0) {
companyDao.updateCompany(company);
addActionMessage("修改完成");
} else {
companyDao.saveCompany(company);
addActionMessage("新增完成");
}
return SUCCESS;
}
}
結果輸出