web.xml文件
<?xml version="1.0" encoding="GBK"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts2_Test</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> </web-app>
struts.xml文件
<?xml version="1.0" encoding="GBK"?> <!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.custom.i18n.resources" value="mess"></constant> <constant name="struts.i18n.encoding" value="GBK"></constant> <package name="lee" extends="struts-default" namespace="/master"> <action name="login" class="com.lee.action.LoginAction" method="execute"> <result name="input">/login.jsp</result> <result name="success">/welcome.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
login.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title><s:text name="loginPage"></s:text></title>
</head>
<body>
<s:form action="master/login">
<s:textfield name="username" key="username"></s:textfield>
<s:password name="password" key="password"></s:password>
<s:submit key="login"></s:submit>
</s:form>
</body>
</html>
LoginAction.java
package com.lee.action; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport implements Action, ServletContextAware, ServletRequestAware, ServletResponseAware { private static final long serialVersionUID = 1L; private String username; private String password; private HttpServletResponse response; private HttpServletRequest request; public String execute() { if ("lee".equals(getUsername()) && "Lz123456".equals(getPassword())) { Map<String, Object> session = ActionContext.getContext() .getSession(); session.put("username", getUsername()); request = ServletActionContext.getRequest(); response = ServletActionContext.getResponse(); return SUCCESS; } return ERROR; } /** * @return the username */ public String getUsername() { return username; } /** * @param username * the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the password */ public String getPassword() { return password; } /** * @param password * the password to set */ public void setPassword(String password) { this.password = password; } @Override public void setServletResponse(HttpServletResponse response) { this.response = response; } @Override public void setServletRequest(HttpServletRequest request) { this.request = request; } @Override public void setServletContext(ServletContext servletContext) { } }