本方列举了Action类中,几种配置了 @Namespace 和 @Action 的情况,以及这些情况下如何访问Action类中的相应方法。
情况一:
@Namespace("/test") @Namespaces({ @Namespace("/test2"), @Namespace("/test3") }) @Action("/test4") @Actions({ @Action("/test5"), @Action("/test6") }) public class FileAction { public String upload() { System.out.println("uploading files..."); return "success"; } }
对于上面的这种配置,我们可以通以下的URL去调用FileAction中的upload()方法:
http://xxx.xxx.xxx.xxx:port/web工程名/test/test5!upload http://xxx.xxx.xxx.xxx:port/web工程名/test/test6!upload http://xxx.xxx.xxx.xxx:port/web工程名/test2/test5!upload http://xxx.xxx.xxx.xxx:port/web工程名/test2/test6!upload http://xxx.xxx.xxx.xxx:port/web工程名/test3/test5!upload http://xxx.xxx.xxx.xxx:port/web工程名/test3/test6!upload
PS: 注意:@Action("/test4") 并未生效,因为它被@Actions({ @Action("/test5"), @Action("/test6") }) 取替了。原因。请看下面的源码:
org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildConfiguration() protected void buildConfiguration(Set<Class> classes) { Map<String, PackageConfig.Builder> packageConfigs = new HashMap<String, PackageConfig.Builder>(); for (Class<?> actionClass : classes) { Actions actionsAnnotation = actionClass.getAnnotation(Actions.class); Action actionAnnotation = actionClass.getAnnotation(Action.class); .......此处省略N行代码 //if there are @Actions or @Action at the class level, create the mappings for them String methodName = hasDefaultMethod ? DEFAULT_METHOD : null; if (actionsAnnotation != null) { List<Action> actionAnnotations = checkActionsAnnotation(actionsAnnotation); for (Action actionAnnotation2 : actionAnnotations) createActionConfig(defaultPackageConfig, actionClass, defaultActionName, methodName, actionAnnotation2); } else if (actionAnnotation != null) createActionConfig(defaultPackageConfig, actionClass, defaultActionName, methodName, actionAnnotation); } } .......此处省略N行代码 } }
从源码来看,当有@Actions注解时,@Action注解将被无视
大家可能会问,那 @Namespaces 会不会 覆盖 @Namespace 呢?答案是不会。原因也请看下面的源码:
org.apache.struts2.convention.PackageBasedActionConfigBuilder.determineActionNamespace() protected List<String> determineActionNamespace(Class<?> actionClass) { List<String> namespaces = new ArrayList<String>(); // Check if there is a class or package level annotation for the namespace //single namespace Namespace namespaceAnnotation = AnnotationTools.findAnnotation(actionClass, Namespace.class); if (namespaceAnnotation != null) { if (LOG.isTraceEnabled()) { LOG.trace("Using non-default action namespace from Namespace annotation of [#0]", namespaceAnnotation.value()); } namespaces.add(namespaceAnnotation.value()); } //multiple annotations Namespaces namespacesAnnotation = AnnotationTools.findAnnotation(actionClass, Namespaces.class); if (namespacesAnnotation != null) { if (LOG.isTraceEnabled()) { StringBuilder sb = new StringBuilder(); for (Namespace namespace : namespacesAnnotation.value()) sb.append(namespace.value()).append(","); sb.deleteCharAt(sb.length() - 1); LOG.trace("Using non-default action namespaces from Namespaces annotation of [#0]", sb.toString()); } for (Namespace namespace : namespacesAnnotation.value()) namespaces.add(namespace.value()); } //don't use default if there are annotations if (!namespaces.isEmpty()) return namespaces; ......此处省略N行代码 }
从源码来看,是先读取@Namespace中的namspace,然后再读取@Namespaces中的namespace(s),将它们的值合并到一起进行返回的。
但是,一般情况下,配置了@Namespaces 或者 @Actions 时,就不会再去配置 @Namespace 或者 @Action了,谁吃饱了没事做啊 : )
转回正题,继续列出另一种配置方式:
情况二:
@Namespace("/test") @Namespaces({ @Namespace("/test2"), @Namespace("/test3") }) @Action("/test4") @Actions({ @Action("/test5"), @Action("/test6") }) public class FileAction { @Action("/test7") @Actions({ @Action("/test8"), @Action("/test9") }) public String upload() { System.out.println("uploading files..."); return "success"; } public String gotoUpload() { return "gotoUpload"; } }
对于上面的这种配置,我们可以通以下的URL去调用FileAction中的upload()方法:
http://xxx.xxx.xxx.xxx:port/web工程名/test/test8 http://xxx.xxx.xxx.xxx:port/web工程名/test/test9 http://xxx.xxx.xxx.xxx:port/web工程名/test2/test8 http://xxx.xxx.xxx.xxx:port/web工程名/test2/test9 http://xxx.xxx.xxx.xxx:port/web工程名/test3/test8 http://xxx.xxx.xxx.xxx:port/web工程名/test3/test9 http://xxx.xxx.xxx.xxx:port/web工程名/test8 http://xxx.xxx.xxx.xxx:port/web工程名/test9
相关推荐
以前学习struts2时候,自己练习的代码,可以供初学者参考学习
struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置...
struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序
前几天在网上下载一个struts2的helloword的例子,那个作者也真够缺德的,搞个错误的程序,害得我查了一天的程序错误。 最后发现竟然是struts.xml被写成啦sturts.xml。 碰见这样的问题先鄙视下提供例子的作者, 再...
struts2中的文件上传和下载示例 struts2中的文件上传和下载示例 struts2中的文件上传和下载示例
struts taglib使用示例 struts taglib使用示例 struts taglib使用示例struts taglib使用示例 struts taglib使用示例
struts2的验证框架的示例代码,快速学习和使用struts2验证框架
struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> ...
9.1 为什么在Struts 2中使用Spring 196 9.1.1 依赖注入能做些什么 197 9.1.2 Spring如何管理对象和注入依赖 199 9.1.3 使用接口隐藏实现 200 9.2 将Spring添加到Struts 2 202 9.2.1 让Spring管理动作、拦截器和结果...
Struts2示例
资源中示例了一个struts2的配置文件中配置的action的result结果为另外一个action的配置,该跳转的过程中是带所有参赛调整的,在另外一个action中可以接受到当前action的所有参数,此配置文件是我日常开发中的经验...
Struts 2实战 struts2 in Action
Struts2的框架及注释和使用法,希望大家支持,我们一起努力,谢谢!
Struts示例demo
NULL 博文链接:https://cdxs2.iteye.com/blog/1925245
最近在做struts2的学习研究,这里共享一份完整能运行的示例
Struts2框架程序示例
b 在struts.xml中添加action,使用通配符的方式 , 注意这里和单独struts2不同的地方: class属性指向的是bean 的 id ,这里我们配置bean采用spring ioc注解的方式, 所以默认的bean的id 为 类名(首字母小写) ...
Struts2中struts_xml的Action配置详解
struts2 示例 目的:构建struts2开发框架 适合:有一定struts2基础 实现:通过对用户信息的基本操作来给大家做个示范 1、struts2的搭建 2、web.xml文件的简单配置 3、struts.xml文件的简单配置 4、网站初始化工作...