现在这个验证呢,跟第一篇文章的那个还有所不一样,之前的是配置资源文件和配置验证文件的做法,这次呢是在ActionForm里面的一个validator方法中纯手工写代码的方式,实现的登录验证效果一样
login.jsp
<%@ page pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Struts第一个应用:登录验证</title>
</head>
<body>
<h3>Struts第一个应用:登录验证</h3><hr/>
<ul style="color:red">
<html:messages id="msg">
<li>${msg}</li>
</html:messages>
</ul>
<form id="myForm" name="myForm" action="login.do" method="post">
<table border="1" width="600px">
<tr>
<td>登录名</td>
<td><input type="text" name="loginname"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="pwd"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
LoginForm.java
package com.javacrazyer.web.formbean;
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;
/**
* 用来收集客户端提交数据.
* 要收集数据的属性的名一定要跟请求参数名相同
*
*/
public class LoginForm extends ActionForm {
private static final long serialVersionUID = 6619272689058619128L;
private String loginname;
private String pwd;
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println("*******调用LoginForm的reset方法");
this.pwd = null;
this.loginname = null;
super.reset(mapping, request);
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
System.out.println("调用LoginForm的validate方法------");
//如果本方法返回null或ActionErrors中没有存放任何对象,表示验证通过。否则验证失败,跳转到对应<action>元素中input属性指定的URL
ActionErrors errors = new ActionErrors();
if(this.loginname == null || "".equals(this.loginname)){
ActionMessage msg = new ActionMessage("用户名不能为空",false);
errors.add(ActionErrors.GLOBAL_MESSAGE, msg);
}
if(this.pwd == null || "".equals(this.pwd)){
ActionMessage msg = new ActionMessage("密码不能为空",false);
errors.add(ActionErrors.GLOBAL_MESSAGE, msg);
}
return errors;
}
public LoginForm(){
System.out.println("调用默认构造方法创建出LoginForm的一个实例");
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
System.out.println("调用setLoginname()方法");
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
System.out.println("调用setPwd()方法");
}
}
WEB-INF/struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- ================================================ Form Bean Definitions -->
<form-beans>
<form-bean name="loginForm" type="com.javacrazyer.web.formbean.LoginForm"/>
<form-bean name="regForm" type="com.javacrazyer.web.formbean.RegisterForm"/>
</form-beans>
<!-- =========================================== Action Mapping Definitions -->
<action-mappings>
<action path="/login" name="loginForm" type="com.javacrazyer.web.action.LoginAction" validate="true"
input="/index.jsp">
<forward name="succ" path="/login_success.jsp"/>
<forward name="failure" path="/login_failure.jsp"/>
</action>
</action-mappings>
</struts-config>
这里的示例就不要用到validator.xml和validator-rules.xml了,也不需要资源文件
关于web.xml的配置、LoginAction和其他JSP页面都与第一篇文章中的一样,这里就不写了,实现的效果也就是第一篇文章中所展示的效果
分享到:
相关推荐
Struts Hibernate Spring 集成开发宝典 actionform 有研究或探讨或开源的请加群:37424970 或联系本人MSN或邮箱:zhuseahui@yahoo.com.cn
Validator验证框架 回顾ActionForm的表单验证,掌握Validator验证框架.zip
Struts中不同的Action和ActionForm组合.rar
struts1使用actionform实现表单验证,表单验证失败追加信息。
总体思路:这个验证是没有添加验证框架的验证,而是直接通过ActionForm的validate()方法进行系统验证, 进行验证要处理三个方面的问题: > 配置资源文件:配置ApplicationResources.prop内容,把验证的的内容写道...
主要是actionForm的基本验证,重写validate方法,返回actionErrors,然后根据资源文件显示错误。 这是一个demo例子,直接导入eclispe中即可。 并且这部分内容会在相应的博客中介绍,稍后会更新博客。
struts提供了标签来显示整个ActionErrors的错误信息。但要在struts-config.xml中设置<action ...
struts 视频 struts视频 actionform
struts中的validator验证demo 其中包括actionform的,动态actionform,还有js提示。 其中这部分内容博客中也有讲解,博客提示:http://blog.csdn.net/llhhyy1989/article/details/8590345
Struts_学习笔记之ActionForm,简单配置即可达到效果
最近自学java中的框架-struts写了一些小例子,这都是很经典的程序,如果大家瞧得起要下载去看看,顺便给俺找找不足的地方。我的qq 821865130 email qingtian_hechen@163.com 希望大家能多多给我帮助。在此谢谢各位!...
用org.apache.struts.validator.DynaValidatorActionForm类验证用户名和密码是否为空,并要大于6个字符,如果符号要求就跳到success.jsp页面将名字显示出来,和validator框架验证4.1.txt 相比,无须要actionForm类,...
验证ActionForm存到了request中.rar
该avi很好的讲解动态actionForm,通过练习可以理解它的运行过程
使用struts1框架实现用户登录,那么就需要新建两个类,比如:LoginForm、LoginAction继承ActionForm、Action,配置struts-config.xml配置文件
视频第二部分 下载前请阅读博客文章详细 博文链接:https://dlivan.iteye.com/blog/65870
11.1 在struts中用validator作服务器端验证 14 第12章 Struts HTML标签库 14 12.1 Struts中基本的HTML标签 14 12.1 Struts中表单处理的HTML标签 14 12.1 Struts中复选框和单选框的HTML标签 14 12.1 Struts下JSP页面...
使用Struts做登录验证的步骤: 1 搭建环境: A 导包 B 建配置文件 struts-config.xml C 修改web.xml 2 建一个 ActionForm 3 建一个 Action 4 页面上面的事情
Struts的概念和体系结构 5 Struts的与Web App的关系 5 Struts的体系结构 5 从视图角度(View) 6 从模型角度(Model) 6 从控制器角度(Controller) 6 Struts的基本组件包 6 Struts framework的工作原理和...
3 import org.apache.struts.action.ActionForm; 4 import org.apache.struts.action.ActionMapping; 5 6 /** 7 * MyEclipse Struts 8 * Creation date: 11-10-2005 9 * 10 * XDoclet definition: 11 * @...