码迷,mamicode.com
首页 > 其他好文 > 详细

struts2学习(11)struts2验证框架1.验证简介、内置验证

时间:2017-07-02 10:23:27      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:default   span   exp   mit   soft   submit   sed   表示   insert   

一、Struts2验证简介:

技术分享

二、struts2内置验证:

技术分享

下面例子,需求是:为用户注册进行验证;

技术分享

 

com.cy.model.User.java:

技术分享
package com.cy.model;

public class User {

    private String userName;
    private String name;
    private int age;
    private String email;
    private String homePage;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    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 getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getHomePage() {
        return homePage;
    }
    public void setHomePage(String homePage) {
        this.homePage = homePage;
    }
    @Override
    public String toString() {
        return "User [userName=" + userName + ", name=" + name + ", age=" + age
                + ", email=" + email + ", homePage=" + homePage + "]";
    }
    
    
}
View Code

com.cy.action.RegisterAction.java:

技术分享
package com.cy.action;

import com.cy.model.User;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("传入的用户" + user);
        return SUCCESS;
    }
    
}
View Code

RegisterAction-validation.xml:

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
    <field name="user.userName">
        <field-validator type="requiredstring">
            <message>请输入用户名</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">6</param>
            <param name="maxLength">10</param>
            <message>用户名必须在${minLength}和${maxLength}之间</message>
        </field-validator>
    </field>
    
    <field name="user.name">
        <field-validator type="requiredstring">
            <message>请输入姓名</message>
        </field-validator>
    </field>
    
    <field name="user.age">
        <field-validator type="int">
            <param name="min">18</param>
            <message>年龄必须满18周岁</message>
        </field-validator>
    </field>
    
    <field name="user.email">
        <field-validator type="requiredstring">
            <message>请输入邮件</message>
        </field-validator>
        <field-validator type="email">
            <message>邮件格式不对</message>
        </field-validator>
    </field>
    
    <field name="user.homePage">
        <field-validator type="requiredstring">
            <message>请输入主页</message>
        </field-validator>
        <field-validator type="url">
            <message>主页格式不对</message>
        </field-validator>
    </field>
    
    <!-- 
        <![CDATA[xxxx]]>表示忽略里面的一些特殊的转义字符
     -->
    <validator type="expression">
        <param name="expression"><![CDATA[!user.name.equals(user.userName)]]></param>
        <message>用户名和真实姓名不能相同</message>
    </validator>
    
</validators>
View Code

struts.xml配置:

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

<struts>
    
    <package name="manage" extends="struts-default">
        <action name="registerAction" class="com.cy.action.RegisterAction">
            <!-- 如果验证失败的话,返回到register.jsp注册页面 -->
            <result name="input">register.jsp</result>
            <result name="success">success.jsp</result>
        </action>
    </package>
    
</struts>

register.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <!-- 这个作用就是表达式验证失败的错误信息,显示在这里。 -->
<s:actionerror/> <s:form action="/registerAction" method="post"> <s:textfield name="user.userName" label="用户名"></s:textfield> <s:textfield name="user.name" label="真实姓名"></s:textfield> <s:textfield name="user.age" label="年龄"></s:textfield> <s:textfield name="user.email" label="邮件"></s:textfield> <s:textfield name="user.homePage" label="主页"></s:textfield> <s:submit value="注册"></s:submit> </s:form> </body> </html>

success.jsp:

技术分享
<body>
注册成功!
</body>
View Code

 

测试:

注册填写失败,提示信息:

技术分享技术分享

注册成功,console打印:

技术分享

 

struts2学习(11)struts2验证框架1.验证简介、内置验证

标签:default   span   exp   mit   soft   submit   sed   表示   insert   

原文地址:http://www.cnblogs.com/tenWood/p/7103894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!