Struts2的使用注解配置Action(零配置)

1,首先引入struts2注解的jar包:struts2-convention-plugin.jar

[java]  view plain  copy
  1.        <!-- struts2注解 -->  
  2. <dependency>  
  3. <groupId>org.apache.struts</groupId>  
  4. <artifactId>struts2-convention-plugin</artifactId>  
  5. <version>${struts2-version}</version>  
  6. lt;/dependency>  


2,classpath下struts.xml配置文件

[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5. <struts>  
  6.   
  7.     <!-- 开发模式下的配置 -->  
  8.     <!-- 开发模式 配置文件改了,不用重新启动-->  
  9.     <!-- <constant name="struts.devMode" value="true" /> -->  
  10.     <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好 关闭 -->     
  11.     <constant name="struts.serve.static.browserCache" value="false" />  
  12.     <!-- 当 struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生 产环境下使用),开发阶段最好打开 -->   
  13.     <constant name="struts.configuration.xml.reload" value="true"/>  
  14.           
  15.     <!-- 请求参数的编码方式-->   
  16.     <constant name="struts.i18n.encoding" value="UTF-8"/>   
  17.     <!--  struts2拦截器默认拦截的后缀名     多个可以用逗号隔开  用于做网站伪静态化-->  
  18.     <constant name="struts.action.extension" value="html"/>  
  19.     <!-- 简单主题 -->  
  20.     <constant name="struts.ui.theme" value="simple" />  
  21.     <!-- 配置文件上传的总大小 -->  
  22.     <constant name="struts.multipart.maxSize" value="2097152000"></constant>  
  23.     <!-- 把struts的请求委托给spring管理, 作用:创建Action实例的过程由spring处理,其他的还是由struts2自己处理 -->  
  24.     <constant name="struts.objectFactory" value="spring" />   
  25.      <!-- 是否开启动态方法调用-->   
  26.     <constant name="struts.enable.DynamicMethodInvocation" value="false"/>   
  27.       
  28.   
  29.     <!-- Struts2约定限制结果页面必须放到/WEB-INF/content/目录中,可以通过下面常量来修改这个限制。-->  
  30.     <constant name="struts.convention.result.path" value="/WEB-INF/jsp" />  
  31.     <!-- 约定Action包位置:可以使用的Action的祖包,默认为没有, 设置为 cn.javass.ssh 就可以读取这个包及其子包的所有头上注册了【 @Action 】 的类。-->  
  32.     <!-- <constant name="struts.convention.action.packages" value="cn.hl.s2sh.user.controller" /> -->  
  33.     <!-- 默认配置包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。通过设置struts.convention.package.locators属性来修改这个配置。 -->  
  34.     <constant name="struts.convention.package.locators"  value="action,actions,struts,struts2,controller" />   
  35.     <!-- 约定Action类名:要求Action的命名必须以Action为后缀,可以使用下面常量来修改后缀限制 -->  
  36.     <constant name="struts.convention.action.suffix"  value="Action" />   
  37.       
  38.       
  39.       
  40.       
  41.       
  42.     <!-- 配置全局的包  继承自json-default,json-default继承自struts-default包 --->  
  43.     <package name="mypackage" extends="json-default">  
  44.         <!-- <interceptors>  
  45.             注册自定义登录拦截器  
  46.             <interceptor name="login" class="com.hl.zoneSystem_v01.struts.common.MyInterceptor" />  
  47.             自定义拦截器栈  
  48.             <interceptor-stack name="myInterceptor">  
  49.                 <interceptor-ref name="login" />  
  50.                 <interceptor-ref name="defaultStack">  
  51.                     <param name="modelDriven.refreshModelBeforeResult">true</param>  
  52.                 </interceptor-ref>  
  53.             </interceptor-stack>  
  54.         </interceptors> -->  
  55.         <!-- 定义默认栈 -->  
  56.         <!-- <default-interceptor-ref name="myInterceptor" /> -->  
  57.         <!-- 定义默认action -->  
  58.         <!-- <default-action-ref name="notFound" /> -->  
  59.           
  60.         <!-- 全局结果集 -->  
  61.         <global-results>  
  62.             <!-- ajax请求的结果集  表单验证用,值为true/false-->  
  63.             <result type="json" name="ajax">  
  64.                 <param name="root">message</param>  
  65.             </result>  
  66.             <!-- ajax请求的结果集  消息推送用,值为数字-->  
  67.             <result type="json" name="ajax2">  
  68.                 <param name="root">message2</param>  
  69.             </result>  
  70.             <!-- ajax请求的结果集 页面数据传送 返回map-->  
  71.             <result type="json" name="jsonResult">  
  72.                 <param name="root">jsonMap</param>  
  73.             </result>  
  74.             <!-- 下载 -->  
  75.             <result name="downloadFiles" type="stream">  
  76.                 <!-- 对应web中下载配置:下载类型无限制  文件编码UTF-8-->  
  77.                 <param name="contentType">application/octet-stream;charset=UTF-8</param>  
  78.                 <param name="inputName">inputStreamFile</param>  
  79.                 <!-- 对应web中下载配置:response.setHeader("Content-Disposition""attachment;filename="+newUser.getFilename()); -->  
  80.                 <param name="contentDisposition">attachment;filename=${fileName}</param>  
  81.                 <!-- 对应web中下载配置:byte[] b = new byte[1024]; -->  
  82.                 <param name="bufferSize">1024</param>  
  83.             </result>  
  84.             <!-- 项目根页面 -->  
  85.             <result name="index" type="redirect">/index.jsp</result>  
  86.             <!-- login 登录 -->  
  87.             <result name="loginUI">/login.jsp</result>  
  88.             <!-- 前台全局消息显示页面 -->  
  89.             <result name="clientMessage">/message.jsp</result>  
  90.             <!-- action错误,处理结果页面   -->  
  91.             <result name="error">/error.jsp</result>  
  92.         </global-results>  
  93.         <!-- 全局异常 -->  
  94.         <!-- <global-exception-mappings>  
  95.             <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>  
  96.         </global-exception-mappings> -->  
  97.           
  98.     </package>  
  99.       
  100.       
  101.       
  102.       
  103. </struts>  


3,action中写法

controller包下

[java]  view plain  copy
  1. package cn.hl.s2sh.user.controller;  
  2.   
  3.   
  4.   
  5. import net.sf.ehcache.Cache;  
  6.   
  7. import org.apache.commons.logging.Log;  
  8. import org.apache.commons.logging.LogFactory;  
  9. import org.apache.struts2.convention.annotation.Action;  
  10. import org.apache.struts2.convention.annotation.ExceptionMapping;  
  11. import org.apache.struts2.convention.annotation.ExceptionMappings;  
  12. import org.apache.struts2.convention.annotation.Namespace;  
  13. import org.apache.struts2.convention.annotation.ParentPackage;  
  14. import org.apache.struts2.convention.annotation.Result;  
  15. import org.apache.struts2.convention.annotation.Results;  
  16. import org.springframework.context.annotation.Scope;  
  17. import org.springframework.stereotype.Controller;  
  18.   
  19. import cn.hl.s2sh.base.controller.struts2.BaseAction;  
  20. import cn.hl.s2sh.base.plugins.cache.CachedManager;  
  21.   
  22. /** 
  23.  * @Description: user控制器 
  24.  * @author hl 
  25.  * @date 2014-11-18 下午11:10:21 
  26.  * 
  27.  */  
  28. @Controller  
  29. @Scope("prototype")  
  30. @ParentPackage("mypackage")  
  31. @Results({  
  32.     @Result(name="test1",location="test.jsp"),  
  33.     @Result(name="test2",location="/WEB-INF/jsp/test2.jsp"),  
  34.     @Result(name="main_postTalk",type="redirectAction",location="main_mindex",params={"uid","${uid}"})  
  35. })  
  36. @ExceptionMappings({@ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })  
  37. @Namespace(value="/u")  
  38. public class UserAction extends BaseAction{  
  39.       
  40.       
  41.     //log4j  
  42.     private static final Log log = LogFactory.getLog(UserAction.class);  
  43.       
  44.       
  45.       
  46.       
  47.       
  48.     @Action("/test1")  
  49.     public String test1(){  
  50.           
  51.         CachedManager.putObjectCache("phoneCode""a""aaa");  
  52.         Cache c = CachedManager.getCache("phoneCode");  
  53.         Cache c2 = CachedManager.getCache("phoneCode2");  
  54.         System.out.println("--------test1---1");  
  55.         //log.debug("--manager-" + manager);  
  56.           
  57.               
  58.           
  59.         return "test1";  
  60.     }  
  61.       
  62.       
  63.       
  64.       
  65.       
  66.     @Action(value="test2")  
  67.     public String test2(){  
  68.           
  69.         String s = (String) CachedManager.getObjectCache("phoneCode""a");  
  70.           
  71.         System.out.println(s);  
  72.           
  73.           
  74.         CachedManager.remove("phoneCode""a");  
  75.           
  76.         System.out.println("--------test2---2");  
  77.           
  78.         return "test2";   
  79.     }  
  80.       
  81.       
  82.       
  83.       
  84.       
  85.       
  86.   
  87. }  

部署启动tomcat,访问

localhost:8080/项目名/u/test1.html

localhost:8080/项目名/u/test2.html

localhost:8080/项目名/test1.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值