一、导入jar包,配置web.xml中的<filter>(基于过滤器)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
二、创建struts.xml配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<!-- <constant name="struts.action.excludePattern" value="/*.file"/>-->
<package name="struts2" extends="struts-default">
<interceptors>
<interceptor name="myInterceptor" class="com.wonders.struts2.interceptor.MyInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="myInterceptor"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
<action name="loginAction" class="com.wonders.struts2.action.LoginAction">
<!-- <interceptor-ref name="myInterceptor"/>-->
<!-- 重定向 到页面 -->
<!-- <result name="success" type="redirect">-->
<!-- <param name="location">/redirect.jsp</param>-->
<!-- <param name="param1">参数1</param>-->
<!-- <param name="param2">参数2</param>-->
<!-- </result>-->
<result name="success" type="redirectAction">
<param name="actionName">redirect</param>
<param name="namespace">/</param>
<param name="param1">重定向参数1</param>
</result>
</action>
<action name="redirect" class="com.wonders.struts2.action.RedirectAction">
<result>/redirect.jsp</result>
</action>
</package>
</struts>
至此登录页面完成
=======================================================================
三、详细配置其中的Interceptor
* 注意拦截器是单实例的,注意线程安全
package com.wonders.struts2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyInterceptor implements Interceptor {
private int count;
public void destroy() {
System.out.println("MyInterceptor has destroy..." + System.currentTimeMillis());
}
public void init() {
System.out.println("MyInterceptor init..." + System.currentTimeMillis());
}
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("MyInterceptor intercept .... " + count++);
return invocation.invoke();
}
}
* 在action 中使用
<default-interceptor-ref name="myStack"/>
时,注意继承一些默认的必要拦截器,否则只有request.parameter带过来(只试了${requestScope.uername},${param.username}猜测应该是这样)
四、resultType设置
1)重定向(重定向页面和Action)
<action name="loginAction" class="com.wonders.struts2.action.LoginAction">
<!-- <interceptor-ref name="myInterceptor"/>-->
<!-- 重定向 到页面 -->
<!-- <result name="success" type="redirect">-->
<!-- <param name="location">/redirect.jsp</param>-->
<!-- <param name="param1">参数1</param>-->
<!-- <param name="param2">参数2</param>-->
<!-- </result>-->
<!-- 重定向到 Action -->
<result name="success" type="redirectAction">
<param name="actionName">redirect</param>
<param name="namespace">/</param>
<param name="param1">重定向参数1</param>
</result>
</action>
<action name="redirect" class="com.wonders.struts2.action.RedirectAction">
<result>/redirect.jsp</result>
</action>
2)chain
<!-- chain -->
<result name="success" type="chain">
<param name="actionName">redirect</param>
<param name="namespace">/</param>
<!-- 此处带参数无效 -->
<!-- <param name="param1">chain参数</param>-->
</result>
3)stream
-
contentType - the stream mime-type as sent to the web browser (default =
text/plain
).
-
contentLength - the stream length in bytes (the browser displays a progress bar).
-
contentDisposition - the content disposition header value for specifing the file name (default =
inline
, values are typically attachment;filename="document.pdf".
-
inputName - the name of the InputStream property from the chained action (default =
inputStream
).
-
bufferSize - the size of the buffer to copy from input to output (default =
1024
).
-
allowCaching if set to 'false' it will set the headers 'Pragma' and 'Cache-Control' to 'no-cahce', and prevent client from caching the content. (default =
true
)
-
contentCharSet if set to a string, ';charset=value' will be added to the content-type header, where value is the string set. If set to an expression, the result of evaluating the expression will be used. If not set, then no charset will be set on the header
<action name="loaddownAction" class="com.wonders.struts2.action.LoaddownAction">
<result name="success" type="stream">
<param name="contentType">image/*</param>
<param name="inputName">is</param>
<param name="contentDisposition">attachment;filename="1.png"</param>
<param name="bufferSize">1024</param>
</result>
</action>
分享到:
相关推荐
Struts2学习笔记,介绍了struts2的基础部分
struts2学习笔记,非本人所写,但有学习的价值,总结的很好,分享一个!
struts2学习笔记总结
struts2学习笔记struts2学习笔记struts2学习笔记
struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换
本人学习struts2的笔记,希望大家可以多多学习以后共同交流
1. struts2框架的引入 1)把struts2的相关jar包导入到项目中去 2)把struts2框架的配置文件struts.xml复制粘贴到项目中的src下面(同时也可以把log4j.properties放到src下) 在这里我们主要是要的这个struts.xml文件...
structs2很详细的学习笔记,structs2的建造,工作原理,例子,逐步讲解,纯文字的