Struts2.1.6中的规约插件

本文介绍Struts2.1中的Convention插件,该插件整合了原有的CodeBehind插件和ZeroConfig插件的功能,并提供了多种约定机制,如通过包名定位Action、通过URL定位类名等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<说明:翻译自Struts2.1.6的文档,由于英语水平有限,很多地方翻译的不是很通顺,如果要看原文档,请参考Convention Plugin>

1 介绍
2 安装
3 Hello world
4 Code behind hello world
5 Results and result codes
6 Chaining
7 XWork packages
8 注解参考
8.1 Action annotation
8.2 InterceptorRef annotation
8.3 Result annotation
8.4 Namespace annotation
8.5 ResultPath annotation
8.6 ParentPackage annotation
8.7 ExceptionMapping Annotation
9 自动配置装载
10 故障排除
10.1 Tips
10.2 Common Errors
11 配置参考

 

1.介绍

从struts2.1开始捆绑规约(Convention)插件,该插件替代了原先的 CodeBehind 插件和Zero Config插件。该插件提供如下特性:

  • 通过包名称规约定位action
  • 通过名称规约定位视图(如JSP,FreeMarker等)
  • 通过URL名称规约定位类名
  • 通过名称规约定位包名
  • SEO compliant ULRs(例如 my-action 而不是MyAction)
  • 利用注解复写Action名
  • 利用注解复写拦截器
  • 利用注解复写名称空间
  • 利用注解复写XWork包
  • Default action and result handling (i.e. /products will try com.example.actions.Products as well as com.example.actions.products.Index)

The Convention Plugin should require no configuration to use. Many of the conventions can be controlled using configuration properties and many of the classes can be extended or overridden.

2.安装

为了用这个插件,首先你必须把该插件的jar文件添加到你应用的WEB-INF/lib目录下(如果你把你的action打包成一个jar文件,请务必确认将struts.convention.action.disableJarScanning设置成ture)

3.Hello World

现在规约插件已经添加到你的应用了,让我们从一个简单的例子开始。该例子将用一个没有action的视图,该视图有URL定义。默认情况下,规约插件假设所有的视图都存放在WEB-INF/content。可以改变这个默认配置,通过修改Struts属性文件中的struts.convention.result.path来指定新的位置。Don't worry about trailing slashes, the Convention plugin handles this for you. Here is our hello world JSP:

WEB-INF/content/hello-world.jsp
<html>
<body>
Hello world!
</body>
</html>

If you start Tomcat (or whichever J2EE container you are using) and type in http://localhost:8080/hello-world into your browser you should get this result:

WEB-INF/content/hello-world.jsp
Hello world!

This illustrates that the Convention plugin will find results even when no action exists and it is all based on the URL passed to Struts.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值