先建立下列文件:
一個ActionForm:LoginReForm
一個Action:LoginReAction
一個xml配置文件:struts-config.xml
三個jsp:register.jsp success.jsp failure.jsp
下面即是這些文件的相關代碼:
LoginReForm
- /*
- * Generated by MyEclipse Struts
- * Template path: templates/java/JavaClass.vtl
- */
- package com.yourcompany.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;
- /**
- * MyEclipse Struts
- * Creation date: 09-11-2007
- *
- * XDoclet definition:
- * @struts.form name="logForm"
- */
- public class LoginReForm extends ActionForm {
- /*
- * Generated Methods
- */
- /**
- * Method validate
- * @param mapping
- * @param request
- * @return ActionErrors
- */
- private String username;
- private String password1;
- private String password2;
- public String getPassword1() {
- return password1;
- }
- public void setPassword1(String password1) {
- this.password1 = password1;
- }
- public String getPassword2() {
- return password2;
- }
- public void setPassword2(String password2) {
- this.password2 = password2;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public ActionErrors validate(ActionMapping mapping,
- HttpServletRequest request) {
- // TODO Auto-generated method stub
- return null;
- }
- /**
- * Method reset
- * @param mapping
- * @param request
- */
- public void reset(ActionMapping mapping, HttpServletRequest request) {
- // TODO Auto-generated method stub
- }
- }
LoginReAction
- /*
- * Generated by MyEclipse Struts
- * Template path: templates/java/JavaClass.vtl
- */
- package com.yourcompany.struts.action;
- import org.apache.struts.action.*;
- import javax.servlet.http.*;
- import java.io.*;
- 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 com.yourcompany.struts.form.LoginReForm;
- /**
- * MyEclipse Struts
- * Creation date: 09-11-2007
- *
- * XDoclet definition:
- * @struts.action validate="true"
- */
- public class LoginReAction 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) {
- // TODO Auto-generated method stub
- LoginReForm rf = (LoginReForm) form;
- String password1 = rf.getPassword1();
- String password2 = rf.getPassword2();
- ActionForward forward = new ActionForward();
- System.out.println("lisabush");
- if (password1.equals(password2)) {
- forward = mapping.findForward("suc");
- }
- else forward = mapping.findForward("fai");
- return (forward);
- }
- }
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="LoginReForm" type="com.yourcompany.struts.form.LoginReForm" />
- </form-beans>
- <global-exceptions />
- <global-forwards />
- <action-mappings >
- <action
- attribute="loginRe"
- input="/register.jsp"
- name="LoginReForm"
- path="/loginRe"
- scope="request"
- type="com.yourcompany.struts.action.LoginReAction">
- <forward name="fai" path="/failure.jsp" />
- <forward name="suc" path="/success.jsp" />
- </action>
- </action-mappings>
- <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
- </struts-config>
register.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>
- <form id="form2" name="input" method="post" action="loginRe.do">
- <label>用戶名:
- <input type="text" name="username" />
- </label>
- <label>密碼:
- <input type="text" name="password1" />
- </label>
- <label>密碼確認:
- <input type="text" name="password2" />
- </label>
- <input type="submit" name="" value="提交" />
- </form>
- </body>
- </html>
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 'success.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>
- success!!! <br>
- </body>
- </html>
failure.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 'failure.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>
- failure!!! <br>
- </body>
- </html>