1、攔截器
定義一個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>
在上面第二段代碼塊TestInterceptor.java中,destroy方法爲攔截器被銷燬時調用的方法、init方法爲攔截器初始化時調用的方法,前面兩個方法都沒什麼好說的,最後一個方法以及它的參數纔是重點。
intercept(ActionInvocation arg0) ,在這裏就不多解釋了: https://blog.csdn.net/beliefyou8/article/details/51956279
2、文件上傳、下載
在下面的demo中是用到了一個jar包的 commons-io-2.5.jar,使用前需要先將jar包導入進去,不導jar也行,需自己寫文件操作的代碼。
<!-- struts中的s標籤 -->
<s:form action="fileAction_upload" namespace="/sy" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</s:form>
定義上傳、下載、打開文件的類
package com.zking.study.five;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.zking.study.four.base.BaseAction;
/**
* 文件上傳下載
*/
public class FileAction extends BaseAction{
//這是jsp頁面傳遞過來的具體文件
private File file;
//文件名
private String fileFileName;
//文件類型
private String fileContentType;
//虛擬路徑
private String serverDir="/upload";
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
//上傳圖片到tomcat
public String upload() {
String realPath = getRealPath(serverDir+"/"+fileFileName);
try {
FileUtils.copyFile(file, new File(realPath));
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
//得到服務器中的真實路徑
private String getRealPath(String path) {
return this.request.getServletContext().getRealPath(path);
}
//保存圖片
public String saveAs() {
String fileName="stationmaster_img.png";
String fileType="image/png";
this.response.setContentType(fileType);
this.response.setHeader("Content-Disposition","attachment;filename=" + fileName);
String realPath=getRealPath(serverDir+"/"+fileName);
try {
//commons-io-2.5.jar 中的方法,不使用該方法則需自己寫文件操作的方法。
FileUtils.copyFile(new File(realPath), response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//打開圖片
public String openAs() {
String fileName="stationmaster_img.png";
String fileType="image/png";
this.response.setContentType(fileType);
this.response.setHeader("Content-Disposition","filename=" + fileName);
String realPath=getRealPath(serverDir+"/"+fileName);
try {
FileUtils.copyFile(new File(realPath), response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
BaseAction.java
package com.zking.study.four.base;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
public class BaseAction implements ServletRequestAware, ServletResponseAware{
/**
* 爲了傳值使用
*/
protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session;
protected ServletContext application;
/**
* 爲了配置跳轉頁面所用
*/
protected final static String SUCCESS="success";
protected final static String FAIL="fail";
protected final static String LIST="list";
protected final static String ADD="add";
protected final static String PROADD="proAdd";
protected final static String EDIT="edit";
protected final static String PROEDIT="proEdit";
protected final static String DEL="del";
protected final static String TOLIST="tolist";
protected final static String DETAIL="detail";
/**
* 具體傳值字段 後端向jsp頁面傳值所用字段
*/
protected Object result;
protected Object msg;
protected int code;
public Object getResult() {
return result;
}
public Object getMsg() {
return msg;
}
public int getCode() {
return code;
}
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
}
點擊上傳後跳轉的頁面
<body>
<img src="${pageContext.request.contextPath}/sy/fileAction_openAs.action">
<a href="${pageContext.request.contextPath}/sy/fileAction_saveAs.action">點擊下載</a>
</body>
struts2配置文件
<?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">
<action name="fileAction_*" class="com.zking.study.five.FileAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
上傳文件是有兩個概念需要理解的,虛擬路徑與真實路徑的概念?
相對於你所在的開發電腦而言,你自己電腦上有的就是真實路徑,你電腦上沒有的路徑就是虛擬的。