LoginForm代码:

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.zh.struts.form; import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage; /**
* MyEclipse Struts
* Creation date: 11-27-2013
*
* XDoclet definition:
* @struts.form name="loginForm"
*/fff
public class LoginForm extends ActionForm {
/*
* Generated Methods
*/ private String userName;
private String passWord;
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
System.out.println("valudate go on");
ActionErrors error=new ActionErrors();
if(userName==null || userName.equals("")){
error.add( "userName",new ActionMessage( "userName")); //错误信息添加到ActionErrors中 //new ActionMessage( "userName") 是在 ApplicationResources_zh_CN.properties 中取出你设置的对应的错误信息 //name value 形式中的name
}
return error;
} /**
* Method reset
* @param mapping
* @param request
*/
public String getUserName() {
return userName;
} public String getPassWord() {
return passWord;
} public void setUserName(String userName) {
this.userName = userName;
} public void setPassWord(String passWord) {
this.passWord = passWord;
}
} 在com.xx.struts目录下的
ApplicationResources_zh_CN.properties文件设置: userName=erro2 sttuts-config文件设置: <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config>
<form-beans >
<form-bean name="loginForm" type="com.zh.struts.form.LoginForm" /> </form-beans> <global-exceptions />
<global-forwards />
<!-- validate="true 是设置在form中是否使用 validate方法 -->
<action-mappings >
<action
validate="true" //注意 这里的validate是必须要设置的 这是对应你的LoginForm中 的validate方法中否执行
attribute="loginForm"
input="/WEB-INF/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="com.zh.struts.action.LoginAction"
cancellable="true" /> </action-mappings> <message-resources parameter="com.zh.struts.ApplicationResources" />
</struts-config> jsp代码: <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<%
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 'login.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>
<h1>&nbsp;登录</h1>
<form action="/strutsTest1/login.do" method="post">
u:<input type="text" name="userName"/>&nbsp; &nbsp; <html:errors property="userName"/> <br/> //这里是你请求失败后返回的信息 property对应 LoginForm 中的error.add( "userName",new ActionMessage( "userName")); add方法第一个参数
p:<input type="password" name="passWord"/><br/>
<input type="submit"/>
</form> </body>
</html> 有讲这个内容更好的资源 http://www.cnblogs.com/eflylab/archive/2007/01/08/614465.html

(Struts)ActionForm类及表单数据验证的更多相关文章

  1. struts1:(Struts)ActionForm类及表单数据验证

    在Struts的中央控制器中写了Struts的控制器角色,在这篇介绍下Struts的视图!Struts的视图组件:Struts框架中的视图组件主要包括:JSP页面.ActionForm类.Struts ...

  2. php表单数据验证类

    非常好用方便的表单数据验证类 <?php //验证类 class Fun{ function isEmpty($val) { if (!is_string($val)) return false ...

  3. SpringMVC4+thymeleaf3的一个简单实例(篇四:form表单数据验证)

    关于表单数据验证有很多中方法,这里我仅介绍JSR303注解验证.JSR303仅仅是一个规范,这里我们要用到它的一个实现:hibernate-validator. 注意在spring的配置文件sprin ...

  4. 表单数据验证方法(二)——ASP.NET后台验证

    昨天写了一下关于如何在前台快捷实现表单数据验证的方法,今天接着昨天的,把后台实现数据验证的方法记录一下.先说明一下哈,我用的是asp.net,所以后台验证方法也是基于.net mvc来做的. 好了,闲 ...

  5. Spring学习笔记2——表单数据验证、文件上传

    在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...

  6. jquery表单数据验证扩展方法

    /** 表单数据验证 **/ $.fn.Validform = function () { var Validatemsg = ""; var Validateflag = tru ...

  7. PHP表单数据验证

    背景: 在上次项目的时候,一直不明白为什么要对数据验证,我能保证我每次请求的数据都是合法的,但是在后面的时候,原来“用户”并不是那样听话,他总是要给我们找麻烦,然后可能让我们的服务器崩掉.但是只对单个 ...

  8. 表单数据验证方法(一)—— 使用validate.js实现表单数据验证

    摘要:使用validate.js在前端实现表单数据提交前的验证 好久没写博客了,真的是罪过,以后不能这样了,只学习不思考,学的都是白搭,希望在博客园能记录下自己学习的点滴,虽然记录的都是些浅显的技术, ...

  9. Angular 开发小妙招1:提交表单数据验证不通过,更改输入组件的样式

    开发表单时,客户端数据完整性校验是必不可少的,在jquery 时代出现了无数的数据验证插件也很好用,开发Angular 应用时,angular 内置了一些常用的数据验证指令.今天要讲的不是这些指令如何 ...

随机推荐

  1. CUDA入门1

      1GPUs can handle thousands of concurrent threads. 2The pieces of code running on the gpu are calle ...

  2. Java向上转型与向下转型

    一.向上转型 例如:Parent p=new Son(); 这样引用p只能调用子类中重载父类的方法:但属性是父类的:如果想调用子类属性的话,可以用getter()方法. 二.向下转型 子类对象的父类引 ...

  3. 广搜+输出路径 POJ 3414 Pots

    POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   ...

  4. HDU 4819 Mosaic --二维线段树(树套树)

    题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...

  5. org.springframework.web.context.ContextLoaderListen 报错解决办法

    今天搭建SSH项目的时候出现了如下错误: 严重: Error configuring application listener of class org.springframework.web.con ...

  6. 集合框架学习笔记<二>

    1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...

  7. FlashInspector 【Firefox浏览器插件,flash分析工具】

    Inspect flash(swf)'s DisplayObject with mouse. Overview the swf's DisplayObject list. Set the inspec ...

  8. Linux安装telnet

    安装环境:CentOS 6.4    上篇已经讲述了memcached的安装,现在要测试Memcached功能的时候,需要使用到telnet服务.于是就有了本篇.   一.安装telnet 1.检测t ...

  9. rm 删除带空格的文件或者目录

    1. 信息来源 from man rm, 绝对真实有效 比较删除一个 windows盘符下的 “Program Files“ 目录, 直接rm Program Files 会直接报错, 需要修改为如下 ...

  10. smarty3缓存设置