Posted on 2008-08-26 02:23
∪∩BUG 阅读(948)
评论(1) 编辑 收藏 所属分类:
Struts2学习笔记
项目树形图
src/struts.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5<struts>
6 <include file="struts-default.xml" /><!-- 使用缺省的struts的配置文件 -->
7
8 <!-- 包空间 ConverterDemo 继承 struts-default -->
9 <package name="ConverterDemo" extends="struts-default">
10
11 <!-- 映射名name="HelloWorld" 与 index.jsp 中的 action="HelloWorld" 对应,使用com.action.LoginAction来实现 -->
12 <action name="HelloWorld" class="com.action.LoginAction">
13 <result>/index.jsp</result>
14 <result name="input">/index.jsp</result>
15 </action>
16
17 <!--
18 1.映射名name="ProductConfirm" 与 submit.jsp 中的 action="ProductConfirm" 对应,使用com.action.ProductConfirm来实现
19 2.成功转到show.jsp页面
20 3.失败转入submit.jsp页面
21 -->
22 <action name="ProductConfirm"
23 class="com.action.ProductConfirm">
24 <result>/show.jsp</result>
25 <result name="input">/submit.jsp</result>
26 </action>
27
28 <!--
29 1.映射名name="ValidationAction" 与 int.jsp 中的 action="ValidationAction" 对应,使用com.action.InputAction来实现
30 2.成功转到output.jsp页面
31 3.失败转入int.jsp页面
32 -->
33 <action name="ValidationAction"
34 class="com.action.InputAction">
35 <result>/output.jsp</result>
36 <result name="input">/int.jsp</result>
37 </action>
38 </package>
39</struts>
40
41
src/com.action.InputAction.java
1package com.action;
2
3import com.opensymphony.xwork2.ActionSupport;
4
5/** *//**
6 * @author ∪∩BUG E-mail: tidelgl@163.com
7 * @version Aug 25, 2008 5:05:10 PM
8 * @表单校验Action
9 */
10public class InputAction extends ActionSupport {
11
12 private String string;
13
14 public String getString() {
15 return string;
16 }
17
18 public void setString(String string) {
19 this.string = string;
20 }
21
22 @Override
23 public String execute() throws Exception {
24
25 return SUCCESS;
26 }
27
28// @Override
29// public void validate() { 非action-validation.xml验证方式用
30// if (this.string.length() <= 0) {
31// this.addFieldError("string", "empty");
32// }
33// }
34
35}
36
src/com.action.InputAction-validation.xml
Struts 2.0校验框架的规范:在相应的Action同级目录下创建名为"Action名-validation.xml"的配置文件
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE validators PUBLIC
3 "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
4 "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
5
6<!--
7 1.InputAction-validation.xml验证对应InputAction.java里的属性,表单信息先通过此验证才过Action
8 2.validators:验证框架,fied表明针对域的验证
9 3.fied name="string":name对应的只能是此Action(InputAction.java)里的属性.
10 4.fied-validator:字段效验器.(这里有两个,可设置多个) type="reqiuredString":属性name="string"的验证规则是type="reqiuredString"
11 5.message:错误提示信息.key="empty"表明从资源文件中读取(直接可以I18n)
12 注意:dtd的版本是xwork-validator-1.0.2.dtd,此例子其他版本会出错!
13-->
14<validators>
15 <field name="string">
16 <field-validator type="requiredstring">
17 <message key="empty"></message>
18 </field-validator>
19 <field-validator type="email">
20 <message>
21 The email address you entered is not valid.
22 </message>
23 </field-validator>
24 </field>
25</validators>
26
WebRoot/int.jsp
1<%@page contentType="text/html; charset=UTF-8"%>
2<%@taglib prefix="s" uri="/struts-tags"%>
3<html>
4 <head>
5 <title>int</title>
6 </head>
7 <body>
8 <!--
9 非action-validation.xml验证方式用
10 <s:fielderror></s:fielderror>
11 -->
12 <s:form action="ValidationAction">
13 <%
14 //name="string"对应InputAction.java中的属性string
15 %>
16 <s:textfield name="string" label="Reqiured String"></s:textfield>
17 <br>
18 <s:submit></s:submit>
19 </s:form>
20 </body>
21</html>
22
WebRoot/output.jsp
1<%@page contentType="text/html; charset=UTF-8"%>
2<%@taglib prefix="s" uri="/struts-tags"%>
3<html>
4 <head>
5 <title>output</title>
6 </head>
7 <body>
8 Reqiured String:
9 <s:property value="string" />
10 </body>
11</html>
12
src/globalMessages_en_US.properties
HelloWorld=HelloWorld
failtip={0}Login failed\!
language=Select language
smg=Now is{0}
succtip={0}Welcome,Login success.
usen=Americal English
zhcn=Simplified Chinese
validation.loc=Locale must be China or USA
empty=input context not empty
src/globalMessages_zh_CN.properties
HelloWorld=\u4F60\u597D
failtip={0}\u767B\u5F55\u5931\u8D25
language=\u9009\u62E9\u8BED\u8A00
smg={0}\u73B0\u5728\u7684\u4E8B\u4EF6\u662F{1}
succtip={0}\u6B22\u8FCE,\u767B\u5F55\u6210\u529F
usen=\u82F1\u8BED
zhcn=\u4E2D\u6587
validation.loc=\u533A\u57DF\u5FC5\u987B\u4E3A\u4E2D\u56FD\u6216\u7F8E\u56FD
empty=\u8F93\u5165\u5185\u5BB9\u4E0D\u80FD\u4E3A\u7A7A