struts2笔记

自定义验证
1、自定义类
public class IDCardValidator extends FieldValidatorSupport {


@Override
public void validate(Object object) throws ValidationException {
//1. 获取字段的名字和值
String fieldName = getFieldName();
        Object value = this.getFieldValue(fieldName, object);
        
//2. 验证
IDCard idCard = new IDCard();
boolean result = idCard.Verify((String)value);
        
//3. 若验证失败, 则 ...
if(!result){
addFieldError(fieldName, object);
}

}


}
2、验证代码
package com.atguigu.struts2.validation.app;


public class IDCard {
final int[] wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
final int[] vi = { 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 };
private int[] ai = new int[18];


public IDCard() {}


public boolean Verify(String idcard) {
if (idcard.length() == 15) {
idcard = uptoeighteen(idcard);
}
if (idcard.length() != 18) {
return false;
}
String verify = idcard.substring(17, 18);
if (verify.equals(getVerify(idcard))) {
return true;
}
return false;
}


public String getVerify(String eightcardid) {
int remaining = 0;


if (eightcardid.length() == 18) {
eightcardid = eightcardid.substring(0, 17);
}


if (eightcardid.length() == 17) {
int sum = 0;
for (int i = 0; i < 17; i++) {
String k = eightcardid.substring(i, i + 1);
ai[i] = Integer.parseInt(k);
}


for (int i = 0; i < 17; i++) {
sum = sum + wi[i] * ai[i];
}
remaining = sum % 11;
}


return remaining == 2 ? "X" : String.valueOf(vi[remaining]);
}


public String uptoeighteen(String fifteencardid) {
String eightcardid = fifteencardid.substring(0, 6);
eightcardid = eightcardid + "19";
eightcardid = eightcardid + fifteencardid.substring(6, 15);
eightcardid = eightcardid + getVerify(eightcardid);
return eightcardid;
}

public static void main(String[] args) {

String idcard1 = "350211197607142059"; 
String idcard2 = "350211197607442059";

IDCard idcard = new IDCard(); 
System.out.println(idcard.Verify(idcard1)); 
System.out.println(idcard.Verify(idcard2)); 
}


}
3、创建validators.xml

        "-//Apache Struts//XWork Validator Definition 1.0//EN"
        "http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd">




   

4、TestValidationAction-testValidation-validation.xml

field-validator type="idcard">
It is not a idCard!




表单重复提交
1、页面加标签
2、struts.xml

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">







   







2097152








class="com.atguigu.struts2.upload.app.UploadAction">
/success.jsp
/upload.jsp




2048







/success.jsp
/token-error.jsp


   








文件上传
1、Html表单的enctype="multipart/form-data"


2、mothod="post"


3、


4、private List ppt;
   private List pptContentType;
   private List pptFileName;


5、回显问题修改页面下标


6、对上传的文件进行限制吗 ? 例如扩展名, 内容类型, 上传文件的大小 ? 若可以, 则若出错, 显示什么错误消息呢 ? 消息可以定制吗 ? 
可以通过配置 FileUploadInterceptor 拦截器的参数的方式来进行限制
maximumSize (optional) - 默认的最大值为 2M. 上传的单个文件的最大值
allowedTypes (optional) - 允许的上传文件的类型. 多个使用 , 分割
allowedExtensions (optional) - 允许的上传文件的扩展名. 多个使用 , 分割.




2097152







7、定制错误消息. 可以在国际化资源文件中定义如下的消息:
struts.messages.error.uploading - 文件上传出错的消息
struts.messages.error.file.too.large - 文件超过最大值的消息
struts.messages.error.content.type.not.allowed - 文件内容类型不合法的消息
struts.messages.error.file.extension.not.allowed - 文件扩展名不合法的消息


8、 文件的下载:


1). Struts2 中使用 type="stream" 的 result 进行下载即可
2). 具体使用细节参看 struts-2.3.15.3-all/struts-2.3.15.3/docs/WW/docs/stream-result.html
3). 可以为 stream 的 result 设定如下参数


contentType: 结果类型
contentLength: 下载的文件的长度
contentDisposition: 设定 Content-Dispositoin 响应头. 该响应头指定接应是一个文件下载类型, 一般取值为  attachment;filename="document.pdf".
inputName: 指定文件输入流的 getter 定义的那个属性的名字. 默认为 inputStream
bufferSize: 缓存的大小. 默认为 1024
allowCaching: 是否允许使用缓存 
contentCharSet: 指定下载的字符集 


9、自定义拦截器
public class MyInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("aaaaaa");
String result = invocation.invoke();
System.out.println("bbbbbb");
return result;
}
}



/index.jsp
/input.jsp

你可能感兴趣的:(struts2笔记)