歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2--Helloworld 入門實例

Struts2--Helloworld 入門實例

日期:2017/3/1 9:17:27   编辑:Linux編程

搭建Struts2環境時,我們一般需要做以下幾個步驟的工作:

1、找到開發Struts2應用需要使用到的jar文件. 2、創建Web工程 3、在web.xml中加入Struts2 MVC框架啟動配置 4、編寫Struts2的配置文件

開發Struts2應用依賴的jar文件

大家可以到http://struts.apache.org/download.cgi#struts2014下載struts-2.x.x-all.zip。下載完後解壓文件,開發struts2應用需要依賴的jar文件在解壓目錄的lib文件夾下。不同的應用需要的JAR包是不同的。下面給出了開發Struts 2程序最少需要的JAR。

struts2-core-2.x.x.jar :Struts 2框架的核心類庫

xwork-core-2.x.x.jar :XWork類庫,Struts 2在其上構建

ognl-2.6.x.jar :對象圖導航語言(Object Graph Navigation Language),struts2框架通過其讀寫對象的屬性

freemarker-2.3.x.jar :Struts 2的UI標簽的模板使用FreeMarker編寫

commons-logging-1.x.x.jar :ASF出品的日志包,Struts 2框架使用這個日志包來支持Log4J和JDK 1.4+的日志記錄。

commons-fileupload-1.2.1.jar :文件上傳組件,2.1.6版本後必須加入此文件

以上這些Jar文件拷貝 到Web項目的WEB-INF/lib目錄中。

Struts2在web.xml中的啟動配置

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

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

在StrutsPrepareAndExecuteFilter的init()方法中將會讀取類路徑下默認的配置文件struts.xml完成初始化操作。

注意:struts2讀取到struts.xml的內容後,以javabean形式存放在內存中,以後struts2對用戶的每次請求處理將使用內存中的數據,而不是每次都讀取struts.xml文件

Struts2應用的配置文件

Struts2默認的配置文件為struts.xml ,該文件需要存放在src目錄下,該文件的配置模版如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

</struts>

實戰--helloworld

Struts2的每個請求需要實現以下內容: JSP Action struts.xml中Action配置

1、新建項目12.01

2、在默認的配置文件struts.xml 中加入如下配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="Hello_World_Struts2" extends="struts-default">
        <action name="index">
            <result>/index.jsp</result>
        </action>
        <action name="hello" class="com.wuyudong.helloworld.action.HelloWorldAction" method="execute">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>

在struts2框架中使用包來管理Action,包的作用和java中的類包是非常類似的,它主要用於管理一組業務功能相關的action。在實際應用中,我們應該把一組業務功能相關的Action放在同一個包下。

配置包時必須指定name屬性,該name屬性值可以任意取名,但必須唯一,他不對應java的類包,如果其他包要繼承該包,必須通過該屬性進行引用。包的namespace屬性用於定義該包的命名空間,命名空間作為訪問該包下Action的路徑的一部分,如訪問上面例子的Action,訪問路徑為:/test/helloworld.action。 namespace屬性可以不配置,對本例而言,如果不指定該屬性,默認的命名空間為“”(空字符串)。

通常每個包都應該繼承struts-default包, 因為Struts2很多核心的功能都是攔截器來實現。如:從請求中把請求參數封裝到action、文件上傳和數據驗證等等都是通過攔截器實現的。 struts-default定義了這些攔截器和Result類型。可以這麼說:當包繼承了struts-default才能使用struts2提供的核心功能。 struts-default包是在struts2-core-2.x.x.jar文件中的struts-default.xml中定義。 struts-default.xml也是Struts2默認配置文件。 Struts2每次都會自動加載 struts-default.xml文件。

例子中使用到的com.wuyudong.helloworld.action.HelloWorldAction類如下:

package com.wuyudong.helloworld.action;

import com.opensymphony.xwork2.ActionSupport;
import com.wuyudong.helloworld.model.MessageStore;

public class HelloWorldAction extends ActionSupport {
    private static final long serialVersionUID = -4958566543551999157L;
    private MessageStore msgStore;
    @Override
    public String execute() throws Exception {
        msgStore = new MessageStore("HelloWorld!");
        return SUCCESS;
    }
    public MessageStore getMsgStore() {
        return msgStore;
    }
    public void setMsgStore(MessageStore msgStore) {
        this.msgStore = msgStore;
    }
}

例子中使用到的hello.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>
<body>
    <h2>
        <s:property value="msgStore.message" />
    </h2>
</body>
</html>

在struts2中,訪問struts2中action的URL路徑由兩部份組成:包的命名空間+action的名稱,例如訪問本例子HelloWorldAction的URL路徑為:/helloworld (注意:完整路徑為:http://localhost:端口/內容路徑/helloworld)。另外我們也可以加上.action後綴訪問此Action。

Action名稱的搜索順序

1.獲得請求路徑的URI,例如url是:http://server/struts2/path1/path2/path3/test.action

2.首先尋找namespace為/path1/path2/path3的package,如果不存在這個package則執行步驟3;如果存在這個package,則在這個package中尋找名字為test的action,當在該package下尋找不到action 時就會直接跑到默認namaspace的package裡面去尋找action(默認的命名空間為空字符串“” ) ,如果在默認namaspace的package裡面還尋找不到該action,頁面提示找不到action

3.尋找namespace為/path1/path2的package,如果不存在這個package,則轉至步驟4;如果存在這個package,則在這個package中尋找名字為test的action,當在該package中尋找不到action 時就會直接跑到默認namaspace的package裡面去找名字為test的action ,在默認namaspace的package裡面還尋找不到該action,頁面提示找不到action

4.尋找namespace為/path1的package,如果不存在這個package則執行步驟5;如果存在這個package,則在這個package中尋找名字為test的action,當在該package中尋找不到action 時就會直接跑到默認namaspace的package裡面去找名字為test的action ,在默認namaspace的package裡面還尋找不到該action,頁面提示找不到action

5.尋找namespace為/的package,如果存在這個package,則在這個package中尋找名字為test的action,當在package中尋找不到action或者不存在這個package時,都會去默認namaspace的package裡面尋找action,如果還是找不到,頁面提示找不到action。

Action配置中的各項默認值

創建 Model類MessageStore

package com.wuyudong.helloworld.model;

public class MessageStore {
    private String message;
    public MessageStore(String msg){
        this.setMessage(msg);
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

創建Action類HelloWorldAction,充當Controller

package com.wuyudong.helloworld.action;

import com.opensymphony.xwork2.ActionSupport;
import com.wuyudong.helloworld.model.MessageStore;

public class HelloWorldAction extends ActionSupport {
    private static final long serialVersionUID = -4958566543551999157L;
    private MessageStore msgStore;
    @Override
    public String execute() throws Exception {
        msgStore = new MessageStore("HelloWorld!");
        return SUCCESS;
    }
    public MessageStore getMsgStore() {
        return msgStore;
    }
    public void setMsgStore(MessageStore msgStore) {
        this.msgStore = msgStore;
    }
}

配置web.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>12.01</display-name>
   <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
    <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
         </filter-class>
    </filter>
    
    <filter-mapping>
      <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

運行後如圖所示

Copyright © Linux教程網 All Rights Reserved