目录结构:
struts1的登陆以及验证
login.jsp
<%@ page language="java" pageEncoding="GB18030"%>
<%@ 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" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<title>login.jsp</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>
<center>
<font color="red"><html:errors property="loginerror"/></font>
<html:form action="login.do" method="post">
<table border="0">
<tr>
<td>username:</td>
<td><html:text property="username" /></td>
<td><font color="red"><html:errors property="username" /></font></td>
</tr>
<tr>
<td>Password:</td>
<td><html:password property="password" /></td>
<td><font color="red"><html:errors property="password" /></font></td>
</tr>
<tr>
<td>Age:</td>
<td><html:text property="age" /></td>
<td><font color="red"><html:errors property="age" /></font></td>
</tr>
<tr>
<td>birthday:</td>
<td><html:text property="birthday" /></td>
<td><font color="red"><html:errors property="birthday" /></font></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit value="提交"/></td>
</tr>
</table>
</html:form>
</center>
</body>
</html:html>
loginForm.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.wszf.struts.form;
import java.awt.image.SampleModel;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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: 04-01-2010
*
* XDoclet definition:
* @struts.form name="loginForm"
*/
public class LoginForm extends ActionForm {
/*
* Generated fields
*/
/** username property */
private String username;
/** password property */
private String password;
private String age;
private String birthday;
/*
* Generated Methods
*/
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
ActionErrors error=new ActionErrors();
if(this.username==null||this.username.trim().equals("")){
error.add("username", new ActionMessage("username.null"));
}
if(this.password==null||this.password.trim().equals("")){
error.add("password", new ActionMessage("password.null"));
}
//验证age
int age=Integer.parseInt(this.getAge());
if(age>100 || age<1){
error.add("age", new ActionMessage("age.err"));
}
//验证Date
SimpleDateFormat sf=new SimpleDateFormat("yyyy-mm-dd");
try {
Date da=sf.parse(this.getBirthday());
System.out.println(da);
} catch (ParseException e) {
// TODO Auto-generated catch block
error.add("birthday",new ActionMessage("birthday.error"));
}
return error;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/**
* Returns the username.
* @return String
*/
public String getUsername() {
return username;
}
/**
* Set the username.
* @param username The username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Returns the password.
* @return String
*/
public String getPassword() {
return password;
}
/**
* Set the password.
* @param password The password to set
*/
public void setPassword(String password) {
this.password = password;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
LoginAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.wszf.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.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import com.wszf.struts.form.LoginForm;
/**
* MyEclipse Struts
* Creation date: 04-01-2010
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request" validate="true"
*/
public class LoginAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
if(loginForm.getUsername().equals("wszf")&&loginForm.getPassword().equals("haha")){
request.getSession().setAttribute("name", loginForm.getUsername());
return mapping.findForward("success");
}else{
ActionMessages error=new ActionMessages();
error.add("loginerror", new ActionMessage("login.error"));
this.addErrors(request, error);
return mapping.getInputForward();
}
}
}
ApplicationResources.properties
# Resources for parameter 'com.wszf.struts.ApplicationResources'
# Project login_struts1
username.null=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A\uFF01
password.null=\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A
login.error=\u7528\u6237\u540D\u6216\u5BC6\u7801\u9519\u8BEF
age.err=\u5E74\u9F84\u8981\u57281-100\u4E4B\u95F4\u54DF\uFF01\uFF01
birthday.error=\u65E5\u671F\u683C\u5F0F\u4E0D\u6B63\u786E\u54DF\uFF01\uFF01
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="com.wszf.struts.form.LoginForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="com.wszf.struts.action.LoginAction" >
<forward name="success" path="/success.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.wszf.struts.ApplicationResources" />
</struts-config>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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 'index.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>
${name}登陆成功
</body>
</html>
注意的问题应该是:一般情况下,在loginForm里,int型和Date型要是String型,然后在转化。