拦截器改进
/20171108_chr_newdownInterceptor/src/nuc/sw/action/DownloadAction.java
package nuc.sw.action;
import com.opensymphony.xwork2.ActionSupport;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
public class DownloadAction extends ActionSupport
{
private String inputPath;
private String contentType;
private String downFileName;
public String getContentType () {
return contentType;
}
public String getDownFileName () {
return downFileName;
}
public String getInputPath () {
return inputPath;
}
public void setContentType (String contentType) {
this .contentType = contentType;
}
public void setDownFileName (String downFileName) throws UnsupportedEncodingException {
this .downFileName = new String(downFileName.getBytes("iso8859-1" ),"utf-8" );
}
public void setInputPath (String inputPath) throws UnsupportedEncodingException {
this .inputPath = new String(inputPath.getBytes("iso8859-1" ),"utf-8" );
}
public InputStream getTargetFile () {
InputStream is = null ;
try {
is = new FileInputStream(inputPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return is;
}
}
/20171108_chr_newdownInterceptor/src/nuc/sw/action/LoginAction.java
package nuc.sw.action;
import java.util.regex.Pattern;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String getUsername () {
return username;
}
public void setUsername (String username) {
this .username = username;
}
public String getPassword () {
return password;
}
public void setPassword (String password) {
this .password = password;
}
@Override
public void validate () {
if (username.trim().length()<6 ||username.trim().length()>15 ||username==null )
{
this .addFieldError("username" , "用户名长度不合法!" );
}
if (password.trim().length()<6 ||password.trim().length()>15 ||password==null )
{
this .addFieldError("password" , "密码长度不合法!" );
}
}
public String loginMethod () {
if (username.equals("chenghaoran" )&&password.equals("12345678" )) {
ActionContext.getContext().getSession().put("user" , username);
return "loginOK" ;
}else {
this .addFieldError("err" ,"用户名或密码不正确!" );
return "loginFail" ;
}
}
public void validateLoginMethod () {
if (username==null ||username.trim().equals("" )) {
this .addFieldError("username" ,"用户名不能为空!" );
}else {
if (!Pattern.matches("[a-zA-Z]{6,15}" , username.trim())) {
this .addFieldError("username" , "用户名格式错误!" );
}
}
if (password==null ||password.trim().equals("" )) {
this .addFieldError("password" ,"密码不能为空!" );
}else {
if (!Pattern.matches("\\d{6,15}" , password.trim())) {
this .addFieldError("password" , "密码格式错误!" );
}
}
}
}
/20171108_chr_newdownInterceptor/src/nuc/sw/interceptor/LoginInterceptor.java
package nuc.sw.interceptor;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept (ActionInvocation arg0) throws Exception {
ActionContext ac=arg0.getInvocationContext();
String username=(String)ac.getSession().get("user" );
if (username!=null &&username.equals("chenghaoran" )) {
return arg0.invoke();
}else {
((ActionSupport)arg0.getAction()).addActionError("请先登录!" );
return Action.LOGIN;
}
}
}
/20171108_chr_newdownInterceptor/src/nuc/sw/interceptor/LogInterceptor.java
package nuc.sw.interceptor;
import java.util.Date;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LogInterceptor extends AbstractInterceptor {
private String name;
public void setName (String name) {
this .name = name;
}
@Override
public String intercept (ActionInvocation arg0) throws Exception {
String actionName=arg0.getAction().getClass().getName();
String methodName=arg0.getProxy().getMethod();
System.out.println(name+"准备要开始访问" +actionName+"的方法" +methodName+"!时间:" +new Date());
long start=System.currentTimeMillis();
long end=System.currentTimeMillis();
String result=arg0.invoke();
System.out.println(name+"访问完" +actionName+"的方法" +methodName+"!时间:" +new Date());
System.out.println("执行" +actionName+"的方法" +methodName+"共花费的时间是:" +(start-end)+"毫秒!" );
return result;
}
}
/20171108_chr_newdownInterceptor/src/struts.xml
<struts >
<package extends ="struts-default" namespace ="/" name ="download" >
<interceptors >
<interceptor name ="login" class ="nuc.sw.interceptor.LoginInterceptor" > </interceptor >
<interceptor name ="log" class ="nuc.sw.interceptor.LogInterceptor" >
<param name ="name" > 日志拦截器</param >
</interceptor >
</interceptors >
<action name ="download" class ="nuc.sw.action.DownloadAction" >
<interceptor-ref name ="login" > </interceptor-ref >
<interceptor-ref name ="defaultStack" > </interceptor-ref >
<interceptor-ref name ="log" >
<param name ="name" > 日志拦截器1</param >
<param name ="name" > 日志拦截器2</param >
</interceptor-ref >
<result name ="login" >
/login.jsp
</result >
<result type ="stream" >
<param name ="contentType" > ${contentType}</param >
<param name ="inputName" > targetFile</param >
<param name ="contentDisposition" > attachment;filename${downFileName}</param >
</result >
</action >
<action name ="loginAction" class ="nuc.sw.action.LoginAction" method ="loginMethod" >
<result name ="loginOK" >
/download.jsp
</result >
<result name ="loginFail" >
/login.jsp
</result >
<result name ="input" >
/login.jsp
</result >
</action >
</package >
</struts >
/20171108_chr_newdownInterceptor/WebContent/download.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html >
<head >
<meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
<title > 下载页</title >
</head >
<body >
<a href ="download?inputPath=f:/123.txt&contentType=text/plain&downFileName=123.txt" > struts2下载文件</a >
</body >
</html >
/20171108_chr_newdownInterceptor/WebContent/login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html >
<head >
<meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
<title > 登录页</title >
<s:head />
</head >
<body >
<s:actionerror />
<s:fielderror fieldName ="err" > </s:fielderror >
<s:form action ="loginAction" method ="post" >
<s:textfield label ="用户名" name ="username" > </s:textfield >
<s:password label ="密码" name ="password" > </s:password >
<s:submit value ="登陆" > </s:submit >
</s:form >
</body >
</html >