struts2_005


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<package name="test" namespace="/test" extends="struts-default">

<interceptors>
<interceptor name="myInterceptor" class="interceptor.MyInterceptor"></interceptor>
</interceptors>

<action name="tokenAction" class="action.TokenAction">
<interceptor-ref name="token"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>

<result name="input" type="redirect">/index.jsp</result>
<result name="success">/success.jsp</result>
<result name="invalid.token">/error.jsp</result>
</action>

<action name="singleUploadAction" class="action.SingleUploadAction">
<interceptor-ref name="fileUpload"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>

<result name="uploadSuccess">/uploadSuccess.jsp</result>
</action>

<action name="multiUploadAction" class="action.MultiUploadAction">
<interceptor-ref name="fileUploadStack"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>

<result name="uploadSuccess">/uploadSuccess.jsp</result>
</action>

<action name="interceptorAction" class="action.InterceptorAction">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>

<result name="success">/success.jsp</result>
</action>
</package>
</struts>




package action;

import com.opensymphony.xwork2.ActionSupport;

public class InterceptorAction extends ActionSupport {

public String test(){
System.out.println("action in");
return SUCCESS;
}
}




package action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class MultiUploadAction extends ActionSupport implements
ServletContextAware {

private File[] su;
private String[] suContentType;
private String[] suFileName;

private ServletContext context;
private String separator = File.separator;
private String directory = "file";

public String upload() throws Exception {

String realPath = context.getRealPath("");
File uploadDirectory = new File(realPath, directory);// 上传文件保存文件夹
if (!uploadDirectory.exists()) {
uploadDirectory.mkdir();
}

File uploadFile;
FileInputStream fis;
BufferedInputStream bis;
FileOutputStream fos;
BufferedOutputStream bos;

for (int i = 0; i < su.length; i++) {
uploadFile = new File(uploadDirectory, suFileName[i]);// 上传文件

fis = new FileInputStream(su[i]);// 读取源文件
bis = new BufferedInputStream(fis);// 缓冲流

fos = new FileOutputStream(uploadFile);// 上传文件
bos = new BufferedOutputStream(fos);// 缓冲流

int len = 0;
byte[] buff = new byte[1024 * 8];
while ((len = bis.read(buff)) != -1) {
bos.write(buff, 0, len);
bos.flush();
}

if (null != bos) {
bos.close();
}
if (null != fos) {
fos.close();
}
if (null != bis) {
bis.close();
}
if (null != fis) {
fis.close();
}

}

return "uploadSuccess";
}

public File[] getSu() {
return su;
}

public void setSu(File[] su) {
this.su = su;
}

public String[] getSuContentType() {
return suContentType;
}

public void setSuContentType(String[] suContentType) {
this.suContentType = suContentType;
}

public String[] getSuFileName() {
return suFileName;
}

public void setSuFileName(String[] suFileName) {
this.suFileName = suFileName;
}

public void setServletContext(ServletContext context) {
this.context = context;
}

}





package action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class SingleUploadAction extends ActionSupport implements
ServletContextAware {
private File su;
private String suContentType;
private String suFileName;

private ServletContext context;

private String separator = File.separator;

public String upload() throws Exception {

// 上传路径文件夹地址
String path = context.getRealPath("");
File uploadAddress = new File(path, "file");
if (!uploadAddress.exists()) {
uploadAddress.mkdir();
}

File uploadFile = new File(uploadAddress, suFileName);// 上传文件

FileInputStream fis = new FileInputStream(su);// 读取原始文件
BufferedInputStream bis = new BufferedInputStream(fis);// 缓冲流

FileOutputStream fos = new FileOutputStream(uploadFile);// 上传文件流
BufferedOutputStream bos = new BufferedOutputStream(fos);// 缓冲流

int len = 0;
byte[] buff = new byte[1024 * 8];

while ((len = bis.read(buff)) != -1) {
bos.write(buff, 0, len);
bos.flush();
}

bos.close();
fos.close();

bis.close();
fis.close();

return "uploadSuccess";

}

public File getSu() {
return su;
}

public void setSu(File su) {
this.su = su;
}

public String getSuContentType() {
return suContentType;
}

public void setSuContentType(String suContentType) {
this.suContentType = suContentType;
}

public String getSuFileName() {
return suFileName;
}

public void setSuFileName(String suFileName) {
this.suFileName = suFileName;
}

public void setServletContext(ServletContext context) {
this.context = context;
}

}




package action;

import com.opensymphony.xwork2.ActionSupport;

public class TokenAction extends ActionSupport {

private String username;
private String password;

public String init(){
return INPUT;
}

public String register() {

System.out.println("username : " + username);
System.out.println("password : " + password);

return SUCCESS;
}

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;
}

}




package interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements Interceptor {

public void destroy() {

}

public void init() {

}

public String intercept(ActionInvocation invocation) throws Exception {

System.out.println("myInterceptor in ");

String result = invocation.invoke();
System.out.println(result);
System.out.println("myInterceptor out ");

return result;
}

}




package test;

public class Action {
public String test(){
System.out.println("action");
return "input";
}
}




package test;

import java.util.ArrayList;
import java.util.List;

public class ActionInvocation {

private List<Interceptor> interceptors = new ArrayList<Interceptor>();
private Action action = new Action();

public ActionInvocation(){
FirstInterceptor fi = new FirstInterceptor();
SecondInterceptor si = new SecondInterceptor();
interceptors.add(fi);
interceptors.add(si);
}

int index = -1;
String result;
public String invoke(){
index++;
if(index < interceptors.size()){
result = interceptors.get(index).interceptor(this);
}else{
result = action.test();
}
return result;
}
}




package test;

public class FirstInterceptor implements Interceptor {

public void destroy() {

}

public void init() {

}

public String interceptor(ActionInvocation invocation) {

System.out.println("1");

String result = invocation.invoke();
System.out.println(result);
System.out.println("-1");
return result;
}

}





package test;

public class InteceptorTest {

/**
* @param args
*/
public static void main(String[] args) {
ActionInvocation invocation = new ActionInvocation();
invocation.invoke();
}

}





package test;

public interface Interceptor {
public void destroy();
public void init();
public String interceptor(ActionInvocation invocation);
}




package test;

public class SecondInterceptor implements Interceptor {

public void destroy() {

}

public void init() {

}

public String interceptor(ActionInvocation invocation) {
System.out.println("2");
String result = invocation.invoke();
System.out.println(result);
System.out.println("-2");
return result;
}

}





<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'error.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

<style type="text/css">
#num{
font-size:14px;
color:red;
font-weight:bold;
}
</style>
<script type="text/javascript" src="javascript/common.jsp"></script>
<script type="text/javascript">


function daojishi(){
document.getElementById("num").innerText = parseInt(document.getElementById("num").innerText) - 1;
if(parseInt(document.getElementById("num").innerText) == 0){
to("/index.jsp");
}
setTimeout(daojishi,1000);
}

window.onload = daojishi;
</script>

</head>

<body>
系统正在处理中...请勿重复提交 !    <span id="num" >5</span>秒后返回注册页面!
<a href="javascript:to('/index.jsp')">手动跳转</a>
</body>


</html>





<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<s:form action="test/tokenAction!register" theme="simple">
username: <s:textfield name="username"></s:textfield><br>
password: <s:password name="password"></s:password><br>;
<s:token></s:token>
<s:submit value="submit"></s:submit><br>
</s:form>
</body>
</html>




<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'success.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
success
</body>
</html>




<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'upload.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<s:form action="test/singleUploadAction!upload" method="post" enctype="multipart/form-data">
<s:file name="su"></s:file>
<s:submit value="上传"></s:submit>
</s:form>

<s:form action="test/multiUploadAction!upload" method="post" enctype="multipart/form-data">
<s:file name="su"></s:file>
<s:file name="su"></s:file>
<s:file name="su"></s:file>
<s:file name="su"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</body>
</html>




<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'uploadSuccess.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<font color="red"><s:property value="suFileName"/></font>上传成功!


</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值