zoukankan      html  css  js  c++  java
  • (Struts)ActionForm类及表单数据验证

    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
  • 相关阅读:
    C# 斐波拉契数列
    Visual Studio [即时窗口] & [命令窗口] (Immediate Window & Command Window) 转
    在.NET平台下 有哪些数据持久层框架 (转)
    WebPart 控件之间通讯 笔记
    WebPart的数据库连接问题 转
    C 语言函数要先声明后定义
    C#单例模式的三种写法(转)
    WCF 绑定wshttpbinding
    关于C#中派生类调用基类构造函数的理解 base使用
    ThinkPHP3.* 模型操作相关函数
  • 原文地址:https://www.cnblogs.com/shaoshao/p/3445373.html
Copyright © 2011-2022 走看看