|
真爱的事业和真正的爱情一生只有一次,都值得我们温柔地相待,因为那种感觉是永远都无法复制的, 这世界真正属于你的东西其实并不多,你不好好珍惜,它便会离你而去,包括机遇,包括爱情,包括生命。 不要找任何理由, 当幸福在你身边的时候就抓住它,你就一定会很幸福! |
时 间 记 忆 |
« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
|
blog名称:玻璃杯中的花生壳 日志总数:162 评论数量:249 留言数量:1 访问次数:828287 建立时间:2004年11月4日 |
 | | |
|
|
第四部分: 增加校验和列表页面 - 增加Person对象的校验逻辑保证firstName和lastName是非空字段并且加入列表页面显示数据库中所有的person纪录。
看这个部分的指南参考上一部分的指南:创建Webwork 框架的 Actions和JSP
第四部分: 增加校验和列表页面 - 增加Person对象的校验逻辑保证firstName和lastName是非空字段并且加入列表页面显示数据库中所有的person纪录。
看这个部分的指南参考上一部分的指南:创建Webwork 框架的 Actions和JSP
内容提要
本文将展示如何使用Webwork的校验框架加入校验逻辑。并且将使用Display标签库(Tag Library)创建列表页面显示数据库中的所有Person。
内容列表
[1] 使用校验规则创建Person-validation.xml文件 [2] 察看加入了校验的JSP页面并且进行测试 [3] 在DAO、ManagerTest类中加入testGetPeople方法声明年 [4] 在PersonDAO和Manager类中增加 getPeople 方法 [5] 在Action Test中加入testSearch方法 [6] 在Action中加入search方法 [7] 创建personList.jsp和Canoo test [8] 在菜单中加入链接
一、使用校验规则创建Person-validation.xml文件
为了利用WebWork校验框架实现数据校验有两件事情要做,第一是创建一个validation.xml文件,第二是在需要进行校验的action中加入一个校验interceptor引用。
WebWork允许两种类型的校验 —— per-action和model-based。因为所有的Action对Person引用都要使用相同的校验规则,所以本文将使用model-based类型的校验。
在src/dao/**/model目录下创建Person-validation.xml文件并加入下列内容:
500)this.width=500'><!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" 500)this.width=500'> "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> 500)this.width=500'><validators> 500)this.width=500'> <field name="person.firstName"> 500)this.width=500'> <field-validator type="requiredstring"> 500)this.width=500'> <message key="errors.required"/> 500)this.width=500'> </field-validator> 500)this.width=500'> </field> 500)this.width=500'> <field name="person.lastName"> 500)this.width=500'> <field-validator type="requiredstring"> 500)this.width=500'> <message key="errors.required"/> 500)this.width=500'> </field-validator> 500)this.width=500'> </field> 500)this.width=500'></validators>
在ApplicationResources_*.properties文件中的"errors.message" 键值使用字段的"name"属性以实现国际化。如果不需要提供对i18n的支持可以直接对<message>元素中指定显示内容。
500)this.width=500'>errors.required=${getText(fieldName)} is a required field.
现在可以配置PersonAction使用visitor validation。为了实现这个目标,在PersonAction目录下创建一个PersonAction-validation.xml文件。加入下面的内容:
500)this.width=500'><!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" 500)this.width=500'> "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> 500)this.width=500'><validators> 500)this.width=500'> <field name="person"> 500)this.width=500'> <field-validator type="visitor"> 500)this.width=500'> <param name="appendPrefix">false</param> 500)this.width=500'> <message/> 500)this.width=500'> </field-validator> 500)this.width=500'> </field> 500)this.width=500'></validators>
糟糕的是,WebWork没有提供一个透明机制读取Person-validation.xml文件并且标记在UI上标记哪个字段时必须的。 AppFuse的Struts和Spring版本使用LabelTag实现了这个目标,不过他们也只是实现了一个普通的校验。我希望有人能够为 WebWork提供相同的功能实现。同时JSP tags "required" 属性实际上没有对你所指定的校验规则作任何事情,仅仅是在加入对应的字段后面加入了一个星号而已。
当然,也可以使用per-action校验。只需要拷贝Person-validation.xml文件到"webapp.action"包中并且把它重命名为PersonAction-validation.xml。
为了使在"savePerson" 操作中我们新加入的校验规则发挥作用,我们要把原来在"validator"属性上的注释去掉。确定最后在web/WEB- INF/classes/xwork.xml文件的"savePerson" <action> 部分包含以下内容:
500)this.width=500'><interceptor-ref name="validationStack"/>
说明:在Appfuse中使用的validationStack和WebWork自带的有些不同,更多的信息可以在WebWork's JIRA中查找。
二、查看加入了校验的JSP并进行测试
现在保存所有的文件。为了测试加入了校验后的JSP,运行 ant db-load deploy,启动Tomcat并且在浏览器中输入 http://localhost:8080/myApp/editPerson.html?id=1。
如果擦掉了firstName或者lastName字段的值并点击save按钮,你将看到错误提示信息(图片略):
三、在DAO和Manager Test中加入testGetPeople方法
为了创建一个List页面(或者说是master页面),我们需要穿件一个方法返回person表中的所有行。我们首先在PersonDAOTest和 PersonManagerTest类中创建测试方法。通常把这个方法命名为getEntities (例如getUsers),你也可以使用 getAll 或者 search —— 这其实是同一类问题。
打开test/dao/**/dao/PersonDAOTest.java文件加入testGetPeople方法:
500)this.width=500'>500)this.width=500'>public void testGetPeople() 500)this.width=500'>{ 500)this.width=500'> person = new Person(); 500)this.width=500'> List results = dao.getPeople(person); 500)this.width=500'> assertTrue(results.size() > 0); 500)this.width=500'> }
我在getPeople方法中传入一个person对象是想在以后方便加入过滤(filtering)处理 (基于person对象中的属性值)。在getPeople()方法中应该说明这个参数是可选的。
现在打来test/service/**/service/PersonManagerTest.java文件加入testGetPeople方法。
500)this.width=500'>500)this.width=500'>public void testGetPeople() 500)this.width=500'>{ 500)this.width=500'> List results = mgr.getPeople(new Person()); 500)this.width=500'> assertTrue(results.size() > 0); 500)this.width=500'> }
为了这两个类能够通过编译,需要在PersonDAO和PersonManager接口中加入getPeople()方法并且加入实现。
四、在DAO和Manager加入getPeople()方法
打开src/dao/**/dao/PersonDAO.java文件并且加入getPeople()方法说明:
500)this.width=500'>public List getPeople(Person person);
现在在src/service/**/service/PersonManager.java文件中加入同样的方法。保存所有的文件并且在tests类中调整imports类。接下来在实现类中实现getPeople()方法。打开 src/dao/**/dao/hibernate/PersonDAOHibernate.java文件加入下面的代码:
500)this.width=500'>500)this.width=500'>public List getPeople(Person person) 500)this.width=500'>{ 500)this.width=500'> return getHibernateTemplate().find("from Person"); 500)this.width=500'> }
你可以注意到现在没有对person 参数作任何处理。仅仅是占了个位置 —— 在以后你可以依靠它的属性值使用Hibernate's查询语言或者Criteria Queries加入filter。
一个使用Criteria Query的示例:
500)this.width=500'> Example example = Example.create(person) 500)this.width=500'> .excludeZeroes() // exclude zero valued properties 500)this.width=500'> .ignoreCase(); // perform case insensitive string comparisons 500)this.width=500'>500)this.width=500'> try 500)this.width=500'>{ 500)this.width=500'> return getSession().createCriteria(Person.class) 500)this.width=500'> .add(example) 500)this.width=500'> .list(); 500)this.width=500'>500)this.width=500'> } catch (Exception e) 500)this.width=500'>{ 500)this.width=500'> throw new DataAccessException(e.getMessage()); 500)this.width=500'> } 500)this.width=500'> return new ArrayList();
在src/service/**/impl/PersonManagerImpl.java中实现getPeople() 方法:
500)this.width=500'>500)this.width=500'>public List getPeople(Person person) 500)this.width=500'>{ 500)this.width=500'> return dao.getPeople(person); 500)this.width=500'> }
保存所有的变更,运行下面的测试方法:
500)this.width=500'>ant test-dao -Dtestcase=PersonDAO 500)this.width=500'>ant test-service -Dtestcase=PersonManager
如果一切正常可以在web层加入读取所有人员信息的功能实现了。
五、在Action Test中加入testSearch()方法
打开test/web/**/action/PersonActionTest.java文件加入下面的方法:
500)this.width=500'>500)this.width=500'>public void testSearch() throws Exception 500)this.width=500'>{ 500)this.width=500'> assertNull(action.getPeople()); 500)this.width=500'> assertEquals(action.list(), "success"); 500)this.width=500'> assertNotNull(action.getPeople()); 500)this.width=500'> assertFalse(action.hasActionErrors()); 500)this.width=500'> }
只有在PersonAction中加入getPeople() 和 list() 方法这个类才能通过编译。
六、在Action中加入list()和getPeople()方法
打开src/web/**/action/PersonAction.java 加入list() 方法。在此之前加入"people"变量和getPeople() 方法。
500)this.width=500'>private List people; 500)this.width=500'> 500)this.width=500'>500)this.width=500'> public List getPeople() 500)this.width=500'>{ 500)this.width=500'> return people; 500)this.width=500'> } 500)this.width=500'> 500)this.width=500'>500)this.width=500'> public String list() 500)this.width=500'>{ 500)this.width=500'> people = personManager.getPeople(new Person()); 500)this.width=500'> 500)this.width=500'> return SUCCESS; 500)this.width=500'> }
运行ant test-web -Dtestcase=PersonAction进行测试。
好!
BUILD SUCCESSFUL Total time: 10 seconds
七、创建personList.jsp和Canoo测试
现在在 web/pages目录下应该有了一个personList.jsp文件. If not, you can create it using viewgen. From the command-line, navigate to extras/viewgen and run ant -Dform.name=Person. This will generate a PersonList.jsp in extras/viewgen/build.
打开web/pages目录下的personList.jsp文件进行编辑。
在文件顶部有一个<ww:set>标签用来展示 "people" 信息。你需要把这个引用的值从"persons" 改成 "people"。
500)this.width=500'><ww:set name="personList" value="people" scope="request"/>
用来创建JSP的模版针对id的属性列使用了硬编码,所以XDoclet加了两次。为了在personList.jsp移出它,在文件中删除下面的部分:
500)this.width=500'><display:column property="id" sort="true" headerClass="sortable" 500)this.width=500'> titleKey="personForm.id"/>
如果有人知道办法修改extras/viewgen/src/List_jsp.xdt不再包括这个列的标签,请通知我。
为了前后一致,可以把自动产生的"persons" 改变成"people"。在大约30行的位置,你可以找到下面这一行:
500)this.width=500'><display:setProperty name="paging.banner.items_name" value="persons"/>
修改成:
500)this.width=500'><display:setProperty name="paging.banner.items_name" value="people"/>
最后在to web/WEB-INF/classes/ApplicationResources_en.properties文件中加入title和heading 键值 (personList.title和 personList.heading) :
500)this.width=500'># -- person list page -- 500)this.width=500'>personList.title=Person List 500)this.width=500'>personList.heading=All People
需要注意的是,personList.title将会出现在浏览器的标题栏中,而personList.heading将会显示在页面中作为标题:
在web/WEB-INF/classes/xwork.xml文件中加入一个新的"people" action:
500)this.width=500'> <action name="people" class="personAction" method="list"> 500)this.width=500'> <result name="success"> |
|
| | |
|