//UserAction.java
package web.struts.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.commons.beanutils.DynaBean;
public class UserAction extends DispatchAction {
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaBean vform=(DynaBean)form;
String username = (String) vform.get("username");
String password = (String) vform.get("password");
request.setAttribute("username",username);
request.setAttribute("password",password);
return mapping.findForward("result");
}
}
//MyLazyValidateForm.java
package web.struts.forms;
import org.apache.struts.validator.LazyValidatorForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
public class MyLazyValidateForm extends LazyValidatorForm {
//重写父类的validate方法 只有method方法值为save时才调用父类的validate方法
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){
String method = request.getParameter("method");
if(method.equals("login")){
return super.validate(mapping,request);
}
return null;
}
}
//com.yourcompany.struts.ApplicationResources'
# Resources for parameter 'com.yourcompany.struts.ApplicationResources'
# Project P/Feitian
userForm.username=username
userForm.password=password
errors.required={0} \u4e0d\u80fd\u4e3a\u7a7a.
errors.maxlength={0} \u957f\u5ea6\u4e0d\u80fd\u5927\u4e8e{1}\u4e2a\u5b57\u7b26.
//index.jsp
<%@ page language="java" 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"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<html:html lang="true">
<head>
</head>
<body>
<table>
<html:form action="/login.do?method=login" onsubmit="return validateUserForm(this)">
<tr>
<td width="30%">
用户名:
</td>
<td>
<html:text name="userForm" property="username" />
<font color=red><html:errors property="username"/></font>
</td>
</tr>
<tr>
<td width="30%">
密码:
</td>
<td>
<html:text name="userForm" property="password" />
<font color=red><html:errors property="password"/></font>
</td>
</tr>
<tr>
<td>
<div