 
UserAction.java
 1
 /**//*
/**//*
 2 * Generated by MyEclipse Struts
 * Generated by MyEclipse Struts
 3 * Template path: templates/java/JavaClass.vtl
 * Template path: templates/java/JavaClass.vtl
 4 */
 */
 5 package com.lucky.struts.action;
package com.lucky.struts.action;
 6
 7 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponse;
 9
10 import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForward;
12 import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMapping;
13 import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessage;
14 import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessages;
15 import org.apache.struts.actions.DispatchAction;
import org.apache.struts.actions.DispatchAction;
16
17 import com.lucky.struts.form.UserForm;
import com.lucky.struts.form.UserForm;
18
19
 /** *//**
/** *//** 
20 * MyEclipse Struts
 * MyEclipse Struts
21 * Creation date: 09-23-2007
 * Creation date: 09-23-2007
22 *
 * 
23 * XDoclet definition:
 * XDoclet definition:
24 * @struts.action path="/user" name="userForm" input="/user.jsp" parameter="method" scope="request" validate="true"
 * @struts.action path="/user" name="userForm" input="/user.jsp" parameter="method" scope="request" validate="true"
25 */
 */
26
 public class UserAction extends DispatchAction
public class UserAction extends DispatchAction  {
{
27 private ActionMessages messages = new ActionMessages();
    private ActionMessages messages = new ActionMessages();
28 public ActionForward toAdd(ActionMapping mapping, ActionForm form,
    public ActionForward toAdd(ActionMapping mapping, ActionForm form,
29
 HttpServletRequest request, HttpServletResponse response)
            HttpServletRequest request, HttpServletResponse response)  {
{
30 UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
        UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
31 String inputPage = mapping.getInput();
        String inputPage = mapping.getInput();
32 this.saveToken(request);
        this.saveToken(request);
33 return mapping.findForward("toAdd");
        return mapping.findForward("toAdd");
34 }
    }
35 
    
36 public ActionForward executeAdd(ActionMapping mapping, ActionForm form,
    public ActionForward executeAdd(ActionMapping mapping, ActionForm form,
37
 HttpServletRequest request, HttpServletResponse response)
            HttpServletRequest request, HttpServletResponse response)  {
{
38 UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
        UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
39 String inputPage = mapping.getInput();
        String inputPage = mapping.getInput();
40
 if (this.isTokenValid(request, true))
        if (this.isTokenValid(request, true))  {
{
41 //没有重复提交
            //没有重复提交
42 System.out.println("新增操作
            System.out.println("新增操作
 .");
.");
43 return mapping.findForward("success");
            return mapping.findForward("success");
44
 }else
        }else {
{
45 messages.clear();
            messages.clear();
46 messages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.token"));
            messages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.token"));
47 request.setAttribute("message", messages);
            request.setAttribute("message", messages);
48 this.resetToken(request);
            this.resetToken(request);
49 return mapping.findForward("error");
            return mapping.findForward("error");
50 }
        }
51 }
    }
52 }
} 
UserForm.java
  1
 /**//*
/**//*
  2 * Generated by MyEclipse Struts
 * Generated by MyEclipse Struts
  3 * Template path: templates/java/JavaClass.vtl
 * Template path: templates/java/JavaClass.vtl
  4 */
 */
  5 package com.lucky.struts.form;
package com.lucky.struts.form;
  6
  7 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
  8 import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionErrors;
  9 import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForm;
 10 import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMapping;
 11
 12
 /** *//**
/** *//** 
 13 * MyEclipse Struts
 * MyEclipse Struts
 14 * Creation date: 09-23-2007
 * Creation date: 09-23-2007
 15 *
 * 
 16 * XDoclet definition:
 * XDoclet definition:
 17 * @struts.form name="userForm"
 * @struts.form name="userForm"
 18 */
 */
 19
 public class UserForm extends ActionForm
public class UserForm extends ActionForm  {
{
 20
 /**//*
    /**//*
 21 * Generated fields
     * Generated fields
 22 */
     */
 23
 24
 /** *//** password property */
    /** *//** password property */
 25 private String password;
    private String password;
 26
 27
 /** *//** username property */
    /** *//** username property */
 28 private String username;
    private String username;
 29
 30
 /** *//** id property */
    /** *//** id property */
 31 private int id;
    private int id;
 32
 33
 /**//*
    /**//*
 34 * Generated Methods
     * Generated Methods
 35 */
     */
 36
 37
 /** *//**
    /** *//** 
 38 * Method validate
     * Method validate
 39 * @param mapping
     * @param mapping
 40 * @param request
     * @param request
 41 * @return ActionErrors
     * @return ActionErrors
 42 */
     */
 43 public ActionErrors validate(ActionMapping mapping,
    public ActionErrors validate(ActionMapping mapping,
 44
 HttpServletRequest request)
            HttpServletRequest request)  {
{
 45 // TODO Auto-generated method stub
        // TODO Auto-generated method stub
 46 return null;
        return null;
 47 }
    }
 48
 49
 /** *//**
    /** *//** 
 50 * Method reset
     * Method reset
 51 * @param mapping
     * @param mapping
 52 * @param request
     * @param request
 53 */
     */
 54
 public void reset(ActionMapping mapping, HttpServletRequest request)
    public void reset(ActionMapping mapping, HttpServletRequest request)  {
{
 55 // TODO Auto-generated method stub
        // TODO Auto-generated method stub
 56 }
    }
 57
 58
 /** *//**
    /** *//** 
 59 * Returns the password.
     * Returns the password.
 60 * @return String
     * @return String
 61 */
     */
 62
 public String getPassword()
    public String getPassword()  {
{
 63 return password;
        return password;
 64 }
    }
 65
 66
 /** *//**
    /** *//** 
 67 * Set the password.
     * Set the password.
 68 * @param password The password to set
     * @param password The password to set
 69 */
     */
 70
 public void setPassword(String password)
    public void setPassword(String password)  {
{
 71 this.password = password;
        this.password = password;
 72 }
    }
 73
 74
 /** *//**
    /** *//** 
 75 * Returns the username.
     * Returns the username.
 76 * @return String
     * @return String
 77 */
     */
 78
 public String getUsername()
    public String getUsername()  {
{
 79 return username;
        return username;
 80 }
    }
 81
 82
 /** *//**
    /** *//** 
 83 * Set the username.
     * Set the username.
 84 * @param username The username to set
     * @param username The username to set
 85 */
     */
 86
 public void setUsername(String username)
    public void setUsername(String username)  {
{
 87 this.username = username;
        this.username = username;
 88 }
    }
 89
 90
 /** *//**
    /** *//** 
 91 * Returns the id.
     * Returns the id.
 92 * @return int
     * @return int
 93 */
     */
 94
 public int getId()
    public int getId()  {
{
 95 return id;
        return id;
 96 }
    }
 97
 98
 /** *//**
    /** *//** 
 99 * Set the id.
     * Set the id.
100 * @param id The id to set
     * @param id The id to set
101 */
     */
102
 public void setId(int id)
    public void setId(int id)  {
{
103 this.id = id;
        this.id = id;
104 }
    }
105 }
} 
ApplicationResources.properties
 1 # Resources for parameter 'conf.ApplicationResources'
# Resources for parameter 'conf.ApplicationResources'
 2 # Project StrutsToken
# Project StrutsToken
 3 # Error messages
# Error messages
 4 error.token=org.apache.struts.taglib.html.TOKEN
error.token=org.apache.struts.taglib.html.TOKEN
 5
 6
 7 # Button messages
# Button messages
 8 button.submit=Submit
button.submit=Submit
 9 button.reset=Reset
button.reset=Reset
10
11
12 # Lable messages
# Lable messages
13 lable.username=username
lable.username=username
14 lable.password=password
lable.password=password
15
 
struts-config.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
 3
 4 <struts-config>
<struts-config>
 5 <data-sources />
    <data-sources />
 6 <form-beans>
    <form-beans>
 7 <form-bean name="userForm"
        <form-bean name="userForm"
 8 type="com.lucky.struts.form.UserForm" />
            type="com.lucky.struts.form.UserForm" />
 9
10 </form-beans>
    </form-beans>
11
12 <global-exceptions />
    <global-exceptions />
13 <global-forwards>
    <global-forwards>
14 <forward name="success" path="/success.jsp"></forward>
        <forward name="success" path="/success.jsp"></forward>
15 <forward name="error" path="/error.jsp"></forward>
        <forward name="error" path="/error.jsp"></forward>
16 </global-forwards>
    </global-forwards>
17 <action-mappings>
    <action-mappings>
18 <action attribute="userForm" input="/user.jsp" name="userForm"
        <action attribute="userForm" input="/user.jsp" name="userForm"
19 parameter="method" path="/user" scope="request"
            parameter="method" path="/user" scope="request"
20 type="com.lucky.struts.action.UserAction">
            type="com.lucky.struts.action.UserAction">
21 <forward name="toAdd" path="/add.jsp"></forward>
            <forward name="toAdd" path="/add.jsp"></forward>
22 </action>
        </action>
23
24 </action-mappings>
    </action-mappings>
25
26 <message-resources parameter="conf.ApplicationResources" />
    <message-resources parameter="conf.ApplicationResources" />
27 </struts-config>
</struts-config>
28
29
 
add.jsp
 1
 <%
<% @ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
 2
 <%
<% @include file="./taglib.jsp"%>
@include file="./taglib.jsp"%>
 3
 <%
<%
 4 String path = request.getContextPath();
    String path = request.getContextPath();
 5 String basePath = request.getScheme() + "://"
    String basePath = request.getScheme() + "://"
 6 + request.getServerName() + ":" + request.getServerPort()
            + request.getServerName() + ":" + request.getServerPort()
 7 + path + "/";
            + path + "/";
 8 %>
%>
 9
10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
11 <html>
<html>
12 <head>
    <head>
13 <base href="<%=basePath%>">
        <base href="<%=basePath%>">
14
15 <title>My JSP 'add.jsp' starting page</title>
        <title>My JSP 'add.jsp' starting page</title>
16
17 <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="pragma" content="no-cache">
18 <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
19 <meta http-equiv="expires" content="0">
        <meta http-equiv="expires" content="0">
20 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
21 <meta http-equiv="description" content="This is my page">
        <meta http-equiv="description" content="This is my page">
22 <!--
        <!--
23 <link rel="stylesheet" type="text/css" href="styles.css">
    <link rel="stylesheet" type="text/css" href="styles.css">
24 -->
    -->
25
26 </head>
    </head>
27
28 <body>
    <body>
29 <br>
        <br>
30 <br>
        <br>
31 This is my JSP page.
        This is my JSP page.
32 <br>
        <br>
33 <html:form action="user.do?method=executeAdd" method="post">
        <html:form action="user.do?method=executeAdd" method="post">
34 <bean:message key="lable.username" />:<html:text property="username"></html:text>
            <bean:message key="lable.username" />:<html:text property="username"></html:text>
35 <br>
            <br>
36 <bean:message key="lable.password" />:<html:text property="password"></html:text>
            <bean:message key="lable.password" />:<html:text property="password"></html:text>
37 <html:submit>
            <html:submit>
38 <bean:message key="button.submit" />
                <bean:message key="button.submit" />
39 </html:submit>   
            </html:submit>   
40 <html:reset>
            <html:reset>
41 <bean:message key="button.reset"/>
                <bean:message key="button.reset"/>
42 </html:reset>
            </html:reset>
43 </html:form>
        </html:form>
44 </body>
    </body>
45 </html>
</html>
46
 
error.jsp
 1
 <%
<% @ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
 2
 <%
<% @include file="./taglib.jsp" %>
@include file="./taglib.jsp" %>
 3
 <%
<%
 4 String path = request.getContextPath();
String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
%>
 7
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
<html>
10 <head>
  <head>
11 <base href="<%=basePath%>">
    <base href="<%=basePath%>">
12 
    
13 <title>My JSP 'error.jsp' starting page</title>
    <title>My JSP 'error.jsp' starting page</title>
14 
    
15 <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="pragma" content="no-cache">
16 <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
17 <meta http-equiv="expires" content="0">
    <meta http-equiv="expires" content="0">    
18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19 <meta http-equiv="description" content="This is my page">
    <meta http-equiv="description" content="This is my page">
20 <!--
    <!--
21 <link rel="stylesheet" type="text/css" href="styles.css">
    <link rel="stylesheet" type="text/css" href="styles.css">
22 -->
    -->
23
24 </head>
  </head>
25 
  
26 <body>
  <body>
27 This is my error JSP page. <br>
    This is my error JSP page. <br>
28 <html:errors name="message"/>
    <html:errors name="message"/>
29 </body>
  </body>
30 </html>
</html>
31
 
index.jsp
 1
 <%
<% @ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
 2
 <%
<% @ include file="./taglib.jsp" %>
@ include file="./taglib.jsp" %>
 3
 <%
<%
 4 String path = request.getContextPath();
String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
%>
 7
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
<html>
10 <head>
  <head>
11 <base href="<%=basePath%>">
    <base href="<%=basePath%>">
12 
    
13 <title>My JSP 'index.jsp' starting page</title>
    <title>My JSP 'index.jsp' starting page</title>
14 <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="pragma" content="no-cache">
15 <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
16 <meta http-equiv="expires" content="0">
    <meta http-equiv="expires" content="0">    
17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18 <meta http-equiv="description" content="This is my page">
    <meta http-equiv="description" content="This is my page">
19 <!--
    <!--
20 <link rel="stylesheet" type="text/css" href="styles.css">
    <link rel="stylesheet" type="text/css" href="styles.css">
21 -->
    -->
22 </head>
  </head>
23 
  
24 <body>
  <body>
25 This is my JSP page. <br>
    This is my JSP page. <br>
26 <html:link href="user.do?method=toAdd">To add page</html:link>
    <html:link href="user.do?method=toAdd">To add page</html:link>
27 </body>
  </body>
28 </html>
</html>
29
 
success.jsp
 1
 <%
<% @ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
 2
 <%
<%
 3 String path = request.getContextPath();
String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
%>
 6
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
<html>
 9 <head>
  <head>
10 <base href="<%=basePath%>">
    <base href="<%=basePath%>">
11 
    
12 <title>My JSP 'success.jsp' starting page</title>
    <title>My JSP 'success.jsp' starting page</title>
13 
    
14 <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="pragma" content="no-cache">
15 <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
16 <meta http-equiv="expires" content="0">
    <meta http-equiv="expires" content="0">    
17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18 <meta http-equiv="description" content="This is my page">
    <meta http-equiv="description" content="This is my page">
19 <!--
    <!--
20 <link rel="stylesheet" type="text/css" href="styles.css">
    <link rel="stylesheet" type="text/css" href="styles.css">
21 -->
    -->
22
23 </head>
  </head>
24 
  
25 <body>
  <body>
26 This is my success JSP page. <br>
    This is my success JSP page. <br>
27 </body>
  </body>
28 </html>
</html>
29
 
taglib.jsp
1
 <%
<% @ page language="java" pageEncoding="ISO-8859-1"%>
@ page language="java" pageEncoding="ISO-8859-1"%>
2
3
 <%
<% @ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
4
 <%
<% @ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
5
 <%
<% @ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
6
 <%
<% @ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
7
8
 
web.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 3 <servlet>
  <servlet>
 4 <servlet-name>action</servlet-name>
    <servlet-name>action</servlet-name>
 5 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
 6 <init-param>
    <init-param>
 7 <param-name>config</param-name>
      <param-name>config</param-name>
 8 <param-value>/WEB-INF/struts-config.xml</param-value>
      <param-value>/WEB-INF/struts-config.xml</param-value>
 9 </init-param>
    </init-param>
10 <init-param>
    <init-param>
11 <param-name>debug</param-name>
      <param-name>debug</param-name>
12 <param-value>3</param-value>
      <param-value>3</param-value>
13 </init-param>
    </init-param>
14 <init-param>
    <init-param>
15 <param-name>detail</param-name>
      <param-name>detail</param-name>
16 <param-value>3</param-value>
      <param-value>3</param-value>
17 </init-param>
    </init-param>
18 <load-on-startup>0</load-on-startup>
    <load-on-startup>0</load-on-startup>
19 </servlet>
  </servlet>
20 <servlet-mapping>
  <servlet-mapping>
21 <servlet-name>action</servlet-name>
    <servlet-name>action</servlet-name>
22 <url-pattern>*.do</url-pattern>
    <url-pattern>*.do</url-pattern>
23 </servlet-mapping>
  </servlet-mapping>
24 <welcome-file-list>
  <welcome-file-list>
25 <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.jsp</welcome-file>
26 </welcome-file-list>
  </welcome-file-list>
27 </web-app>
</web-app>
28
29