struts2笔记

来源:互联网 发布:中国城镇住户调查数据 编辑:程序博客网 时间:2024/06/16 05:14
自定义验证
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator Definition 1.0//EN"
        "http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd">


<!-- START SNIPPET: validators-default -->
<validators>
    <validator name="idcard" class="com.atguigu.struts2.validation.app.IDCardValidator"/>
</validators>
4、TestValidationAction-testValidation-validation.xml
<field name="idCard">
field-validator type="idcard">
<message>It is not a idCard!</message>
</field-validator>
</field>


表单重复提交
1、页面加<s:token>标签
2、struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">


<struts>

<!-- 配置国际化资源文件 -->
<constant name="struts.custom.i18n.resources" value="i18n"></constant>

    <package name="default" namespace="/" extends="struts-default">

<interceptors>

<interceptor name="hello" class="com.atguigu.struts2.interceptors.MyInterceptor"></interceptor>

<interceptor-stack name="atguigustack">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">2097152</param>
<!--  
<param name="fileUpload.allowedTypes">text/html,text/xml</param>
<param name="fileUpload.allowedExtensions">html,dtd,xml</param>
-->
</interceptor-ref>
</interceptor-stack>

</interceptors>

<default-interceptor-ref name="atguigustack"></default-interceptor-ref>

<action name="testUpload" 
class="com.atguigu.struts2.upload.app.UploadAction">
<result>/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>

<action name="testDownload" class="com.atguigu.struts2.download.app.DownLoadAction">
<result type="stream">
<param name="bufferSize">2048</param>
</result>
</action>

<action name="testToken" class="com.atguigu.struts2.token.app.TokenAction">
<interceptor-ref name="hello"></interceptor-ref>
<interceptor-ref name="tokenSession"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>/success.jsp</result>
<result name="invalid.token">/token-error.jsp</result>
</action>

    </package>




</struts>


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


2、mothod="post"


3、<input type="file">或<s:file>


4、private List<File> ppt;
   private List<String> pptContentType;
   private List<String> pptFileName;


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


6、对上传的文件进行限制吗 ? 例如扩展名, 内容类型, 上传文件的大小 ? 若可以, 则若出错, 显示什么错误消息呢 ? 消息可以定制吗 ? 
可以通过配置 FileUploadInterceptor 拦截器的参数的方式来进行限制
maximumSize (optional) - 默认的最大值为 2M. 上传的单个文件的最大值
allowedTypes (optional) - 允许的上传文件的类型. 多个使用 , 分割
allowedExtensions (optional) - 允许的上传文件的扩展名. 多个使用 , 分割.
<interceptors>
<interceptor name="hello" class="com.atguigu.struts2.interceptors.MyInterceptor"></interceptor>
<interceptor-stack name="atguigustack">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">2097152</param>
<!--  
<param name="fileUpload.allowedTypes">text/html,text/xml</param>
<param name="fileUpload.allowedExtensions">html,dtd,xml</param>
-->
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="atguigustack"></default-interceptor-ref>


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;
}
}
<action name="validationAction" class="com.struts2.validation.app.ValidationAction">
<interceptor-ref name="hello"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>/index.jsp</result>
<result name="input">/input.jsp</result>
</action>
0 0
原创粉丝点击