环境:
Windows2003
jdk1.5.05
Eclipse3.1+MyEclipse4.03
做一登录练习。
现已经完成了login的struts部份,实现了动态ActionForm和动态validator验证,详细全部文件内容如下:
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="password" type="java.lang.String" />
<form-property name="username" type="java.lang.String" />
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards >
<forward name="indexGo" path="/index.jsp" />
</global-forwards>
<action-mappings >
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
validate="true"
type="com.test.struts.action.LoginAction" />
</action-mappings>
<message-resources parameter="com.test.struts.ApplicationResources" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>
</struts-config>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for loginForm form</title>
</head>
<body>
<html:form action="/login">
username : <html:text property="username"/><html:errors property="username"/><br/>
password : <html:password property="password"/><html:errors property="password"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
index.jsp
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<a href="login.jsp">Login</a>
</body>
</html>
LoginAction.java
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package com.test.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
/**
* MyEclipse Struts
* Creation date: 10-27-2005
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="login.jsp" scope="request" validate="true"
*/
public class LoginAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DynaValidatorForm loginForm = (DynaValidatorForm) form;
// TODO Auto-generated method stub
String username=(String)loginForm.get("username");
String password=(String)loginForm.get("password");
loginForm.set("password",null);
if(username.equals("sonic")||password.equals("sonic")){
System.out.println("AAA");
return mapping.findForward("indexGo");
}
else {
System.out.println("BBB");
return mapping.getInputForward();
}
}
}
ApplicationResources.properties
prompt.username=User Name
prompt.password=User Password
errors.required={0} is required.
ApplicationResources_zh_CN.properties
prompt.username=用户名称
prompt.password=登录密码
errors.required={0} 是必须的.
validation.xml
<form-validation>
<formset>
<form name="loginForm">
<field property="username" depends="required">
<arg0 key="prompt.username" />
</field>
<field property="password" depends="required">
<arg0 key="prompt.password" />
</field>
</form>
</formset>
</form-validation>
到此已经完成了全部代码!