struts2.5.x環境配置

  • 開發環境
    Myeclipse2017 / Tomcat8.5 / Struts2.5.12
  • jar包導入
1.  commons-fileupload-1.3.3.jar  
2.  commons-io-2.4.jar  
3.  commons-lang3-3.3.6.jar  
4.  commons-logging-1.1.3.jar  
5.  freemarker-2.3.23.jar  
6.  javassist-3.20.0-GA.jar  
7.  log4j-api-2.5.jar  
8.  log4j-core-2.8.2.jar
9.  ognl-3.1.12.jar  
10. struts2-core-2.5.2.jar  
  • 在web.xml中編寫配置文件
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  • 寫struts2.xml文件
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
        
<struts>
    <package name="action" extends="struts-default">
    <!-- 
        @package        package is a unit to group action which is similar to the package in Java
                            package can be extended and overridden.
        @attribute        name          it is required for distinction.
                            extends     inherits other package
                            namespace   URL of the web project
                            abstract        an empty package
     -->
        <action name="hello" class="action.action1" >
        <!--  
        @action     action is a action that you can visit the JSP page by the action name
        @attribute        name      it is require for distinction
                            flush       Default is true.flush the write at the end of the action or not.
                            ignoreContextParams when the action is invoked ,the parameter is included.
                            var         Push the value into Value Stack
        -->
            <result name="success">/hello.jsp</result>
            <!-- 
            @result         Result is the definition of action
            @attribute  name        it has default name "dispatcher".It corresponds to the function
                                       of the JavaBean.
             -->
        </action>
    </package>
    
</struts>
  • 寫java類實現execute()方法
package action;

public class action1 {
    public String  execute() {
        System.out.println("action1");
        return "success";
    }
}
  • 對應的jsp頁面(只是簡單顯示而已)命名要和struts.xml中的result中的值對應,通過瀏覽器訪問。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 概述 什么是Struts2的框架Struts2是Struts1的下一代產品,是在 struts1和WebWork的...
    inke閱讀 2,276評論 0 50
  • 本文包括: 1、Struts 2 概述2、Struts 2 快速入門3、Struts 2 的執行流程4、配置 st...
    廖少少閱讀 2,998評論 3 13
  • 概述 Struts就是基于mvc模式的框架!(struts其實也是servlet封裝,提高開發效率!) Strut...
    奮斗的老王閱讀 2,986評論 0 51
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • strust2框架來歷簡介: WebWork是由OpenSymphony組織開發的,致力于組件化和代碼重用的J2E...
    清楓_小天閱讀 8,932評論 4 26