Struts2入門(一)——設定檔,struts2入門

來源:互聯網
上載者:User

Struts2入門(一)——設定檔,struts2入門
一、前言

1、瞭解三大架構

什麼是架構?

架構是一種規範,一種規則,一種把技術組織起來的規則,這就是架構。

 

什麼是三大架構(SSH),Struts、hibernate和spring的作用是什嗎?

Struts主要負責層的顯示,spring利用IOC和AOP來處理控制業務(負責對資料庫的操作),hibernate主要作用是資料的持久性到資料庫。

Struts的處理流程:

在Struts 2下載檔案Struts-core-2.5.2.檔案中(注意:不同jarStruts核心控制器的名稱不同),StrutsPrepareAndExecuteFilter是Struts 2架構的核心控制器,

它負責攔截由<url-pattern>/*</url-pattern>指定的所有使用者請求,當使用者請求到達時,該Filter會過濾使用者的請求。

預設情況下,如果使用者請求的路徑不帶尾碼或者後以.action結尾,這時請求將被轉入Struts2架構處理,否則Struts 2架構將略過該請求的處理。

當請求轉入Struts 2架構處理時會先經過一系列的攔截器,然後再到Action。與Struts1不同,Struts 2對使用者的每一次請求都會建立一個Action,所以Struts2中的Action是安全執行緒的。

2、搭建Struts 2環境

(1)下載關於Struts 2所需要的jar檔案,這裡使用的是struts-2.5.2-all.zip,(這裡)

(2)編寫Struts 2設定檔,分為struts.xml

(3)在Web.xml加入Struts 2的架構啟動配置

 

搭建Struts 2開發環境——所需的jar包

下載完畢之後解壓檔案,將所需要的檔案拷貝到項目的lib檔案下,這裡配置的是Struts 2.5所需要的設定檔

必備jar包:尤其是log4j.jar和javassist.jar為必備包

注意:如果clesses沒有的話,自行建立,然後建立struts.xml

配置模板代碼如下:

<?xml version="1.0" encoding="UTF-8"?>  <!--注意:有時候建立不會自動產生以下的<!DOCTYPE>,所以需要自動引入,
如果使用的是2.3或者是1點幾的話,只需要將Confinuration2.5//EN和下面的struts-2.5.dtd中的2.5改變為2.3或者1.幾即可--><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"><struts> </struts>

 

搭建Struts 2開發環境——Struts 2在Web中的啟動配置

在struts1.x中, struts架構是通過Servlet啟動的。在struts2中,struts架構是通過Filter啟動的。在web.xml中的配置如下:

  <!--為Struts2定義一個過濾器  -->   <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>

注意:<!-- 自從Struts 2.1.3以後,下面的FilterDispatcher已經標註為過時  

     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> --> 

在StrutsPrepareAndExecuteFilter類中的init方法會讀取路徑下的設定檔struts.xml完成初始化操作

在讀取完struts.xml的內容後,會以javabean形式存放到內容中,以後struts 2對使用者的每次請求都會使用記憶體中的資料,而不是去讀取檔案。

 

入門例子:(該例子是官方文檔例子)

在建立的struts.xml設定檔中加入配置:

 

<!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"    "http://struts.apache.org/dtds/struts-2.5.dtd"><struts>    <package name="tutorial" extends="struts-default"><!--name:可自己定義名稱,class:實作類別的準確路徑-->        <action name="helloword" class="com.hello.HelloWord" method="execute">            <result name="success">HelloWorld.jsp</result>        </action>    </package></struts>

在Struts2架構中使用包來管理action,主要用於管理一組業務相關的action,實際開發中,應該把一組業務相關的Action放在同一個包下。

注意:struts.xm和Actoin配合使用,難免會使檔案變得臃腫,我們可以通過<include file="檔案名稱.xml"/>將struts.xml分割成多個。

 

解析struts.xml中的標籤:

Action中的name屬性:

配置包時必須指定name屬性,可以任意命名但是必須唯一,不能對應java類名,如果要讓其他包繼承該包,可使用該屬性進行引用。

method屬性:

指定伺服器調用哪個方法,預設為execute。

namespace屬性:

表示命名空間,作為訪問路徑的一部分,如果路徑是helloword.action的話,則加上/命名空間/helloword.action,如果不指定,預設為""(Null 字元串)

通常每個包都應該繼承struts-default包, 因為Struts2很多核心的功能都是攔截器來實現。

使用Action元素必須指定系列的result元素,每個result元素定義邏輯視圖和物理視圖之間的映射。

result元素中type屬性預設jsp為視圖,可設定type的值改變資料所要顯示的視圖。

 

例子中使用的HellWord.java實作類別代碼:

package com.hello;import com.opensymphony.xwork2.ActionSupport;public class HelloWord extends ActionSupport {        public static final String MESSAGE = "Struts is up and running ...";    private String message;    public void setMessage(String message){        this.message = message;    }     public String getMessage() {        return message;    }    public String execute() throws Exception {        setMessage(MESSAGE);        return SUCCESS;    }}

 

struts.xml設定檔中的HelloWord.jsp代碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %>    <!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>Hello World</title></head>    <head>        <title>Hello World!</title>    </head>    <body>        <h2><s:property value="message" /></h2>    </body></html>

 

訪問struts2中action的URL路徑由兩部份組成:包的命名空間+action的名稱,例如訪問本例子HelloWord的URL路徑為:/helloworld (注意:完整路徑為

http://localhost:連接埠/項目名稱/helloworld)。另外我們也可以加上.action尾碼訪問此Action。

 

入門例子運行效果如下:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.