struts2的配置和一個簡單的例子

來源:互聯網
上載者:User

本文最後更新於<2017年12月06日> Struts2版本號碼:2.5.14.1
更新內容:struts2和相關lib的版本號碼,例子能夠正確運行

簡介

這篇文章主要講如何在eclipse中使用struts2,文章使用的struts2的版本是2.5.14.1,
會與其他的版本有一小點的差別,文章裡已經說明。例子的完整源碼在文末,親測沒有任何錯誤。

struts2下載

官網下載地址 USTC鏡像站下載地址
最新版是2.5.14.1,這個版本的一些jar包與舊版本不太一樣,不過變化不大。
具體的變化建議閱讀2.5版本的 version-notes


圖1 struts2下載頁面

這裡選擇完整的包(Full Distribution)下載。

下載解壓後的檔案結構如下圖:

圖2 struts2檔案結構圖

apps中是使用struts2的一些例子,docs是文檔,包括協助文檔和api文檔,lib是jar包,src中是源碼。

一個簡單的例子

eclipe

環境

需要的環境 tomcat 我使用的是:tomcat-8.0.42 jdk > 7 我使用的是:jdk-1.8.0_131< eclipse 我使用的是:eclipse neon (4.6.2)

首先保證tomcat能夠正常運行,關於tomcat的配置這裡就不講了。

詳細的步驟

建立一個WEB項目,名為HelloWorld
在eclipse中建一個普通的WEB項目。保證項目能夠運行。

添加相關的jar包
把需要的jar包從struts2的lib目錄複寫到WEB-INF/lib檔案夾下,最基礎的需要8個jar包:
commons-fileupload-1.3.3.jar、commons-io-2.5.jar、commons-lang3-3.6.jar、freemarker-2.3.26.jar、
log4j-api-2.9.1.jar、ognl-3.1.15.jar、struts2-core-2.5.14.1.jar、javassist-3.20.0-GA.jar

注意:struts2.5之前的版本有點不同,還需要xwork-core.jar,不需要log4j-api-2.7.jar。原因是struts2.5把xwork的源碼
合并到了struts-core中。struts2.5之前使用logging API,而struts2.5用log4j 2 API取代。
如果你聽從了我的建議,閱讀了struts2.5的 version-notes ,你就能知道詳細的原因。

圖3 需要的jar包

在web.xml中配置struts2架構的核心控制器StrutsPrepareAndExexuteFilter

圖4 web.xml的配置

Filter的完整類名struts2.5以前是 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。
更多改變請閱讀 struts2.5 version-notes

在src目錄下建立一個業務控制Action類,繼承自com.opensymphony.xwork2.ActionSupport,內容如下:

圖5 建立一個Action

Action需要在Struts2的核心設定檔中進行配置
Struts2的核心設定檔為struts.xml,放在src目錄下。

圖6 struts.xml內容

注意struts.xml的置放位置。

建立一個result.jsp檔案,用來action顯示返回的視圖

圖7 result.jsp的內容

最後運行HelloWorld項目,在瀏覽器訪問http://localhost:8080/HelloWorld/helloworld
最後展現的內容應該是result.jsp的內容。

控制台輸出Action的列印內容

到這裡,struts2就算配置完成了。

涉及的代碼

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><display-name>HelloWorld</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- 配置核心攔截器 --><filter>    <!-- Filter的名字 -->    <filter-name>struts2</filter-name>    <!-- Filter的實作類別 struts2.5以前可能有所不同 -->    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping>    <filter-name>struts2</filter-name>    <!-- 攔截所有的url -->    <url-pattern>/*</url-pattern></filter-mapping></web-app>

struts.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="default" namespace="/" extends="struts-default">        <!-- name action的名字,訪問時使用helloworld.action訪問,class:實作類別 -->        <action name="helloworld" class="cn.xhcoding.action.HelloWorldAction">            <!-- 結果集,即action中SUCCESS返回的視圖 -->            <result>            /result.jsp            </result>        </action>        </package>    </struts>

HelloWorldAction.java

package cn.xhcoding.action;    public class HelloWorldAction extends com.opensymphony.xwork2.ActionSupport{      @Override      public String execute() throws Exception {          System.out.println("正在執行的Action");          // 返回視圖 SUCCESS,這是架構定義          return SUCCESS;      }    }
result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Action Result</title>
</head>
<body>
<h1>恭喜成功配置好基本的struts2環境</h1>
<h2>Hello World, I am Successful</h2>
</body>
</html>

本文永久更新地址

參考網址:
struts2官方文檔:http://struts.apache.org/docs/
struts2.5版本改變:http://struts.apache.org/docs/version-notes-25.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.