天天看點

Struts2架構的搭建

搭建一個struts2的架構,在之前已經搭建過struts的架構了,這裡的流程基本上差不多,詳見 struts1的搭建 首先到官網上下載下傳jar包,這裡附一個git的連結 struts2jar包下載下傳 建立工程,将下載下傳的jar解壓至工程中,項目結構如下:

Struts2架構的搭建

項目結構

接下來編寫struts.xml

預設加載的配置檔案名為struts.xml

private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";

此處為Dispatcher中的設定

如果要預設讀取的位置需要在struts2filter中加入

<init-param>  
        <param-name>filterConfig</param-name>  
        <param-value>classpath:struts2_demo/struts.xml</param-value>  
    </init-param>  
           

下面是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>
    <!-- package是包,是struts提供的對于action的管理方式 -->
    <!-- name是package的唯一标示符 -->
    <!-- extends 繼承 繼承會将被繼承包的action與result都繼承到,而所有包都需要繼承struts-default這個一個包 -->
    <!-- struts-default這一個包是struts提供給我們的一個預設包,裡面包含了很多已經定義好的攔截器與result -->
    <package name="hello" extends="struts-default">
        <!-- action 是指一個具體的動作 -->
        <!-- class 指處理這一具體動作的類 -->
        <!-- method 指處理該動作的類的方法,預設為execute -->
        <action name="hi" class="com.education.action.HelloWorldAction"
            method="execute">
            <!-- result 傳回結果 -->
            <!-- name 指執行方法的傳回值,預設為success -->
            <!-- result中間的值是最終跳轉到的頁面 -->
            <!-- result的預設跳轉方式為轉發,如果需要重定向,則增加一個屬性 type="redirect";重定向到某個action type="redirectAction" -->
            <result>/success.jsp</result>
            <result name="error">/fail.jsp</result>
        </action>
    </package>
</struts>
           

struts.xml配置好後需要在web.xml中加入struts的過濾器

<?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" version="3.0">
  <display-name>struts2_demo</display-name>
  <!-- 配置struts2的過濾器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <!-- 該類可在struts2-core包中找到 -->
    <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>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
           

struts配置完畢後建立一個action

package com.education.action;

import com.education.bean.User;
import com.opensymphony.xwork2.ActionSupport;

//action都會繼承一個ActionSupport,單這不是強制的,ActionSupport中包含了很多方法以及常用常量
public class HelloWorldActionextends ActionSupport {

    // 前台傳入的值會直接注入到該Action的屬性中,必須含有get/set方法
    // 如果是非對象則以 <input name="testMsg"/>這樣的形式傳值
    // 如果是對象則以<input name="user.username"/>這樣的形式傳值
    private User user;

    private String testMsg;

    public String getTestMsg() {
        return testMsg;
    }

    public void setTestMsg(String testMsg) {
        this.testMsg = testMsg;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    // execute方法是當struts.xml沒有指定方法來處理請求時,就會預設調用該方法
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    // validate方法是struts架構自帶的驗證方法
    // 如果重寫了該方法則會先于execute方法執行
    // 如果運作了addFieldError方法則會直接傳回,不再執行execute方法
    // 傳回值為input
    @Override
    public void validate() {
        if (1 > 2) {
            addFieldError("name", "it's impossible");
        }
    }
}
           

好了,一個struts2架構的項目就已經搭建完畢了,這裡就不再發jsp頁面的代碼了,剩下的部分就請自己補全吧。

這裡附上完整項目的下載下傳連結

點此下載下傳