struts2学习笔记十二(第12讲.Struts2的文件上传和下载)
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");}@Overrideprotected String doIntercept(ActionInvocation invocation) throws Exception {System.out.println("my interceptor3");String result = invocation.invoke();return result;}}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");}@Overrideprotected 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 name="login" name="code">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();}}}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";}}@Overridepublic 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");}}}<package name="struts2" extends="struts-default"><interceptors><interceptor name="myInterceptor" type="redirect">/login2.jsp</result></global-results><action name="login" 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>
<%@ 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>
<%@ 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>
<?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>