struts2 convention插件的使用(一)

来源:互联网 发布:如何使用linux系统 编辑:程序博客网 时间:2024/06/04 20:02

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关键部分

     

     

Xml代码  收藏代码
  1. <!-- ============================ 使用struts2处理页面请求 ============================ -->  
  2.     <filter>  
  3.         <filter-name>struts2</filter-name>  
  4.         <filter-class>  
  5.             org.apache.struts2.dispatcher.FilterDispatcher  
  6.         </filter-class>  
  7.     </filter>  
  8.     <filter-mapping>  
  9.         <filter-name>struts2</filter-name>  
  10.         <url-pattern>*.action</url-pattern>  
  11.     </filter-mapping>  

 

 

struts2.xml配置如下

 

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <!--开发模式设置开始//-->  
  8.     <!--++++++++++++++++++++++++++++++++++++++++++++++++开发状态 -->  
  9.     <constant name="struts.devMode" value="true" />  
  10.     <!-- 国际化资源重新加载  -->  
  11.     <constant name="struts.i18n.reload" value="true" />  
  12.     <!-- 配置文件重新加载 -->  
  13.     <constant name="struts.configuration.xml.reload" value="true" />  
  14.     <!-- convention类从新加载 -->  
  15.     <constant name="struts.convention.classes.reload" value="true" />  
  16.     <!--++++++++++++++++++++++++++++++++++++++++++++++++开发模式结束 -->  
  17.     <!-- 主题 -->  
  18.     <constant name="struts.ui.theme" value="simple" />  
  19.     <!-- 地区 -->  
  20.     <constant name="struts.locale" value="zh_CN" />  
  21.     <!-- 国际化编码 -->  
  22.     <constant name="struts.i18n.encoding" value="UTF-8" />  
  23.     <!--  扩展-->  
  24.     <constant name="struts.action.extension" value="action,do,jsp" />  
  25.     <!-- 启用动态方法调用 -->  
  26.     <constant name="struts.enable.DynamicMethodInvocation" value="true" />  
  27.     <!-- 设置Struts 2是否允许在Action名中使用斜线 -->  
  28.     <constant name="struts.enable.SlashesInActionNames" value="false" />  
  29.     <!-- 结果资源所在路径 -->  
  30.     <constant name="struts.convention.result.path" value="/"/>   
  31.     <!-- action后缀 -->  
  32.     <constant name="struts.convention.action.suffix" value="Action"/>   
  33.     <!-- 名称首字母小写 -->  
  34.     <constant name="struts.convention.action.name.lowercase" value="true"/>   
  35.     <!-- 分隔符 一个action名字的获取。比如为HelloWorldAction。按照配置,actionName为hello_world。 -->  
  36.     <constant name="struts.convention.action.name.separator" value="_"/>   
  37.     <!-- 禁用扫描 -->  
  38.     <constant name="struts.convention.action.disableScanning" value="false"/>   
  39.     <!-- 默认包 -->  
  40.     <constant name="struts.convention.default.parent.package" value="default"/>   
  41.     <!--确定搜索包的路径。只要是结尾为action的包都要搜索。basePackage按照默认不用配置,如果配置,只会找以此配置开头的包。locators及locators.basePackage都是一组以逗号分割的字符串。 -->  
  42.     <constant name="struts.convention.package.locators" value="actions"/>   
  43.     <!-- 禁用包搜索 -->  
  44.     <constant name="struts.convention.package.locators.disable" value="false"/>   
  45.     <!-- 基于什么包 -->  
  46.     <constant name="struts.convention.package.locators.basePackage" value="com.sunflower.actions"/>  
  47.     <!--  排除的包 -->  
  48.     <constant name="struts.convention.exclude.packages" value="org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.*"/>  
  49.     <!-- 包含的包 -->  
  50.     <!-- 包括的jar,一般用于大型项目,其action一般打包成jar -->  
  51.     <constant name="struts.convention.action.includeJars" value="" />  
  52.     <!-- 结果类型 -->  
  53.     <constant name="struts.convention.relative.result.types" value="dispatcher,freemarker"/>   
  54.     <!--   
  55.      如果此值设为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。   
  56.   
  57.       如果此值设为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。   
  58.      -->  
  59.     <constant name="struts.convention.result.flatLayout" value="true"/>  
  60.     <constant name="struts.convention.action.mapAllMatches" value="false"/>   
  61.     <!-- 检查是否实现action -->  
  62.     <constant name="struts.convention.action.checkImplementsAction" value="true"/>  
  63.     <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>  
  64.     <constant name="struts.convention.redirect.to.slash" value="true"/>  
  65.     <package name="default" extends="struts-default">  
  66.         <interceptors>  
  67.             <interceptor-stack name="defaultStack">  
  68.                 <interceptor-ref name="exception" />  
  69.                 <interceptor-ref name="servletConfig" />  
  70.                 <interceptor-ref name="actionMappingParams" />  
  71.                 <interceptor-ref name="staticParams" />  
  72.                 <interceptor-ref name="params" />  
  73.             </interceptor-stack>  
  74.         </interceptors>  
  75.     </package>  
  76. </struts>  

 

 

NavigatorAction源码

 

Java代码  收藏代码
  1. package com.sunflower.actions;  
  2.   
  3. import org.apache.struts2.convention.annotation.Action;  
  4. import org.apache.struts2.convention.annotation.Actions;  
  5. import org.apache.struts2.convention.annotation.Result;  
  6. import org.apache.struts2.convention.annotation.Results;  
  7.   
  8. import com.opensymphony.xwork2.ActionContext;  
  9.   
  10. @Results( {  
  11.         @Result(name = "next", location = "/next.jsp", type = "dispatcher"),  
  12.         @Result(name = "error", location = "error.jsp", type = "dispatcher"),  
  13.         @Result(name = "redirectAction", location = "./child/child", type = "redirectAction"),  
  14.         @Result(name = "redirect", location = "http://sunflowers.iteye.com", type = "redirect") })  
  15. public class NavigatorAction {  
  16.     private String actionName;  
  17.   
  18.     public void setActionName(String actionName) {  
  19.         this.actionName = actionName;  
  20.     }  
  21.   
  22.     public String index() {  
  23.         outputMsg("method:index");  
  24.         return "next";  
  25.     }  
  26.   
  27.     public String execute() {  
  28.         outputMsg("method:execute,no index method");  
  29.         return "next";  
  30.     }  
  31.   
  32.     public String error() {  
  33.         try {  
  34.             throw new Exception();  
  35.         } catch (Exception e) {  
  36.             outputMsg(e);  
  37.             return "error";  
  38.         }  
  39.     }  
  40.   
  41.     public String redirect() {  
  42.         System.out.println("重定向:rediret");  
  43.         return "redirect";  
  44.     }  
  45.   
  46.     public String redirectAction() {  
  47.         outputMsg("navigatorAction 跳转而来,原地址是navigator!redirectAction.action,请查看地址栏");  
  48.         return "redirectAction";  
  49.     }  
  50.   
  51.     @Action(value = "/test/childTest")  
  52.     public String action() {  
  53.         outputMsg("@action ---method:action");  
  54.         return "next";  
  55.     }  
  56.   
  57.     @Actions( { @Action(value = "/test/action1"),  
  58.             @Action(value = "/test/action2") })  
  59.     public String actions() {  
  60.         outputMsg("@actions ---method:actions,action=" + actionName);  
  61.         return "next";  
  62.     }  
  63.     private void outputMsg(Object msg) {  
  64.         System.out.println(msg);  
  65.         ActionContext.getContext().put("msg", msg);  
  66.     }  
  67. }  

 

 

 ChildAction源码

 

Java代码  收藏代码
  1. package com.sunflower.actions.child;  
  2.   
  3. import org.apache.struts2.convention.annotation.Result;  
  4. import org.apache.struts2.convention.annotation.Results;  
  5.   
  6. @Results( {  
  7.         @Result(name = "next", location = "next.jsp", type = "dispatcher")})  
  8. public class ChildAction {  
  9.     public String execute() {  
  10.         System.out.println("childAction 默认方法执行");  
  11.         return "next";  
  12.     }  
  13. }  

 

     index.jsp源码
    Html代码  收藏代码
    1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
    2.     pageEncoding="UTF-8"%>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    4. <html>  
    5. <head>  
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    7. <title>struts2 convention测试</title>  
    8. <style>  
    9. * {  
    10.     line-height: 2em;  
    11. }  
    12.   
    13. a:visited,a:hover,a:active,a:link {  
    14.     color: #00f;  
    15. }  
    16. </style>  
    17. </head>  
    18. <body>  
    19. 后台Action&nbsp;  
    20. <ul class="horizontal">  
    21.     <li>com.sunflower.actions.NavigatorAction</li>  
    22.     <li>com.sunflower.actions.child.ChildAction</li>  
    23. </ul>  
    24. 页面资源&nbsp;  
    25. <ul class="horizontal">  
    26.     <li>error.jsp</li>  
    27.     <li>index.jsp</li>  
    28.     <li>next.jsp</li>  
    29.     <li>child/next.jsp</li>  
    30. </ul>  
    31. <ul>  
    32.     <li>主题:确认action内默认执行方法是execute还是index<br />  
    33.     请求url:navigator.action&nbsp;&nbsp;<br />  
    34.     类型:dispatcher <br />  
    35.     方法:execute或index <br />  
    36.     <a href="./navigator.action">执行默认方法</a></li>  
    37.     <li>主题:测试如何跳转到站外<br />  
    38.     请求url:navigator!redirect.action&nbsp;&nbsp; <br />  
    39.     类型:redirect(旧版本使用ServletRedirectResult.class) <br />  
    40.     方法:redirect <br />  
    41.     <a href="./navigator!redirect.action">跳转到站外</a></li>  
    42.     <li>主题:测试redirectAction、basePackage内子包action的映射、以及参数传递<br />  
    43.     请求url:navigator!redirectAction.action&nbsp;&nbsp; <br />  
    44.     类型:redirectAction (旧版本使用ServeltActionRedirectResult.class) <br />  
    45.     方法:redirectAction <br />  
    46.     location:child/child(basePackage=com.sunflower.actions,this&nbsp;package=com.sunflower.actions.child)  
    47.     <br />  
    48.     <font color="red">注意</font><br />  
    49.     1:ChildAction内的execute方法返回指向location="next.jsp",查看结果页面next.jsp是根目录资源还是/child/next.jsp资源  
    50.     <br />  
    51.     2: redirectAction方法向请求作用域设置了msg信息,查看是否显示了msg <br />  
    52.     <a href="./navigator!redirectAction.action">跳转到站内action</a></li>  
    53.     <li>主题:错误跳转<br />  
    54.     请求url:navigator!error.action&nbsp;&nbsp; <br />  
    55.     <a href="./navigator!error.action">生成错误</a></li>  
    56.     <li>主题:@Action的使用已经Result的location绝对与相对的区别<br />  
    57.     使用注解@Action(value="/test/childTest"<br />  
    58.     请求url:/test/childTest.action&nbsp;&nbsp; <br />  
    59.     <font color="red">注意</font>:类仍然是NavigatorAction <br />  
    60.     <a href="./test/childTest.action">next.jsp为绝对路径</a></li>  
    61.     <li>主题:@Actions的使用<br />  
    62.     注解@Actions({ @Action(value="/test/actions1"),  
    63.     @Action(value="/test/actions2") })<br />  
    64.     请求<br />  
    65.     url:/test/action1.action <br />  
    66.     url:/test/action2.action <br />  
    67.     <font color="red">注意</font>:类仍然是NavigatorAction <br />  
    68.     <a href="./test/action1.action?actionName=action1">action1</a>&nbsp;<a  
    69.         href="./test/action2.action?actionName=action2">action2</a></li>  
    70. </ul>  
    71. </body>  
    72. </html>  

     

     

     next.jsp源码

     

Html代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title><style>  
  8. *{  
  9. line-height:3em;  
  10. }  
  11. </style>  
  12. </head>  
  13. <body>  
  14. <h1>struts2 &nbsp;&nbsp;&nbsp;convention插件使用测试</h1>  
  15. 测试信息  
  16. <br />  
  17. ${msg}  
  18. <br />  
  19. <a href="#" onclick="window.history.go(-1);return false;">返回</a>  
  20. </body>  
  21. </html>  

 

 

 child/next.jsp源码

 

Html代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title><style>  
  8. *{  
  9. line-height:3em;  
  10. }  
  11. </style>  
  12. </head>  
  13. <body>  
  14. child/next.jsp:{msg}  
  15. </body>  
  16. </html>