---
title: "Struts 1.3.8 学习笔记（三）"
author: "CoderDream"
date: "2013-07-09T19:21:13Z"
tags: []
categories: ["企业架构"]
slug: "struts-1-3-8"
draft: false
lastmod: "2013-07-12T16:03:42Z"
id: 1902200
url: "https://coderdream.iteye.com/blog/1902200"
---

第三个版本，我们给项目增加校验功能，这里我们提供用户名和密码的非空校验。

 

**实现校验主要有两步：**

1、在Form中增加validate方法；

2、在jsp中显示校验出错的信息；

 

**修改后的代码：**

LoginForm.java

```
package com.coderdream.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;
import org.apache.struts.action.ActionMessages;

public class LoginForm extends ActionForm {
	private String userName;// 用户名
	private String password;// 密码

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		// 错误对象
		ActionErrors error = new ActionErrors();
		// 消息对象
		ActionMessages msg = new ActionMessages();
		// 验证用户名不能为空
		if (userName == null || userName.equals("")) {
			msg.add("userName", new ActionMessage(
					"login.error.username.required"));
		}
		// 验证密码为空
		if (password == null || password.equals("")) {
			msg.add("password", new ActionMessage(
					"login.error.password.required"));
		}
		// 将错误的验证消息添加到错误对象中
		error.add(msg);
		return error;
	}

	public String getUserName() {
		return userName;
	}

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

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
```

 

login.jsp

```
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF">
<title>Login</title>
</head>
<body>
	<html:form action="/login">
		<bean:message key="login.username" />
		<html:text property="userName" />
		<html:errors property="userName"/>
		<br />
		<bean:message key="login.password" />
		<html:text property="password" />
		<html:errors property="password"/>
		<br />
		<html:submit>
			<bean:message key="login.submit" />
		</html:submit>
	</html:form>
</body>
</html>
```

 

application\_zh.properties

```
login.username=\u7528\u6237\u540d
login.password=\u5bc6\u7801
login.submit=\u767b\u9646
login.error.username.required=<font color='red'>\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a</font>
login.error.password.required=<font color='red'>\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a</font>
```

 application\_en.properties

```
login.username=User Name
login.password=Password
login.submit=Submit
login.error.username.required=<font color='red'>username is required</font>
login.error.password.required=<font color='red'>password is required</font>
```

 

**更新说明**

1、LoginForm中覆盖了父类的validate方法，用于校验页面传过来的Form；

2、jsp中增加了&lt;html:errors&gt;标签，用于显示出错信息；

 

**运行结果**

中文环境  
![](http://dl2.iteye.com/upload/attachment/0086/7341/41a0c624-a622-37ba-a5f5-d764359307a9.png)  
英文环境

![](http://dl2.iteye.com/upload/attachment/0086/7339/fbe34274-acdc-35b1-8111-6a40a58d5c2b.png)

 

源代码：（库文件请从版本一下载）