the5fire

关注Python、Django、Vim、Linux、Web开发、团队管理和互联网--Life is short, we need Python.


Struts2之Validation的简单使用实例

作者:the5fire | 标签:     | 发布:2011-01-14 10:23 p.m. | 阅读量: 10806, 10510

本来不打算写这些细枝末节的东西了,因为在文档中都有现成的实例,写了也只能自己用来参考,不过这个Validation文档中给的不全,从网上查之后才知道用法。 首先建立一个QuizAction.java文件:

package com.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class QuizAction extends ActionSupport {

    private static final long serialVersionUID = -7505437345373234225L;

    String name;
    int age;
    String answer;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }
}

相应的需要建立QuizAction-validation.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
    <field name="name">
        <field-validator type="requiredstring">
            <message>You must enter a name</message>
        </field-validator>
    </field>
    <field name="age">
        <field-validator type="int">
            <param name="min">13</param>
            <param name="max">19</param>
            <message>Only people ages 13 to 19 may take this quiz</message>
        </field-validator>
    </field>
</validators>

接着配置struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
    <constant name="struts.devMode" value="true"></constant>
    <package name="front" namespace="/" extends="struts-default">
        <action name="index" class="com.struts2.QuizAction">
            <result>/success.jsp</result>
            <result name="error">/error.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
    </package>
</struts>

然后建立三个页面index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
  <s:form action="index" namespace="/"  method="post">
        <s:textfield label="Name" name="name"/>
        <s:textfield label="Age" name="age"/>
        <s:textfield label="Favorite color" name="answer"/>
        <s:submit/>
  </s:form>
  </body>
</html>

error.jsp:body标签中写上error success.jsp:body标签中写success

然后浏览器输入url访问index.jsp页面。填入数据,如果填入数据和validation配置文件中定义的不同,则出现错误就会提示在文件中配置的错误信息。

- from the5fire.com
----EOF-----

微信公众号:Python程序员杂谈


其他分类: