三大框架(ssh)学习——struts深入学习(手动数据校验)

手动数据校验

我们可以手动进行数据校验,这种方式更加灵活。通过封装后,代码也比较简单。

增加reg2.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>测试struts2</title>

  </head>

  <body>

   <s:form action="testValidate2!register" name="regFrm" method="get" >

   <s:textfield label="用户名" name="uname"></s:textfield>

   <s:password label="密码" name="pwd" ></s:password>

   <s:textfield label="邮箱" name="email"></s:textfield>

   <s:submit  value="注册" ></s:submit>

   </s:form>

  </body>

</html>

TestValidate2Action的代码如下:

package com.bjsxt.test;

 

 

import java.util.regex.Pattern;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class TestValidate2Action extends ActionSupport/*需要继承从而支持校验*/{

 

private String uname;

private String pwd;

private String email;

 

public void validateRegister(){

System.out.println("TestValidate2Action.validateRegister()");

if(uname==null||"".equals(uname.trim())){

this.addFieldError("uname", "用户名不能为空!");

}else if(!Pattern.matches(".{5,10}", uname.trim())){

this.addFieldError("uname", "用户名长度应该在5-10之间!");

}else if(!Pattern.matches("\\w{5,10}", uname.trim())){

this.addFieldError("uname", "用户名应该是字母、数字、下划线!");

}

}

 

public String register() throws Exception {

System.out.println("TestValidate2Action.register()");

return SUCCESS;

}

 

 

public String getUname() {

return uname;

}

 

public void setUname(String uname) {

this.uname = uname;

}

 

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;

}

 

 

}

 

Struts.xml中的配置如下:

<action name="testValidate2" class="com.bjsxt.test.TestValidate2Action">

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

            <result name="input">/reg2.jsp</result>

        </action>


自学内容:

1.校验器短路(  short-circuit="true"  )

2.ajax验证

3.改变struts2错误消息样式(修改模板文件)

修改FreeMarker模板,将错误消息显示到表单域的右方并且以红色字体显示。具体做法如下:

  1. 解压struts2-core-2.3.1.jar
  2. 找到:struts2-core-2.3.1\template\xhtml\controlheader-core.ftlcontrolfooter.ftl
  3. controlheader-core.ftl内容修改如下:

<#--

/*

 * $Id: controlfooter.ftl 590812 2007-10-31 20:32:54Z apetrelli $

 *

 * 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.

 */

-->

<#assign hasFieldErrors = parameters.name?exists && fieldErrors?exists && fieldErrors[parameters.name]?exists/>

${parameters.after?if_exists}<#t/>

    </td><#lt/>

<#if hasFieldErrors>

<#list fieldErrors[parameters.name] as error>

 

<#if parameters.labelposition?default("") == 'top'>

    <td align="left" valign="top"><#rt/>

<#else>

    <td align="center" valign="top" colspan="3"><#rt/>

</#if>

        <span class="errorMessage" style="color:red;">${error?html}</span><#t/>

    </td><#lt/>

 

</#list>

</#if>

</tr>

4.将controlfooter.ftl内容修改如下:

<#--

/*

 * $Id: controlfooter.ftl 590812 2007-10-31 20:32:54Z apetrelli $

 *

 * 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.

 */

-->

<#assign hasFieldErrors = parameters.name?exists && fieldErrors?exists && fieldErrors[parameters.name]?exists/>

${parameters.after?if_exists}<#t/>

    </td><#lt/>

<#if hasFieldErrors>

<#list fieldErrors[parameters.name] as error>

 

<#if parameters.labelposition?default("") == 'top'>

    <td align="left" valign="top"><#rt/>

<#else>

    <td align="center" valign="top" colspan="3"><#rt/>

</#if>

        <span class="errorMessage" style="color:red;">${error?html}</span><#t/>

    </td><#lt/>

 

</#list>

</#if>

</tr>

5.重新打包到struts2-core-2.3.1.jar。即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值