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