`
zdb_cn
  • 浏览: 123531 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Struts2 学习笔记

 
阅读更多

一、导入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框架概览 #### MVC模式的理解与演进 Struts2是基于MVC(Model-View-Controller)模式设计的一种Java Web开发框架。在MVC...

    struts2学习笔记.doc

    ### Struts2学习笔记知识点概览 #### 一、环境搭建 **1.1 Struts2简介** - **Struts2概述**:Struts2是一个开源的MVC框架,它结合了Struts 1.x、WebWork和其他一些框架的优点。Struts2的主要目标是简化Web应用程序...

    struts2学习笔记(1)

    ### Struts2学习笔记知识点详解 #### 一、Struts2框架的基本引入步骤 ##### 1. 导入Struts2相关Jar包 在引入Struts2框架时,首先需要将Struts2的相关Jar包导入到项目的类路径中。这些Jar包通常包括核心库以及其他...

    struts2学习笔记

    以上就是Struts2学习笔记中涵盖的主要知识点,这些内容涵盖了从基础到进阶的应用,对于理解和掌握Struts2框架至关重要。尽管对于初学者来说可能有些复杂,但随着深入学习,你会发现Struts2的强大和实用性。

    struts2学习笔记(完美总结)——转自OPEN经验库

    Struts2是一个强大的Java web应用程序开发框架,它遵循Model-View-Controller (MVC)设计模式,用于构建可维护性和可扩展性高的企业级应用。本文将深入探讨Struts2的核心概念,包括Action、Result、配置文件、OGNL与...

    struts 2 学习笔记

    在Struts2中,学习笔记通常会涵盖以下几个关键概念: 1. **源代码查看和Javadoc**:开发者可以通过查看源代码来理解Struts2的工作原理,而Javadoc则提供了API文档,帮助理解类和方法的功能。 2. **包(Package)和...

    张龙圣思园struts2学习笔记word

    张龙圣思园的Struts2学习笔记,无疑为Java开发者提供了一份宝贵的参考资料,它可能涵盖了Struts2的基础概念、核心组件、配置方式以及实战技巧。 首先,让我们深入了解Struts2的核心特性。Struts2是MVC(Model-View-...

    struts2学习笔记总结

    本笔记将全面总结Struts2的核心概念、主要功能以及实际开发中的应用。 一、Struts2概述 Struts2是Apache软件基金会下的一个开源项目,它继承了Struts1的优点并解决了其存在的问题,如性能和灵活性。Struts2的核心是...

Global site tag (gtag.js) - Google Analytics