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

struts标签解决用户输入验证的解决方案

时间:2016-07-14 21:23:35      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

首先新建

User测试类

package com.cx.verify;

/**
 * Created by cxspace on 16-7-14.
 */
public class User {
    private String userName;

    private String pwd;

    private String email;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

接着来写带有登录逻辑的userAction类

package com.cx.verify;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by cxspace on 16-7-14.
 */

/*
  如果要用struts的数据校验功能,必须要继承actionsupport以及实现相关接口

 */

public class UserAction extends ActionSupport{

    //封装请求数据
    private User user;

    public User getUser() {
        return user;
    }

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

 
    public String login(){return SUCCESS;
    }
}

验证实现的xml配置

com/cx/verify/UserAction-validation.xml

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

<!DOCTYPE validators PUBLIC
          "-//Apache Struts//XWork Validator 1.0.3//EN"
          "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">

<validators>

    <!--验证的每一个字段用field表示-->
    <field name="user.userName">

        <!--使用指定的验证器-->
        <field-validator type="requiredstring">

            <!--验证失败的错误提示信息-->
            <message>用户名不能为空!</message>
        </field-validator>
    </field>

    <field name="user.pwd">
        <field-validator type="requiredstring">
            <message>密码不能为空!</message>
        </field-validator>
        <!--长度验证-->
        <field-validator type="stringlength">

            <param name="minLength">6</param>
            <param name="maxLength">10</param>

            <message>密码必须为6-10位</message>
        </field-validator>

    </field>

    <!--
    <field name="user.birth">
        <field-validator type="date">
            <message>日期格式不对</message>
        </field-validator>
    </field>
    -->

   <field name="user.email">
       <field-validator type="email">
           <message>邮箱格式不对!</message>
       </field-validator>
   </field>

</validators>

 

配置Action

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

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="verify" extends="struts-default">
        <action name="user_*" class="com.cx.verify.UserAction" method="{1}">

            <result name="success">/index.jsp</result>

            <result name="input">/login.jsp</result>
        </action>

    </package>

</struts>

login.jsp里面的内容

<%--
  Created by IntelliJ IDEA.
  User: cxspace
  Date: 16-7-14
  Time: 下午3:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>login</title>


    <!--修改struts样式-->
    <style type="text/css">

        ul{
            display:inline;
        }

        ul li{

            display: inline;
            color: brown;
        }

    </style>

</head>
<body>

<s:debug></s:debug>

 <form method="post" action="${pageContext.request.contextPath}/user_login">

    用户名:<input type="text" name="user.userName">
     <s:fielderror fieldName="user.userName"></s:fielderror>
    <br>
    密码:<input type="password" name="user.pwd">
     <s:fielderror fieldName="user.pwd"></s:fielderror>
    <br>
     邮箱:<input type="text" name="user.email" value="">

     <s:fielderror fieldName="user.email"></s:fielderror>
     <br>
    <input type="submit">

 </form>
</body>
</html>

 如果不想在login.jsp里面加样式可修改struts核心包里面的fielderror.ftl如下

然后把它放到工程的/src/template/simple/fielderror.ftl目录下

<#--
/*
 * $Id: fielderror.ftl 805635 2009-08-19 00:18:54Z musachy $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
-->
<#if fieldErrors??><#t/>
<#assign eKeys = fieldErrors.keySet()><#t/>
<#assign eKeysSize = eKeys.size()><#t/>
<#assign doneStartUlTag=false><#t/>
<#assign doneEndUlTag=false><#t/>
<#assign haveMatchedErrorField=false><#t/>
<#if (fieldErrorFieldNames?size > 0) ><#t/>
    <#list fieldErrorFieldNames as fieldErrorFieldName><#t/>
        <#list eKeys as eKey><#t/>
        <#if (eKey = fieldErrorFieldName)><#t/>
            <#assign haveMatchedErrorField=true><#t/>
            <#assign eValue = fieldErrors[fieldErrorFieldName]><#t/>
            <#if (haveMatchedErrorField && (!doneStartUlTag))><#t/>
                   <#assign doneStartUlTag=true><#t/>
            </#if><#t/>
            <#list eValue as eEachValue><#t/>
                ${eEachValue}
            </#list><#t/>            
        </#if><#t/>
        </#list><#t/>
    </#list><#t/>
    <#if (haveMatchedErrorField && (!doneEndUlTag))><#t/>
        <#assign doneEndUlTag=true><#t/>
    </#if><#t/>
<#else><#t/>    
    <#if (eKeysSize > 0)><#t/>
        <ul<#rt/>
<#if parameters.cssClass??>
 class="${parameters.cssClass?html}"<#rt/>
<#else>
 class="errorMessage"<#rt/>
</#if>
<#if parameters.cssStyle??>
 style="${parameters.cssStyle?html}"<#rt/>
</#if>
>
            <#list eKeys as eKey><#t/>
                <#assign eValue = fieldErrors[eKey]><#t/>
                <#list eValue as eEachValue><#t/>
                    <li><span><#if parameters.escape>${eEachValue!?html}<#else>${eEachValue!}</#if></span></li>
                </#list><#t/>
            </#list><#t/>
        </ul>
    </#if><#t/>
</#if><#t/>
</#if><#t/>

 

struts标签解决用户输入验证的解决方案

标签:

原文地址:http://www.cnblogs.com/cxspace/p/5671719.html

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