﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Martin's Home-文章分类-Struts2.x</title><link>http://www.blogjava.net/martinyuan/category/39730.html</link><description>希望我的积累对大家有所帮助！</description><language>zh-cn</language><lastBuildDate>Thu, 21 May 2009 08:25:42 GMT</lastBuildDate><pubDate>Thu, 21 May 2009 08:25:42 GMT</pubDate><ttl>60</ttl><item><title>struts2 convention插件的使用(一)</title><link>http://www.blogjava.net/martinyuan/articles/271945.html</link><dc:creator>Martin Yuan</dc:creator><author>Martin Yuan</author><pubDate>Thu, 21 May 2009 05:49:00 GMT</pubDate><guid>http://www.blogjava.net/martinyuan/articles/271945.html</guid><wfw:comment>http://www.blogjava.net/martinyuan/comments/271945.html</wfw:comment><comments>http://www.blogjava.net/martinyuan/articles/271945.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/martinyuan/comments/commentRss/271945.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/martinyuan/services/trackbacks/271945.html</trackback:ping><description><![CDATA[
		<p style="MARGIN: 0px">struts2 convention插件的相关文章在网络上还很少，今天研究了一下</p>
		<p style="MARGIN: 0px">官方文档 http://cwiki.apache.org/WW/convention-plugin.html</p>
		<p style="MARGIN: 0px"> </p>
		<p style="MARGIN: 0px">Dynamic Web Project</p>
		<p style="MARGIN: 0px">eclipse3.3</p>
		<p style="MARGIN: 0px">jdk1.6</p>
		<p style="MARGIN: 0px">tomcat6</p>
		<p style="MARGIN: 0px"> </p>
		<p style="MARGIN: 0px">必要jar列表</p>
		<p style="MARGIN: 0px"> </p>
		<p style="MARGIN: 0px">commons-fileupload-1.2.1.jar</p>
		<p style="MARGIN: 0px">commons-logging-1.0.4.jar</p>
		<p style="MARGIN: 0px">freemarker-2.3.13.jar</p>
		<p style="MARGIN: 0px">ognl-2.6.11.jar</p>
		<p style="MARGIN: 0px">struts2-convention-plugin-2.1.6.jar</p>
		<p style="MARGIN: 0px">struts2-core-2.1.6.jar</p>
		<p style="MARGIN: 0px">xwork-2.1.2.jar</p>
		<div>
				<ul>
						<li>  
<ul><li>com.sunflower.actions.NavigatorAction 
</li><li>com.sunflower.actions.child.ChildAction </li></ul><ul><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  <br />类型：dispatcher <br />方法：execute或index <br /><a href="http://localhost:8080/struts2/navigator.action">执行默认方法</a></li><li>主题：测试如何跳转到站外<br />请求url：navigator!redirect.action   <br />类型：redirect（旧版本使用ServletRedirectResult.class） <br />方法：redirect <br /><a href="http://localhost:8080/struts2/navigator!redirect.action">跳转到站外</a></li><li>主题：测试redirectAction、basePackage内子包action的映射、以及参数传递<br />请求url：navigator!redirectAction.action   <br />类型：redirectAction （旧版本使用ServeltActionRedirectResult.class） <br />方法：redirectAction <br />location：child/child(basePackage=com.sunflower.actions,this package=com.sunflower.actions.child) <br /><span style="COLOR: #ff0000">注意</span><br />1：ChildAction内的execute方法返回指向location="next.jsp"，查看结果页面next.jsp是根目录资源还是/child/next.jsp资源 <br />2： redirectAction方法向请求作用域设置了msg信息，查看是否显示了msg <br /><a href="http://localhost:8080/struts2/navigator!redirectAction.action">跳转到站内action</a></li><li>主题：错误跳转<br />请求url：navigator!error.action   <br /><a href="http://localhost:8080/struts2/navigator!error.action">生成错误</a></li><li>主题：@Action的使用已经Result的location绝对与相对的区别<br />使用注解@Action(value="/test/childTest") <br />请求url：/test/childTest.action   <br /><span style="COLOR: #ff0000">注意</span>：类仍然是NavigatorAction <br /><a href="http://localhost:8080/struts2/test/childTest.action">next.jsp为绝对路径</a></li><li>主题：@Actions的使用<br />注解@Actions({ @Action(value="/test/action1"), @Action(value="/test/action2") })<br />请求<br />url：/test/action1.action <br />url：/test/action2.action <br /><span style="COLOR: #ff0000">注意</span>：类仍然是NavigatorAction <br /><a href="http://localhost:8080/struts2/test/action1.action?actionName=action1">action1</a> <a href="http://localhost:8080/struts2/test/action2.action?actionName=action2">action2</a></li></ul><p style="MARGIN: 0px"> </p><p style="MARGIN: 0px">Web.xml关键部分</p><p style="MARGIN: 0px"> </p><p style="MARGIN: 0px"> </p></li>
				</ul>
				<pre name="code">&lt;!-- ============================ 使用struts2处理页面请求 ============================ --&gt;
        &lt;filter&gt;
                &lt;filter-name&gt;struts2&lt;/filter-name&gt;
                &lt;filter-class&gt;
                        org.apache.struts2.dispatcher.FilterDispatcher
                &lt;/filter-class&gt;
        &lt;/filter&gt;
        &lt;filter-mapping&gt;
                &lt;filter-name&gt;struts2&lt;/filter-name&gt;
                &lt;url-pattern&gt;*.action&lt;/url-pattern&gt;
        &lt;/filter-mapping&gt;
</pre>
				<p style="MARGIN: 0px"> </p>
				<p style="MARGIN: 0px"> </p>
				<p style="MARGIN: 0px">struts2.xml配置如下</p>
				<p style="MARGIN: 0px"> </p>
				<p style="MARGIN: 0px"> </p>
				<pre name="code">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
&lt;!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;

&lt;struts&gt;
        &lt;!--开发模式设置开始//--&gt;
        &lt;!--++++++++++++++++++++++++++++++++++++++++++++++++开发状态 --&gt;
        &lt;constant name="struts.devMode" value="true" /&gt;
        &lt;!-- 国际化资源重新加载  --&gt;
        &lt;constant name="struts.i18n.reload" value="true" /&gt;
        &lt;!-- 配置文件重新加载 --&gt;
        &lt;constant name="struts.configuration.xml.reload" value="true" /&gt;
        &lt;!-- convention类从新加载 --&gt;
        &lt;constant name="struts.convention.classes.reload" value="true" /&gt;
        &lt;!--++++++++++++++++++++++++++++++++++++++++++++++++开发模式结束 --&gt;
        &lt;!-- 主题 --&gt;
        &lt;constant name="struts.ui.theme" value="simple" /&gt;
        &lt;!-- 地区 --&gt;
        &lt;constant name="struts.locale" value="zh_CN" /&gt;
        &lt;!-- 国际化编码 --&gt;
        &lt;constant name="struts.i18n.encoding" value="UTF-8" /&gt;
        &lt;!--  扩展--&gt;
        &lt;constant name="struts.action.extension" value="action,do,jsp" /&gt;
        &lt;!-- 启用动态方法调用 --&gt;
        &lt;constant name="struts.enable.DynamicMethodInvocation" value="true" /&gt;
        &lt;!-- 设置Struts 2是否允许在Action名中使用斜线 --&gt;
        &lt;constant name="struts.enable.SlashesInActionNames" value="false" /&gt;
        &lt;!-- 结果资源所在路径 --&gt;
        &lt;constant name="struts.convention.result.path" value="/"/&gt; 
        &lt;!-- action后缀 --&gt;
        &lt;constant name="struts.convention.action.suffix" value="Action"/&gt; 
        &lt;!-- 名称首字母小写 --&gt;
    &lt;constant name="struts.convention.action.name.lowercase" value="true"/&gt; 
    &lt;!-- 分隔符 一个action名字的获取。比如为HelloWorldAction。按照配置，actionName为hello_world。 --&gt;
    &lt;constant name="struts.convention.action.name.separator" value="_"/&gt; 
    &lt;!-- 禁用扫描 --&gt;
    &lt;constant name="struts.convention.action.disableScanning" value="false"/&gt; 
    &lt;!-- 默认包 --&gt;
    &lt;constant name="struts.convention.default.parent.package" value="default"/&gt; 
    &lt;!--确定搜索包的路径。只要是结尾为action的包都要搜索。basePackage按照默认不用配置，如果配置，只会找以此配置开头的包。locators及locators.basePackage都是一组以逗号分割的字符串。 --&gt;
    &lt;constant name="struts.convention.package.locators" value="actions"/&gt; 
    &lt;!-- 禁用包搜索 --&gt;
    &lt;constant name="struts.convention.package.locators.disable" value="false"/&gt; 
    &lt;!-- 基于什么包 --&gt;
    &lt;constant name="struts.convention.package.locators.basePackage" value="com.sunflower.actions"/&gt;
    &lt;!--  排除的包 --&gt;
    &lt;constant name="struts.convention.exclude.packages" value="org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.*"/&gt;
    &lt;!-- 包含的包 --&gt;
    &lt;!-- 包括的jar,一般用于大型项目，其action一般打包成jar --&gt;
    &lt;constant name="struts.convention.action.includeJars" value="" /&gt;
    &lt;!-- 结果类型 --&gt;
    &lt;constant name="struts.convention.relative.result.types" value="dispatcher,freemarker"/&gt; 
    &lt;!-- 
     如果此值设为true，如果一个action的命名空间为/login，名称为HelloWorldAction。result返回值是success，默认会找到/WEB-INF/pages/login/hello_world.jsp（如果有hello_world_success.jsp就找这个文件，连接符“_”是在&lt;constant name="struts.convention.action.name.separator" value="_"/&gt;中配置的）。如果有一个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。 
     --&gt;
    &lt;constant name="struts.convention.result.flatLayout" value="true"/&gt;
    &lt;constant name="struts.convention.action.mapAllMatches" value="false"/&gt; 
    &lt;!-- 检查是否实现action --&gt;
    &lt;constant name="struts.convention.action.checkImplementsAction" value="true"/&gt;
    &lt;constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/&gt;
        &lt;constant name="struts.convention.redirect.to.slash" value="true"/&gt;
        &lt;package name="default" extends="struts-default"&gt;
                &lt;interceptors&gt;
                        &lt;interceptor-stack name="defaultStack"&gt;
                                &lt;interceptor-ref name="exception" /&gt;
                                &lt;interceptor-ref name="servletConfig" /&gt;
                                &lt;interceptor-ref name="actionMappingParams" /&gt;
                                &lt;interceptor-ref name="staticParams" /&gt;
                                &lt;interceptor-ref name="params" /&gt;
                        &lt;/interceptor-stack&gt;
                &lt;/interceptors&gt;
        &lt;/package&gt;
&lt;/struts&gt;
</pre>
				<p style="MARGIN: 0px"> </p>
				<p style="MARGIN: 0px"> </p>
				<p style="MARGIN: 0px">
						<span style="COLOR: #ff0000">NavigatorAction源码</span>
				</p>
				<p style="MARGIN: 0px"> </p>
				<pre name="code">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.javaeye.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);
        }
}
</pre>
				<p style="MARGIN: 0px">  </p>
				<p style="MARGIN: 0px"> </p>
				<p style="MARGIN: 0px">
						<span style="COLOR: #ff0000"> ChildAction源码</span>
				</p>
				<p style="MARGIN: 0px"> </p>
				<pre name="code">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";
        }
}
</pre>
				<p style="MARGIN: 0px"> </p>
				<div style="MARGIN-LEFT: 2em">
						<span style="COLOR: #ff0000"> index.jsp源码</span>
				</div>
				<div style="MARGIN-LEFT: 2em">
						<pre name="code">
								<span style="COLOR: #ff0000">&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;struts2 convention测试&lt;/title&gt;
&lt;style&gt;
* {
        line-height: 2em;
}

a:visited,a:hover,a:active,a:link {
        color: #00f;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
后台Action&amp;nbsp;
&lt;ul class="horizontal"&gt;
        &lt;li&gt;com.sunflower.actions.NavigatorAction&lt;/li&gt;
        &lt;li&gt;com.sunflower.actions.child.ChildAction&lt;/li&gt;
&lt;/ul&gt;
页面资源&amp;nbsp;
&lt;ul class="horizontal"&gt;
        &lt;li&gt;error.jsp&lt;/li&gt;
        &lt;li&gt;index.jsp&lt;/li&gt;
        &lt;li&gt;next.jsp&lt;/li&gt;
        &lt;li&gt;child/next.jsp&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
        &lt;li&gt;主题：确认action内默认执行方法是execute还是index&lt;br /&gt;
        请求url：navigator.action&amp;nbsp;&amp;nbsp;&lt;br /&gt;
        类型：dispatcher &lt;br /&gt;
        方法：execute或index &lt;br /&gt;
        &lt;a href="./navigator.action"&gt;执行默认方法&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;主题：测试如何跳转到站外&lt;br /&gt;
        请求url：navigator!redirect.action&amp;nbsp;&amp;nbsp; &lt;br /&gt;
        类型：redirect（旧版本使用ServletRedirectResult.class） &lt;br /&gt;
        方法：redirect &lt;br /&gt;
        &lt;a href="./navigator!redirect.action"&gt;跳转到站外&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;主题：测试redirectAction、basePackage内子包action的映射、以及参数传递&lt;br /&gt;
        请求url：navigator!redirectAction.action&amp;nbsp;&amp;nbsp; &lt;br /&gt;
        类型：redirectAction （旧版本使用ServeltActionRedirectResult.class） &lt;br /&gt;
        方法：redirectAction &lt;br /&gt;
        location：child/child(basePackage=com.sunflower.actions,this&amp;nbsp;package=com.sunflower.actions.child)
        &lt;br /&gt;
        &lt;font color="red"&gt;注意&lt;/font&gt;&lt;br /&gt;
        1：ChildAction内的execute方法返回指向location="next.jsp"，查看结果页面next.jsp是根目录资源还是/child/next.jsp资源
        &lt;br /&gt;
        2： redirectAction方法向请求作用域设置了msg信息，查看是否显示了msg &lt;br /&gt;
        &lt;a href="./navigator!redirectAction.action"&gt;跳转到站内action&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;主题：错误跳转&lt;br /&gt;
        请求url：navigator!error.action&amp;nbsp;&amp;nbsp; &lt;br /&gt;
        &lt;a href="./navigator!error.action"&gt;生成错误&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;主题：@Action的使用已经Result的location绝对与相对的区别&lt;br /&gt;
        使用注解@Action(value="/test/childTest") &lt;br /&gt;
        请求url：/test/childTest.action&amp;nbsp;&amp;nbsp; &lt;br /&gt;
        &lt;font color="red"&gt;注意&lt;/font&gt;：类仍然是NavigatorAction &lt;br /&gt;
        &lt;a href="./test/childTest.action"&gt;next.jsp为绝对路径&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;主题：@Actions的使用&lt;br /&gt;
        注解@Actions({ @Action(value="/test/actions1"),
        @Action(value="/test/actions2") })&lt;br /&gt;
        请求&lt;br /&gt;
        url：/test/action1.action &lt;br /&gt;
        url：/test/action2.action &lt;br /&gt;
        &lt;font color="red"&gt;注意&lt;/font&gt;：类仍然是NavigatorAction &lt;br /&gt;
        &lt;a href="./test/action1.action?actionName=action1"&gt;action1&lt;/a&gt;&amp;nbsp;&lt;a
                href="./test/action2.action?actionName=action2"&gt;action2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;</span>
						</pre>
						<p style="MARGIN: 0px"> </p>
						<p style="MARGIN: 0px">
								<span style="COLOR: #ff0000">
								</span> </p>
						<p style="MARGIN: 0px">
								<span style="COLOR: #ff0000"> next.jsp源码</span>
						</p>
						<p style="MARGIN: 0px"> </p>
				</div>
				<pre name="code">&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;Insert title here&lt;/title&gt;&lt;style&gt;
*{
line-height:3em;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;struts2 &amp;nbsp;&amp;nbsp;&amp;nbsp;convention插件使用测试&lt;/h1&gt;
测试信息
&lt;br /&gt;
${msg}
&lt;br /&gt;
&lt;a href="#" onclick="window.history.go(-1);return false;"&gt;返回&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
				<p style="MARGIN: 0px"> </p>
				<p style="MARGIN: 0px"> </p>
				<p style="MARGIN: 0px"> <span style="COLOR: #ff0000">child/next.jsp源码</span></p>
				<p style="MARGIN: 0px"> </p>
				<pre name="code">&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;Insert title here&lt;/title&gt;&lt;style&gt;
*{
line-height:3em;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
child/next.jsp:{msg}
&lt;/body&gt;
&lt;/html&gt;
</pre>
		</div>
<img src ="http://www.blogjava.net/martinyuan/aggbug/271945.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/martinyuan/" target="_blank">Martin Yuan</a> 2009-05-21 13:49 <a href="http://www.blogjava.net/martinyuan/articles/271945.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>