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> 登录</h1>
<form action="/strutsTest1/login.do" method="post">
u:<input type="text" name="userName"/> <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