Tomcat的webapps文件夹下面的文件结构如下图
struts2qs
|----WEB-INF
| |----classes(struts.xml , LoginAction.class)
| |----lib(struts2-core.jar, xwork-core.jar, ognl.jar, freemarker.jar, commons-logging.jar, commons-io.jar, comons-fileupload.jar, commons-long.jar, javassis.GA.jar )
| |----web.xml
|----login.jsp
|---welcome.jsp
|---error.jsp
其中javassis.GA.jar 在structs/lib里面没有,在struts 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>
<package name="lee" extends="struts-default">
<action name="Login" class="lee.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="GBK"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 定义Struts2的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
其中,
<filter-class>中,在struts2.1之前是org.apache.struts2.dispatcher.FilterDispatcher。structs2.1之后是
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterlogin.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%> <html> <head> <title>登录页面</title> </head> <body> <form action="Login.action" method="post"> <table align="center"> <caption><h3>用户登录</h3></caption> <tr> <td>用户名:<input type="text" name="username"/></td> </tr> <tr> <td>密 码:<input type="text" name="password"/></td> </tr> <tr align="center"> <td colspan="2"><input type="submit" value="登录"/><input type="reset" value="重填" /></td> </tr> </table> </form> </body> </html>
控制器 LoginAction.java
package lee; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; /** * @author yeeku.H.lee kongyeeku@163.com * @version 1.0 * <br>Copyright (C), 2005-2008, yeeku.H.Lee * <br>This program is protected by copyright laws. * <br>Program Name: * <br>Date: */ public class LoginAction implements Action { private String username; private String password; 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; } public String execute() throws Exception { if (getUsername().equals("scott") && getPassword().equals("tiger") ) { ActionContext.getContext().getSession().put("user" , getUsername()); return SUCCESS; } else { return ERROR; } } }