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="#" οnclick="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>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值