配置struts2的最小空項目的方法:html
commons-logging-1.0.4.jar freemarker-2.3.16.jar ongnl-3.0.jar struts2-core-2.2.1.jar xwork-core-2.2.1.jar 從Struts2.1.6開始要再引入兩個包類: commons-fileupload-1.2.1.jar commons-io-1.3.2.jar
<?xml version="1.0" encoding="GBK"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!--定義struts 2的FileterDispatcher的Filter--> <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>login.jsp</welcome-file> </welcome-file-list> </web-app>
web.xml的filter配置得很不規範。。。不建議使用org.apache.struts2.dispatcher.FilterDispatcher了,
官方文檔建議使用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter java
<?xml version="1.0" encoding="GBK"?> <!-- 指定Struts 2配置文件的DTD信息 --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <!-- struts是Struts 2配置文件的根原屬 --> <struts> <!-- Struts 2的Action必須放在指定的包空間下定義 --> <package name="com.exercise.action" extends="struts-default"> <!-- 定義login的Action,該Action的實現類爲com.exercise.action類 --> <action name="Login" class="com.exercise.action.LoginAction"> <!-- 定義處理結果和資源之間映射關係。 --> <result name="input">/Login.jsp</result> <result name="error">/error.jsp</result> <result name="success">/welcome.jsp</result> </action> </package> </struts>
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
package johnson.action; public class loginAction{ 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{ //帳號,密碼:scott/tiger時,返回success //不然返回error if(getUsername().equals("scott") && getPassword().equals("tiger")) { return "success"; } else { return "error"; } } }
這樣就能夠啓動項目了web