Struts2的文件上传和下载
接上节的拦截器未完成的部分。
功能:简单介绍监听器。用struts2的拦截器做一个关于权限校验的简单例子。
一、在src下创建一个包com.test.listener,然后在此包下创建一个监听器类MyListener.java继承自接口PreResultListener:
package com.test.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyInterceptor3 extends MethodFilterInterceptor {
public void init(){
System.out.println("init3");
}
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("my interceptor3");
String result = invocation.invoke();
return result;
}
}
二、将这个监听器注册到MyIntercepoter3拦截器里面去:
package com.test.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.test.listener.MyListener;
public class MyInterceptor3 extends MethodFilterInterceptor {
public void init(){
System.out.println("init3");
}
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
invocation.addPreResultListener(new MyListener());
System.out.println("my interceptor3");
String result = invocation.invoke();
System.out.println("after my interceptor3 finished");
return result;
}
}
说明:监听器的执行时机是在Action的execute方法(或者自己定义的方法如test()方法)执行之后,但是在返回结果之前执行。
功能:使用拦截器进行一个权限校验,用户输入用户名和密码之后跳转到register.jsp页面,不允许用户直接通过register2.jsp进行提交,如果通过register2.jsp提交的话会直接的跳转到login2.jsp页面,模拟一下。
三、在之前做的login2.jsp页面为例子,在struts.xml中修改输入成功之后跳转到register2.jsp:
<action name="login" class="com.test.action.LoginAction">
<result name="input">/login2.jsp</result>
<result name="success">/register2.jsp</result>
<result name="failer">login2.jsp</result>
</action>
四、在com.test.interceptor中创建一个权限验证的拦截器AuthInterceptor.java继承自AbstractInterceptor类:
package com.test.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthInterceptor extends AbstractInterceptor {
@Override
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation invocation) throws Exception {
Map map = invocation.getInvocationContext().getSession();
if(map.get("user") == null){
return Action.LOGIN;
}
else
{
return invocation.invoke();
}
}
}
五、修改LoginAction.java类,把用户信息存放到Session中:
package com.test.action;
import java.util.Map;
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;
}
@SuppressWarnings("unchecked")
public String execute() throws Exception
{
if("zly".equals(this.getUsername().trim()) && "zly".equals(this.getPassword().trim()))
{
Map map = ActionContext.getContext().getSession();
map.put("user", "valid");
return "success";
}
else
{
this.addFieldError("username", "username or password error");
return "failer";
}
}
@Override
public void validate()
{
if(null == this.getUsername() || "".equals(this.getUsername().trim()))
{
this.addFieldError("username", "username required");
}
if(null == this.getPassword() || "".equals(this.getPassword().trim()))
{
this.addFieldError("password", "password required");
}
}
}
六、在struts.xml中注册拦截器,并且添加一个全局的转向global-results,设置type属性类型为重定向redirect:
<package name="struts2" extends="struts-default">
<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
</interceptor>
<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">
</interceptor>
<interceptor name="auth" class="com.test.interceptor.AuthInterceptor">
</interceptor>
<interceptor-stack name="myStack">
<!--
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
-->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<global-results>
<result name="login" type="redirect">/login2.jsp</result>
</global-results>
<action name="login" class="com.test.action.LoginAction">
<result name="input">/login2.jsp</result>
<result name="success">/register2.jsp</result>
<result name="failer">login2.jsp</result>
</action>
<action name="pointConverter" class="com.test.action.PointAction">
<result name="success">/output.jsp</result>
</action>
<action name="register" class="com.test.action.RegisterAction" method="test">
<result name="success">/success.jsp</result>
<result name="input">/register2.jsp</result>
<interceptor-ref name="auth"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<!--
<interceptor-ref name="myInterceptor3">
<param name="excludeMethods">test,execute</param>
<param name="includeMethods">test</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
-->
</action>
</package>
关于拦截器的部分完成。
功能:struts2的上传和下载。
七、在WebRoot下创建一个文件夹upload,并且创建一个upload.jsp:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="result.jsp" method="post" enctype="application/x-www-form-urlencoded">
Information:<input type="text" name="info"><br>
File:<input type="file" name="file"><br>
<input type="submit" name="submit" value=" submit ">
</form>
</body>
</html>
八、接着在创建一个结果页面result.jsp:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="java.io.*" %>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<%
InputStream is = request.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String buffer = null;
while((buffer = br.readLine()) != null){
out.print(buffer + "<br>");
}
%>
</body>
</html>
说明:form中使用的是enctype="application/x-www-form-urlencoded",这样是会出现编码错误的问题,所以要改成enctype="multipart/form-data",还会出现下面的错误:
修改的方法是不加载关于struts2的内容,所以要先注销了web.xml中的下面代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
<!--表示客服端发送过来的所有请求都必须由FilterDispatcher过滤器来过滤-->
</web-app>
说明:这样会把上传文件中的信息都显示出来,未完待续。