用Stripes做Java Web开发

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入 另外还有一个Integer类型的变量age,是Person对象的一个属性。Stripes首先试图对request中命为person.age的parameter转换为Integer类型,并将其捆绑到Person对象上。在Person对象的age变量被付值以后,Stripes将验证该Integer值是否小于13。如果用户输入了一个字符串而非整数,用户得到这个消息:The value (Mark) entered in field Person Age must be a valid number. 若是用户输入了一个小于13的整数,用户将看到这个消息:The minimum allowed value for Age is 13. 同样地,我们没有必要为这些错误消息提供任何外部的配置文件。Annotation提供的验证与你的变量在同一个位置上,使得程序员定位验证、理解验证的内容、和对验证进行维护变动更容易。 这个Stripes动作还有两个可被激活的方法(称为事件)。事件是ActionBean类中有如下特征的方法:public Resolution eventName 请注意index方法被标注为@DefaultHandler annotation。因为在本动作中有多个事件,其中一个必须被指定为默认事件。如果调用本动作的URL没有指定哪个事件,Stripes则查找标注有@DefaultHandler annotation的事件,并执行。 显示层(View) 现在让我们给Hello World例程加上显示层的逻辑。Stripes默认支持JSP为显示层的标准技术,不过你也可以用其他的显示层技术,比如FreeMaker。除了Stripes的tag库以外,没有什么新的东西要学。Hello.jsp是初始的显示,可以让用户输入和提交姓名。

<%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld" %>......<stripes:errors/><stripes:form beanclass="com. myco. web. stripes. action. example. HelloWorldAction">Say hello to: <br>First name: <stripes:text name="person.firstName"/><br>Age:<stripes:text name="person.age"/><br><stripes:submit name="hello" value="Say Hello"/></stripes:form>......

这个JSP易读易维护。而Stripes用于form和input的tag跟对应的HTML代码非常相似。stripes:form tag包含一个beanclass属性,其值为我们前面定义的controller类的完整类名。我们可以用stripes:form中的action属性来替换beanclass属性,但是beanclass属性可以让你在以后对Stripes动作进行重构的时候更加方便。如果你要用在stripes:form tag中使用action属性,方法如下:

<stripes:form action="/example/HelloWorld.action">

有一个stripes:input tag指定了一个名为person.firstName属性,其作用是将其储存的输入值付给controller的Person对象的firstName变量中。最后,stripes:submit tag指定一个name属性来告诉Stripes的HelloWorldAction类使用哪一个事件。我们现在已经完成了提交姓名的值给HelloWorldAction,剩下的就是在另一个view中将其反馈给用户了。

<%@ taglib prefix="stripes"  uri="http://stripes.sourceforge.net/stripes.tld" %>......<stripes:errors/><h2>Hello ${actionBean.person.firstName} your age is ${actionBean.person.age} </h2><p/><stripes:link beanclass="com.myco.web.stripes.action. example.HelloWorldAction">Say Hello Again</stripes:link>......

本JSP将自己通过一个对动作的引用读取person的姓名信息并显示。为达到这一目的,Stripes自动在request的属性中添加一个名为actionBean动作对象,以供JSTL存取。最后,我们用了一个strips:link tag来建立一个返回HelloWorldAction地链接从而可以让我们输入不同的姓名。我们以可以通过如下办法显式地创建一个指向index事件的stripes:link:

<stripes:link beanclass="com.myco.web.stripes.action. example.HelloWorldAction" event="index">Say Hello Again</stripes:link>

因为我们已经用annotation把index方法标记为@DefaultHandler,Stripes无须event属性也知道要执行哪一个方法。

[1][2][3][4]

有人要进来,有一些人不得不离开。

用Stripes做Java Web开发

相关文章:

你感兴趣的文章:

标签云: