Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1188589
  • 博文数量: 89
  • 博客积分: 10546
  • 博客等级: 上将
  • 技术积分: 1510
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-16 01:24
文章分类

全部博文(89)

文章存档

2012年(7)

2011年(4)

2010年(5)

2009年(52)

2008年(21)

分类: Java

2009-03-14 12:22:23

ActionBean 是 Stripes 的核心,本章介绍如何有效的使用 ActionBean,让 web 开发更加简单。

ActionBean 接口

web.xml 中注册 StripesFilter 时,需要指定一个参数ActionResolver.Packages,其值是 ActionBean 所在的包名,你可以使用指定多个包,中间用逗号隔开。


ActionResolver.Packages
tutorial.action

Stripes 会扫描参数值(这里是tutorial.action)中包中的 ActionBean 类。

可能你已经注意,你的 HelloActionBean 实现了 ActionBean 接口,这是必须的。

public class HelloActionBean implements ActionBean{
...
}

让我们再来看一下 ActionBean 接口定义。

public interface ActionBean {
public ActionBeanContext getContext();
public void setContext(ActionBeanContext context);
}

看起来很简单,仅仅提供了 gettersetter 两个方法。在运行环境时,Stripes 会通过 setter 方法注入一个 ActionBeanContext 实例,你可以通过 getter 方法来取得一个ActionBeanContext 对象。通过 ActionBeanContext 你可以访问 HttpServletRequestHttpServletResponseServletContext 对象。这里就可以看出,为了简化开发,一般情况下,Stripes 将它们隐藏起来,同时也提供了简单的方法,在需要的时候访问它们。

在实际开发中,可以用一个公用类来访问来实现 ActionBean 接口,其它具体的 ActionBean 这个基类继承。

public abstract class BaseActionBean implements ActionBean {
private ActionBeanContext context;

public ActionBeanContext getContext() {
return context;
}
public void setContext(ActionBeanContext context) {
this.context = context;
}
}

处理请求事件

Stripes 是一个基于 action 框架,但它也包含了一些基于事件的框架的概念。在 helloworld 实例中,页面中一个 FORM 是由一个 ActionBean 负责进行处理,提交时会触发一个事件,submit 按钮的名称(sayHello)映射到 ActionBean 的某个方法,这个方法称为 event handler。

下面是一个 Event Handler 的定义。

public Resolution sayHello(){
return new ForwardResolution("/greeting.jsp");
}

它是一个无参数的方法,返回一个 Resolution

默认情况下,submit 按钮的 name 属性值就是相应的 ActionBean 的方法名。你可以在方法使用 Annotation HanlesEvent 来指定 ActionBean 的方法来处理那些事件。

@HandlesEvent("sayHello")
public Resolution greeting(){
return new ForwardResolution("/greeting.jsp");
}

在上面的代码中,用 greeting 方法来处理 sayHello 事件请求。

另外一个可能会用到 Annotation 是 DefaultHandler,当请求中没有提供相应的事件名称时,会执行带有 DefaultHandler Annotation 的方法。如果 ActionBean 只一个事件处理方法,那么这个方法就是默认的。

URL 映射

可能你已经注意到,在 helloworld 的 index.jsp,在 stripes:form 使用了一个 beanclass="tutorial.action.HelloActionBean" 属性,在运行时,它自动为你生成了一个非常友好的 URL

Stripes 提供一套有效的机制,根据 ActionBean 类名和 包名生成 URL 地址。它会将ActionBean 的包名转换成路径添加到 URL中,再添加 ActionBean 类名,最后加一个后缀 .action。你可以会产生疑问,觉得 hellworld中生成的 URL 不符合这一规则。在URL处理时,为了使用URL 看起来更加简洁,Stripes 作了以下处理。

  • 将包名转换成路径,但是忽略包中有'web', 'www', 'stripes' 和 'action' 子包之前的部分,只截取其后的部分。
  • URL 中添加类名,如果类名是以 ActionBean 或者是 ActionBean 结尾,去掉这些后缀。
  • 最后添加一个 .action 后缀,与 web.xml 中定义一致。

现在你不会觉得奇怪了,tutorial.action.HelloActionBean的蝶变过程如下。

  • tutorial.action.HelloActionBean
  • /tutorial/Hello
  • /tutorial/Hello.cation

这里我们忽略了前面的Servlet Context Path("/helloworld")。

如果你不愿意使用它生成的URL ,可以自定义 URL,在 ActionBean 类上使用 Annotation UrlBinding,在参数中指定你的自定义的 URL

@UrlBinding("/SayHello")
public class HelloActionBean implements ActionBean{
...
}

重新运行这个程序,你可以发现生成的文件,已经替换成你的 URL

阅读(2383) | 评论(4) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-12-19 21:06:58

url后缀定义是最基本的servlet知识。 Stripes 问题可以查阅官方文档,你也可获取所有stripes系列的源代码。

chinaunix网友2009-12-18 16:27:31

哈,又弄出来了,可以用.do 没问题了.

chinaunix网友2009-12-18 16:00:47

哈,自己瞎弄八弄,总算被我折腾出来了, 写成@UrlBinding("/SayHello"),就会出错, 写成@UrlBinding("/SayHello.action"),就好了。 但还是不太明白其中的基理。 能自己改后缀名吗?比如想Struts那样的,/SayHello.do ? 还是只能是.action的?

chinaunix网友2009-12-18 15:47:02

白先生你好!   首先感谢你的文章,我在学习Stripes。   在阅读和尝试中,关于UrlBinding的使用,我碰到问题了。   如果类前不加@UrlBinding("/SayHello"),程序可以正常跑起来,但加了之后,产生Http status 404的错误,这个怎么处理?