`

struts2 convention插件的使用(一)

阅读更多

 

 

 

struts2 convention插件的相关文章在网络上还很少,今天研究了一下

官方文档 http://cwiki.apache.org/WW/convention-plugin.html

 

 

 

附件内源码环境(下载请到页尾)

Dynamic Web Project

eclipse3.3

jdk1.6

tomcat6

 

必要jar列表

 

commons-fileupload-1.2.1.jar

commons-logging-1.0.4.jar

freemarker-2.3.13.jar

ognl-2.6.11.jar

struts2-convention-plugin-2.1.6.jar

struts2-core-2.1.6.jar

xwork-2.1.2.jar

  •  
    • com.sunflower.actions.NavigatorAction
    • com.sunflower.actions.child.ChildAction
    • error.jsp
    • index.jsp
    • next.jsp
    • child/next.jsp
    • 主题:确认action内默认执行方法是execute还是index
      请求url:navigator.action  
      类型:dispatcher 
      方法:execute或index 
      执行默认方法
    • 主题:测试如何跳转到站外
      请求url:navigator!redirect.action   
      类型:redirect(旧版本使用ServletRedirectResult.class) 
      方法:redirect 
      跳转到站外
    • 主题:测试redirectAction、basePackage内子包action的映射、以及参数传递
      请求url:navigator!redirectAction.action   
      类型:redirectAction (旧版本使用ServeltActionRedirectResult.class) 
      方法:redirectAction 
      location:child/child(basePackage=com.sunflower.actions,this package=com.sunflower.actions.child) 
      注意
      1:ChildAction内的execute方法返回指向location="next.jsp",查看结果页面next.jsp是根目录资源还是/child/next.jsp资源 
      2: redirectAction方法向请求作用域设置了msg信息,查看是否显示了msg 
      跳转到站内action
    • 主题:错误跳转
      请求url:navigator!error.action   
      生成错误
    • 主题:@Action的使用已经Result的location绝对与相对的区别
      使用注解@Action(value="/test/childTest") 
      请求url:/test/childTest.action   
      注意:类仍然是NavigatorAction 
      next.jsp为绝对路径
    • 主题:@Actions的使用
      注解@Actions({ @Action(value="/test/action1"), @Action(value="/test/action2") })
      请求
      url:/test/action1.action 
      url:/test/action2.action 
      注意:类仍然是NavigatorAction 
      action1 action2
  •  

    Web.xml关键部分

     

     

<!-- ============================ 使用struts2处理页面请求 ============================ -->
	<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>*.action</url-pattern>
	</filter-mapping>

 

 

struts2.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.i18n.reload" value="true" />
	<!-- 配置文件重新加载 -->
	<constant name="struts.configuration.xml.reload" value="true" />
	<!-- convention类从新加载 -->
	<constant name="struts.convention.classes.reload" value="true" />
	<!--++++++++++++++++++++++++++++++++++++++++++++++++开发模式结束 -->
	<!-- 主题 -->
	<constant name="struts.ui.theme" value="simple" />
	<!-- 地区 -->
	<constant name="struts.locale" value="zh_CN" />
	<!-- 国际化编码 -->
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<!--  扩展-->
	<constant name="struts.action.extension" value="action,do,jsp" />
	<!-- 启用动态方法调用 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<!-- 设置Struts 2是否允许在Action名中使用斜线 -->
	<constant name="struts.enable.SlashesInActionNames" value="false" />
	<!-- 结果资源所在路径 -->
	<constant name="struts.convention.result.path" value="/"/> 
	<!-- action后缀 -->
	<constant name="struts.convention.action.suffix" value="Action"/> 
	<!-- 名称首字母小写 -->
    <constant name="struts.convention.action.name.lowercase" value="true"/> 
    <!-- 分隔符 一个action名字的获取。比如为HelloWorldAction。按照配置,actionName为hello_world。 -->
    <constant name="struts.convention.action.name.separator" value="_"/> 
    <!-- 禁用扫描 -->
    <constant name="struts.convention.action.disableScanning" value="false"/> 
    <!-- 默认包 -->
    <constant name="struts.convention.default.parent.package" value="default"/> 
    <!--确定搜索包的路径。只要是结尾为action的包都要搜索。basePackage按照默认不用配置,如果配置,只会找以此配置开头的包。locators及locators.basePackage都是一组以逗号分割的字符串。 -->
    <constant name="struts.convention.package.locators" value="actions"/> 
    <!-- 禁用包搜索 -->
    <constant name="struts.convention.package.locators.disable" value="false"/> 
    <!-- 基于什么包 -->
    <constant name="struts.convention.package.locators.basePackage" value="com.sunflower.actions"/>
    <!--  排除的包 -->
    <constant name="struts.convention.exclude.packages" value="org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.*"/>
    <!-- 包含的包 -->
    <!-- 包括的jar,一般用于大型项目,其action一般打包成jar -->
    <constant name="struts.convention.action.includeJars" value="" />
    <!-- 结果类型 -->
    <constant name="struts.convention.relative.result.types" value="dispatcher,freemarker"/> 
    <!-- 
     如果此值设为true,如果一个action的命名空间为/login,名称为HelloWorldAction。result返回值是success,默认会找到/WEB-INF/pages/login/hello_world.jsp(如果有hello_world_success.jsp就找这个文件,连接符“_”是在<constant name="struts.convention.action.name.separator" value="_"/>中配置的)。如果有一个action的result返回值是“error”,就会找/WEB-INF/pages /login/hello_world_error.jsp。 

      如果此值设为false,如果一个action的命名空间为/login,名称为HelloWorldAction。result返回值是success,默认会找到/WEB- INF/pages/login/hello_world/index.jsp(如果有success.jsp就找这个文件)。如果有一个action的result返回值是“error”,就会找/WEB-INF/pages /login/hello_world/error.jsp。 
     -->
    <constant name="struts.convention.result.flatLayout" value="true"/>
    <constant name="struts.convention.action.mapAllMatches" value="false"/> 
    <!-- 检查是否实现action -->
    <constant name="struts.convention.action.checkImplementsAction" value="true"/>
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
	<constant name="struts.convention.redirect.to.slash" value="true"/>
	<package name="default" extends="struts-default">
		<interceptors>
			<interceptor-stack name="defaultStack">
				<interceptor-ref name="exception" />
				<interceptor-ref name="servletConfig" />
				<interceptor-ref name="actionMappingParams" />
				<interceptor-ref name="staticParams" />
				<interceptor-ref name="params" />
			</interceptor-stack>
		</interceptors>
	</package>
</struts>

 

 

NavigatorAction源码

 

package com.sunflower.actions;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionContext;

@Results( {
		@Result(name = "next", location = "/next.jsp", type = "dispatcher"),
		@Result(name = "error", location = "error.jsp", type = "dispatcher"),
		@Result(name = "redirectAction", location = "./child/child", type = "redirectAction"),
		@Result(name = "redirect", location = "http://sunflowers.iteye.com", type = "redirect") })
public class NavigatorAction {
	private String actionName;

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public String index() {
		outputMsg("method:index");
		return "next";
	}

	public String execute() {
		outputMsg("method:execute,no index method");
		return "next";
	}

	public String error() {
		try {
			throw new Exception();
		} catch (Exception e) {
			outputMsg(e);
			return "error";
		}
	}

	public String redirect() {
		System.out.println("重定向:rediret");
		return "redirect";
	}

	public String redirectAction() {
		outputMsg("navigatorAction 跳转而来,原地址是navigator!redirectAction.action,请查看地址栏");
		return "redirectAction";
	}

	@Action(value = "/test/childTest")
	public String action() {
		outputMsg("@action ---method:action");
		return "next";
	}

	@Actions( { @Action(value = "/test/action1"),
			@Action(value = "/test/action2") })
	public String actions() {
		outputMsg("@actions ---method:actions,action=" + actionName);
		return "next";
	}
	private void outputMsg(Object msg) {
		System.out.println(msg);
		ActionContext.getContext().put("msg", msg);
	}
}

 

 

 ChildAction源码

 

package com.sunflower.actions.child;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

@Results( {
		@Result(name = "next", location = "next.jsp", type = "dispatcher")})
public class ChildAction {
	public String execute() {
		System.out.println("childAction 默认方法执行");
		return "next";
	}
}

 

     index.jsp源码
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <!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=UTF-8">
    <title>struts2 convention测试</title>
    <style>
    * {
    	line-height: 2em;
    }
    
    a:visited,a:hover,a:active,a:link {
    	color: #00f;
    }
    </style>
    </head>
    <body>
    后台Action&nbsp;
    <ul class="horizontal">
    	<li>com.sunflower.actions.NavigatorAction</li>
    	<li>com.sunflower.actions.child.ChildAction</li>
    </ul>
    页面资源&nbsp;
    <ul class="horizontal">
    	<li>error.jsp</li>
    	<li>index.jsp</li>
    	<li>next.jsp</li>
    	<li>child/next.jsp</li>
    </ul>
    <ul>
    	<li>主题:确认action内默认执行方法是execute还是index<br />
    	请求url:navigator.action&nbsp;&nbsp;<br />
    	类型:dispatcher <br />
    	方法:execute或index <br />
    	<a href="./navigator.action">执行默认方法</a></li>
    	<li>主题:测试如何跳转到站外<br />
    	请求url:navigator!redirect.action&nbsp;&nbsp; <br />
    	类型:redirect(旧版本使用ServletRedirectResult.class) <br />
    	方法:redirect <br />
    	<a href="./navigator!redirect.action">跳转到站外</a></li>
    	<li>主题:测试redirectAction、basePackage内子包action的映射、以及参数传递<br />
    	请求url:navigator!redirectAction.action&nbsp;&nbsp; <br />
    	类型:redirectAction (旧版本使用ServeltActionRedirectResult.class) <br />
    	方法:redirectAction <br />
    	location:child/child(basePackage=com.sunflower.actions,this&nbsp;package=com.sunflower.actions.child)
    	<br />
    	<font color="red">注意</font><br />
    	1:ChildAction内的execute方法返回指向location="next.jsp",查看结果页面next.jsp是根目录资源还是/child/next.jsp资源
    	<br />
    	2: redirectAction方法向请求作用域设置了msg信息,查看是否显示了msg <br />
    	<a href="./navigator!redirectAction.action">跳转到站内action</a></li>
    	<li>主题:错误跳转<br />
    	请求url:navigator!error.action&nbsp;&nbsp; <br />
    	<a href="./navigator!error.action">生成错误</a></li>
    	<li>主题:@Action的使用已经Result的location绝对与相对的区别<br />
    	使用注解@Action(value="/test/childTest") <br />
    	请求url:/test/childTest.action&nbsp;&nbsp; <br />
    	<font color="red">注意</font>:类仍然是NavigatorAction <br />
    	<a href="./test/childTest.action">next.jsp为绝对路径</a></li>
    	<li>主题:@Actions的使用<br />
    	注解@Actions({ @Action(value="/test/actions1"),
    	@Action(value="/test/actions2") })<br />
    	请求<br />
    	url:/test/action1.action <br />
    	url:/test/action2.action <br />
    	<font color="red">注意</font>:类仍然是NavigatorAction <br />
    	<a href="./test/action1.action?actionName=action1">action1</a>&nbsp;<a
    		href="./test/action2.action?actionName=action2">action2</a></li>
    </ul>
    </body>
    </html>

     

     

     next.jsp源码

     

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title><style>
*{
line-height:3em;
}
</style>
</head>
<body>
<h1>struts2 &nbsp;&nbsp;&nbsp;convention插件使用测试</h1>
测试信息
<br />
${msg}
<br />
<a href="#" onclick="window.history.go(-1);return false;">返回</a>
</body>
</html>

 

 

 child/next.jsp源码

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title><style>
*{
line-height:3em;
}
</style>
</head>
<body>
child/next.jsp:{msg}
</body>
</html>

 

 

 

 

 

 

     

 

 

 

 

 

 

 

 

15
3
分享到:
评论
5 楼 Pauvrepetiti 2012-06-13  
写的很好
4 楼 sunxden 2011-12-25  
呵呵,那个Pages不一致不是要点,请不要把这个当成问题所在了!
3 楼 sunxden 2011-12-25  
你好,如果 结果资源所在路径设置为<constant name="struts.convention.result.path" value="/WEB-INF/pages"/>,那么在NavigatorAction中@Results({@Result(name = "redirectAction", location = "./child/child", type = "redirectAction")})执行站内跳转到ChildAction后,再执行默认的execute方法时,根据其注解@Results( {@Result(name = "next", location = "next.jsp", type = "dispatcher")}),我个人认为此处的next.jsp页面的路径应该是/WEB-INF/pages/child/next.jsp(已经新建该路径及其next.jsp页面),但是运行这个Project后,它却提示404,找不到该请求页面

type Status report

message /struts/WEB-INF/page/child/next.jsp

description The requested resource (/struts/WEB-INF/page/child/next.jsp) is not available.
这是怎么回事?希望你能给个解答!

2 楼 bolo 2010-04-02  
补充一下,默认根包就是像你这样的 package com.sunflower.actions;
1 楼 bolo 2010-04-02  
我有问题想请教下,如果我在一个Action中,想从当前执行的方法,重定向到该Action中的另一个方法,请问这个result该怎么写,另外这个Action是在默认根包下

相关推荐

    Struts2插件convention

    在使用Struts2 Convention插件时,以下是一些关键知识点: 1. **类命名规范**:类名通常应该以`Action`结尾,比如`UserAction`。这样,Struts2会自动将这些类视为处理请求的Action类。 2. **方法命名**:方法名将...

    STRUTS2 Convention零配置

    Struts2框架自2.1版本开始引入了一种新的零配置方式——Convention插件。与之前的Codebehind插件不同,Convention插件更加彻底地摆脱了对配置文件的需求,包括struts.xml以及Annotation等传统配置方式,通过约定而非...

    struts2-Convention插件使用

    ### Struts2-Convention插件使用详解 #### 引言 自Struts2.1版本起,Convention插件被引入,旨在替代原有的Codebehind插件,实现框架内的零配置理念。这一插件的设计思想围绕着减少XML配置的依赖,通过代码结构与...

    struts2的Convention插件说明书(中文版)

    Struts2的Convention插件是一种自动化配置工具,从2.1版本开始引入,旨在减少XML配置,实现Struts2应用的零配置或者最少配置。该插件通过一系列预定义的命名规则和约定,自动解析和映射Action、结果页面、拦截器等...

    struts2的convention配置详解 很全

    Convention插件是Struts2提供的一种自动化配置方式,它旨在减少XML配置文件的数量,提高开发效率。这篇详尽的指南将深入讲解Struts2的Convention配置,让你更好地理解和应用这一特性。 首先,了解什么是Conventions...

    Struts2 convention

    Struts2 Convention插件是基于Apache Struts2框架的一个组件,它引入了一种约定优于配置(Convention over Configuration)的理念,简化了MVC应用的开发。这个插件在默认情况下可以根据类和方法的名字来自动映射URL...

    struts2的convention插件使用.docx

    Struts2的Convention插件是其框架中的一个重要组成部分,旨在简化配置,提供一种自动映射Action类到URL路径的方法,使得开发更加高效。以下是关于Conventio

    Struts2 Convention Plugin中文文档 Annotion

    Struts2 Convention Plugin 是从 Struts2.1 版本开始引入的一个插件,它的主要目标是实现 Struts2 框架的零配置。通过约定优于配置的原则,开发者可以更加专注于业务逻辑,减少大量的 XML 配置工作。以下是 ...

    struts2的插件使用

    这篇博文主要探讨的是Struts2中的"convention-plugin",这是一个自动配置插件,极大地简化了开发过程。 在传统的Struts2应用中,开发者需要手动编写XML配置文件来定义Action类和结果页面的映射。然而,随着...

    如何使用struts2的零配置插件convention

    要使用`Convetion`插件,首先需要将`struts-convention-plugin-x.x.x.jar`文件添加到项目的`WEB-INF/lib`目录下。然后,插件会自动扫描指定的包(默认是`action`, `actions`, `struts`, `struts2`)内的所有Java类,...

    struts2 convention plugin(插件) 测试项目

    Struts2 Convention Plugin是Apache Struts框架的一个重要组成部分,它为开发者提供了一种更为便捷的配置方式,使得在Struts2应用中实现MVC模式变得更加简单。这个测试项目旨在帮助我们理解和掌握如何在实际开发中...

    Struts2-rest插件(有注释)

    从 Struts 2.1 开始,Struts 2 改为使用 Convention 插件来支持零配置。Convention 插件彻底地抛弃了配置信息,不仅不需要使用 struts.xml 文件进行配置,甚至不需要使用 Annotation 进行配置。而是由 Struts 2 根据...

    struts2-convention-plugin-2.3.32.jar

    在给定的文件"struts2-convention-plugin-2.3.32.jar"中,我们关注的是Struts2的Convention插件,版本号为2.3.32。这个插件是Struts2框架的一个重要组成部分,旨在提供更加灵活和自动化的配置方式。 Convention...

    简述STRUTS2_Convention零配置

    3. `struts.convention.action.packages`: 它指定一个或多个额外的包,让Convention插件去扫描其中的Action类。 在Action名称的映射上,Convention遵循一定的规则。如果类名包含"Action"后缀,它会被去掉,然后将...

    struts2采用convention-plugin实现零配置

    Struts2是一个流行的Java Web应用程序...Convention Plugin是Struts2的一个插件,它基于“约定优于配置”的原则,自动地为Action类和结果页面建立映射,从而极大地减少了手动配置的需求。通过自动探测和推理,Conventio

    struts2-convention-plugin-2.1.6.jar

    在Struts2的生态系统中,`struts2-convention-plugin`是一个重要的组件,其版本号为2.1.6,正如标题所示。这个插件引入了一种更加便捷的方式来配置应用,尤其是对于那些不喜欢或者不习惯手动编写繁琐的`struts.xml`...

    Struts2_Convention_Plugin中文文档

    Struts2 Convention Plugin是Apache Struts框架的一个重要插件,主要目标是简化MVC(Model-View-Controller)架构中的配置工作。这个插件引入了一种约定优于配置(Convention over Configuration)的理念,允许...

    struts _convention _plugin application

    在Struts2框架中,Convention插件是一个强大的自动化工具,旨在简化配置过程,实现零或最少配置的开发模式。这篇博文将深入探讨Struts2 Convention插件的应用及其核心概念。 首先,了解Struts2 Convention插件的...

Global site tag (gtag.js) - Google Analytics