定义一个action。.
TestAction.java
package com.zking.study.five;
/**
* 用于测试的action,与普通action没啥区别
*/
public class TestAction{
public String execute() {
System.out.println("进入了InterceptorAction的execute方法。");
return null;
}
定义一个拦截器,实现Interceptor接口,( com.opensymphony.xwork2.interceptor.Interceptor )
TestInterceptor.java
package com.zking.study.five;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
* 用于测试的拦截器,实现Interceptor接口
*/
public class TestInterceptor implements Interceptor{
@Override
public void destroy() {
System.out.println("TestInterceptor is destroying.");
}
@Override
public void init() {
System.out.println("TestInterceptor is initing.");
}
@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("拦截的类:"+arg0.getAction().getClass().getName());
System.out.println("调用的Action是:"+arg0.getProxy().getActionName());
System.out.println("调用的方法是:"+arg0.getProxy().getMethod());
arg0.invoke();
return null;
}
}
struts中的配置文件 struts-sy.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sy" extends="base" namespace="/sy">
<!-- 拦截器定义在package标签下,于action同级 -->
<interceptors>
<interceptor name="testInterceptor" class="com.zking.study.five.TestInterceptor"></interceptor>
</interceptors>
<!-- 配置我们之前写的那个测试action -->
<action name="testAction" class="com.zking.study.five.TestAction">
<!-- 将之前定义好的拦截器配置到action里,(可配置多个) -->
<interceptor-ref name="testInterceptor"></interceptor-ref>
</action>
</package>
</struts>
Interceptor
implements Interceptor
extends AbstractInterceptor
与filter的区别:先过filter再过interceptor
org.apache.struts2.interceptor.FileUploadInterceptor
文件上传:
三种上传方案
虚拟路径与真实路径 /upload
copyFile与copydirectory
0. 文件下载
另存为
直接打开
-
内容类型
response.setContentType(d.getMime()); -
设置响应头
response.setHeader(“Content-Disposition”,“attachment;filename=” + fileName);//文件名 -
处理文件名的中文乱码
String fileName = d.getFileName();
fileName = new String(fileName.getBytes(“utf-8”), “iso8859-1”); -
struts2文件上传大小设置
-
struts2文件上传类型设置
image/png,image/gif,image/jpeg
根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制
-
其它
enctype=“multipart/form-data” method=“post”
private File file;
private String fileContentType;
private String fileFileName;