日期:2014-05-20 浏览次数:20824 次
package c1; 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 LoginActionForm extends ActionForm { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { if ("".equals(this.getUsername()) || this.username == null) { ActionErrors ae = new ActionErrors(); ActionMessage am = new ActionMessage("error.eng"); ae.add("username", am); return ae; } else { return super.validate(mapping, request); } } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config SYSTEM "struts-config_1_1.dtd"> <struts-config> <form-beans> <form-bean name="login" type="c1.LoginActionForm"></form-bean> </form-beans> <action-mappings> <action path="/c1/main" name="login" type="c1.LoginAction" input="/c1/test1.jsp" validate="true"> </action> </action-mappings> <message-resources parameter="resource"></message-resources> </struts-config>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="structs-html" uri="/WEB-INF/struts-html.tld"%> <!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-8"> <title>Insert title here</title> </head> <body> <form action="main.do"> username<input type="text"><structs-html:errors property="username"/><br> <input type="submit" value="提交"> </form> </body> </html>