注解验证
http://www.opensymphony.com/webwork/wikidocs/J2SE%205%20Support.html
http://www.opensymphony.com/webwork/wikidocs/Validation%20Annotation.html
WEBWORK中需要加入包xwork-tiger
一般有两种注解法
1.直接注在get方法上面,像这种
@RequiredStringValidator(message="Supply password")
public String getPassword() {
return password;
}
2.注在方法上面,like
@Validations(
requiredStrings={
@RequiredStringValidator(fieldName="username",message="用户名不能为空!" key="i18n,化功能"),
@RequiredStringValidator(fieldName="telNum",message="电话号码不能为空!")
},
regexFields={@RegexFieldValidator(fieldName="telNum",expression="^(\\+86|0|1)\\d{10,11}$",
message="电话号码格式不正确!")}
)
package com.gumtreenet.company.action;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Timestamp;
import java.util.List;
import com.gumtreenet.company.base.Page;
import com.gumtreenet.company.service.IDdSubService;
import com.gumtreenet.company.service.IDownloadlibraryService;
import com.gumtreenet.company.vo.Downloadlibrary;
import com.gumtreenet.company.vo.Resume;
import com.opensymphony.webwork.dispatcher.json.JSONObject;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork.validator.annotations.Validations;
/**
* 附件action
*
* @author 李义星
* @version 0.1
* @datetime 2011-03-22
*/
public class DownloadlibraryAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private final static String DDMAINNAME = "下载类型";
private Downloadlibrary downlib;
private IDownloadlibraryService downService;
private IDdSubService ddSubService;
private FileInputStream downIn;
private File down;
private String downFileName;
private String downContentType;
private Page page;
private JSONObject jsonObject;
private Timestamp start;
private Timestamp end;
private List<Resume> findDowns;
public Downloadlibrary getDownlib() {
return downlib;
}
public void setDownlib(Downloadlibrary downlib) {
this.downlib = downlib;
}
public IDownloadlibraryService getDownService() {
return downService;
}
public void setDownService(IDownloadlibraryService downService) {
this.downService = downService;
}
public IDdSubService getDdSubService() {
return ddSubService;
}
public void setDdSubService(IDdSubService ddSubService) {
this.ddSubService = ddSubService;
}
public FileInputStream getDownIn() {
return downIn;
}
public void setDownIn(FileInputStream downIn) {
this.downIn = downIn;
}
public File getDown() {
return down;
}
public void setDown(File down) {
this.down = down;
}
public String getDownFileName() {
return downFileName;
}
public void setDownFileName(String downFileName) {
this.downFileName = downFileName;
}
public String getDownContentType() {
return downContentType;
}
public void setDownContentType(String downContentType) {
this.downContentType = downContentType;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public JSONObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
public Timestamp getStart() {
return start;
}
public void setStart(Timestamp start) {
this.start = start;
}
public Timestamp getEnd() {
return end;
}
public void setEnd(Timestamp end) {
this.end = end;
}
public List<Resume> getFindDowns() {
return findDowns;
}
public void setFindDowns(List<Resume> findDowns) {
this.findDowns = findDowns;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件标题"),
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件") })
public String addDown() {
return SUCCESS;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请1"),
@RequiredStringValidator(fieldName = "downFileName", message = "") })
public String deleteDown() {
return SUCCESS;
}
}
但无论是哪种注解,默认情况下都是对这个action的所有方法进行验证。
如上面的图,我虽然给deleteDown和addDown两个方法都写了对应的验证注解,但是结果是访问delete的时候,add的验证也起作用了。
在struts2中好解决
<interceptor name="validation" key="i18n,化功能"),
@RequiredStringValidator(fieldName="telNum",message="电话号码不能为空!")
},
regexFields={@RegexFieldValidator(fieldName="telNum",expression="^(\\+86|0|1)\\d{10,11}$",
message="电话号码格式不正确!")}
)
package com.gumtreenet.company.action;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Timestamp;
import java.util.List;
import com.gumtreenet.company.base.Page;
import com.gumtreenet.company.service.IDdSubService;
import com.gumtreenet.company.service.IDownloadlibraryService;
import com.gumtreenet.company.vo.Downloadlibrary;
import com.gumtreenet.company.vo.Resume;
import com.opensymphony.webwork.dispatcher.json.JSONObject;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork.validator.annotations.Validations;
/**
* 附件action
*
* @author 李义星
* @version 0.1
* @datetime 2011-03-22
*/
public class DownloadlibraryAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private final static String DDMAINNAME = "下载类型";
private Downloadlibrary downlib;
private IDownloadlibraryService downService;
private IDdSubService ddSubService;
private FileInputStream downIn;
private File down;
private String downFileName;
private String downContentType;
private Page page;
private JSONObject jsonObject;
private Timestamp start;
private Timestamp end;
private List<Resume> findDowns;
public Downloadlibrary getDownlib() {
return downlib;
}
public void setDownlib(Downloadlibrary downlib) {
this.downlib = downlib;
}
public IDownloadlibraryService getDownService() {
return downService;
}
public void setDownService(IDownloadlibraryService downService) {
this.downService = downService;
}
public IDdSubService getDdSubService() {
return ddSubService;
}
public void setDdSubService(IDdSubService ddSubService) {
this.ddSubService = ddSubService;
}
public FileInputStream getDownIn() {
return downIn;
}
public void setDownIn(FileInputStream downIn) {
this.downIn = downIn;
}
public File getDown() {
return down;
}
public void setDown(File down) {
this.down = down;
}
public String getDownFileName() {
return downFileName;
}
public void setDownFileName(String downFileName) {
this.downFileName = downFileName;
}
public String getDownContentType() {
return downContentType;
}
public void setDownContentType(String downContentType) {
this.downContentType = downContentType;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public JSONObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
public Timestamp getStart() {
return start;
}
public void setStart(Timestamp start) {
this.start = start;
}
public Timestamp getEnd() {
return end;
}
public void setEnd(Timestamp end) {
this.end = end;
}
public List<Resume> getFindDowns() {
return findDowns;
}
public void setFindDowns(List<Resume> findDowns) {
this.findDowns = findDowns;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件标题"),
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件") })
public String addDown() {
return SUCCESS;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请1"),
@RequiredStringValidator(fieldName = "downFileName", message = "") })
public String deleteDown() {
return SUCCESS;
}
}
但无论是哪种注解,默认情况下都是对这个action的所有方法进行验证。
如上面的图,我虽然给deleteDown和addDown两个方法都写了对应的验证注解,但是结果是访问delete的时候,add的验证也起作用了。
在struts2中好解决
<interceptor name="validation" />
4个文件代码
http://dl.iteye.com/topics/download/4142f2ff-2d36-3c9e-a004-ba35d3d0e275 1 楼 liyixing1 2011-04-30 没人看,自己顶下。