开发环境:
Eclipse IDE for Java EE Developers(下载地址)
struts-2.3.1.2(下载地址)
apache-tomcat-6.0.35(下载地址)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>struts2_20120314_01</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" extends="struts-default"> <action name="LoginAction" class="com.zeph.struts2.action.LoginAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>
package com.zeph.struts2.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1L; private String userName; private String password; private ActionContext context; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public void validate() { if(getUserName().length() == 0){ addFieldError("userName", "User Name is required."); } } @Override public String execute() throws Exception { context = ActionContext.getContext(); Integer count = (Integer) context.getApplication().get("count"); if (count == null) { count = 1; } else { count++; } context.getApplication().put("count", count); if (getUserName().equals("admin")) { context.getSession().put("userName", getUserName()); return SUCCESS; } else { return ERROR; } } }
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>login</title> </head> <body> <s:form action="LoginAction"> <s:textfield name="userName" label="UserName"></s:textfield> <s:password name="password" label="Password"></s:password> <s:submit></s:submit> </s:form> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login Success</title> </head> <body> <s:property value="#session.userName" /> ,login success, <a href="context.jsp">redirect to context</a>. times: <s:property value="#application.count" /> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>context page</title> </head> <body> welcome, <s:property value="#session.userName" /> . <script language="JavaScript" type="text/javascript"> var today; var day; var date; today = new Date(); if (today.getDay() == 0) day = "Sunday "; if (today.getDay() == 1) day = "Monday"; if (today.getDay() == 2) day = "Tuesday "; if (today.getDay() == 3) day = "Wednesday"; if (today.getDay() == 4) day = "Thursday "; if (today.getDay() == 5) day = "Friday"; if (today.getDay() == 6) day = "Saturday "; date = "Today is " + (today.getYear()) + "/" + (today.getMonth() + 1) + "/" + today.getDate(); document.write(date); document.write(" " + day); </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>error</title> </head> <body> <P>Sorry, login failed!!!!</P> </body> </html>