[Struts 2系列] Struts 2入門之HelloWorld

Struts 2框架的下載與安裝:java

Struts 2下載地址: http://struts.apache.org/download.cgiweb

截止筆者準備開始着手寫[Struts 2]系列博客時,Struts 2的最新版本爲: Struts 2.3.24.1apache

下載Struts 2.3.24.1時包含以下下載項:小程序

Full Distribution: Struts 2的完整版api

Example Applications: Struts 2的示例應用 (Struts 2的完整版包含該選項下的所有應用)app

Essential Dependencies Only: Struts 2的核心庫(Struts 2的完整版包含該選項下的所有內容)框架

Documentation: Struts2的相關文檔(Struts 2的完整版包含該選項下的所有內容)jsp

Source: Struts 2的所有源代碼(Struts 2的完整版包含該選項下的所有內容)測試

這裏咱們只需下載 Full Distribution(Struts 2的完整版)便可。url

Struts 2框架目錄結構:

apps: 該文件夾下包含了基於Struts 2的示例應用,包含了5個WAR文件;

docs: 該文件夾下包含了Struts 2的相關文檔,包含Struts 2的快速入門、Struts 2的文檔以及API文檔等內容(該文件夾包含了docs、struts2-core-apidocs<Struts 2核心文檔>、struts2-plugins<Struts 2官方插件的API文檔>、xwork-apidocs<XWork的文檔>);

lib: 該文件夾下包含了Struts 2框架的核心類庫,以及Struts 2的第三方類庫;

src: 該文件夾下包含了Struts 2框架的所有源代碼;

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- HELLO WORLD -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

下面咱們一步步手動編寫第一個Hello World程序:

(1) MyEclipse中新建一個Web Project;

(2) 解壓apps目錄下struts2-blank.war示例,將解壓目錄的WEB-INF/lib下的jar包拷貝到上述項目的WEB-INF/lib下

(3) 編輯Web應用的web.xml配置文件,配置Struts 2的核心Filter,以下:

<?xml version="1.0" encoding="UTF-8"?>
<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>BestEU</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 定義Struts 2的核心控制器: StrutsPrepareAndExecuteFilter -->
  <filter>
   <!-- 定義核心Filter的名字 -->
   <filter-name>struts2</filter-name>
   <!-- 定義核心Filter的實現類 -->
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <!-- StrutsPrepareAndExecuteFilter用來處理全部的HTTP請求 -->
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

在作好上述準備,咱們就能夠使用Struts 2來爲咱們作一些事情了。下面將經過Struts 2來實現接受請求與返回結果。

(1) struts 2配置文件,配置Action

    將Struts 2的配置文件struts.xml文件放在classes路徑下,該文件主要放置Struts 2的Action定義。定義Struts 2 Action時,處理須要指定該Action的實現類外,還須要定義Action處理結果和資源之間的映射關係。

下面爲本示例應用的struts.xml文件代碼:

<?xml version="1.0" encoding="UTF-8" ?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<!-- 配置Struts 2配置文件的根元素 -->
<struts>
    <!-- 指定Struts 2開發模式devMode(開發模式下修改struts.xml後無需重啓服務,可實現熱部署) -->
    <constant name="struts.devMode" value="true" />
    <!-- Struts 2的Action必須放在指定的包空間下定義 -->
    <package name="default" namespace="/" extends="struts-default">
    	<action name="*">
    		<result>/WEB-INF/content/what/{1}.jsp</result>
    	</action>
    </package>
    
</struts>

: 在配置文件中咱們指定了請求路徑中包含namespace爲"/"的均被攔截,且該action可處理全部的請求,並返回結果/WEB-INF/content/what/{1}.jsp ({1}與*號內容相同)

(2) 啓動Web服務,輸入請求地址。

    請求地址例如: http://localhost:7996/ZLv_Struts/HelloWorld , 返回結果截圖:

報錯中咱們能夠看到Struts 2已經起做用,可是指定目錄下咱們沒有建立HelloWorld.jsp,因此報錯找不到HelloWorld.jsp。

到這裏,咱們完成了Java Web引用Struts 2的環境搭建,並以HelloWorld小程序測試了Struts 2.

在下一節內容中咱們將經過使用Struts 2實現簡單的登陸功能,使讀者能夠更好的掌握Struts 2的配置文件及處理流程.

相關文章
相關標籤/搜索