Struts2之【配置文件】

Struts2使用須要用到兩個配置文件html

struts.xml : Struts2本身的配置文件,用來配置Actionjava

web.xml : 用來加載Struts2框架web

1.struts.xml

struts.xml是Struts2默認的文件名,通常不會更改。apache

真正的項目中不會只用struts.xml一個配置文件,由於一個配置文件會形成配置文件過於臃腫, 會按照模塊進行拆分。服務器

struts.xml
    |-struts-user.xml
    |-...

如下是struts.xml和struts-user.xml的詳細配置及解釋app

struts.xml框架

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

    <!-- 開發者模式:自動從新加載。 每次修改XML配置時,不用重啓服務器。默認:false-->
    <constant name="struts.devMode" value="true" />
	
    <!-- URL中的動做擴展名: http://localhost:8080/StrutsDemo/User/login.html  
        不須要擴展名時,value="" :http://localhost:8080/StrutsDemo/User/login 
    -->
    <constant name="struts.action.extension" value="html" />
	
    <!-- 根據模塊將struts.xml文件也分紅多個, 分放在不一樣的包下 -->
    <include file="config/struts-user.xml"></include>

</struts>

struts-user.xmljsp

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    
<struts>
    <!-- name:包名
	 namespace:命名空間   (通常狀況 包名與命名空間名字一致, 命名空間首字母大寫)
                    http://localhost:8080/StrutsDemo/User/login
	 extends:繼承
          <parkage name="defalut" namespace="/" extends="struts-defalut"></parkage>
    -->
    <parkage name="user" namespace="User" extends=struts-default"">
	<!-- methord:指定須要執行的方法名, 默認執行execute()方法 -->
	<action name="login" class="com.main.java.action.user.LoginAction" methord="">
            <!-- 方法返回字符串:return SUCCESS; -->
	    <result name="SUCCESS">welcom.jsp</result>
	    <result name="Error">error.jsp</result>
			
	    <!-- type:redirect重定向,默認轉發方式 -->
	    <result name="INDEX" type="redirect">index/indexAction</result>
	</action>
    </parkage>
</struts>

2.web.xml

全部的MVC框架都須要web應用加載一個核心控制器。ide

對於struts2而言,須要加載FilterDispatch,只要加載了FileterDispatch,FileterDispatch就會自動加載Struts2框架url

因此只須要在web.xml中配置FileterDispatch就能夠了

具體配置詳解以下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<filter>
    <!-- 配置Struts2核心Filter的名字 -->
    <filter-name>struts2</filter-name>

    <!-- 配置Struts2核心Filter的實現類 -->
    <!-- FilterDispatch:在Struts2.1.3中已經被廢棄 -->
    <!-- Struts版本 >= 2.1.3 建議使用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFileter-->
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

    <!-- 配置Struts2框架默認加載的Action包結構,能夠沒有。 -->
    <init-param>
        <param-name>actionPackages</param-name>
        <param-value>org.apache.struts2.showcase.person</param-value>
    </init-param>

    <!-- 配置Struts2框架的配置提供者類,能夠沒有 -->
    <init-param>
        <param-name>configProviders</param-name>
        <param-value>lee.MyConfigurationProvider</param-value>
    </init-param>
</filter>

<!-- 配置Filter攔截的URL -->
<filter-mapping>
    <!-- 配置Struts2的核心FilterDispatcher攔截全部用戶請求 -->
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

爲Filter配置初始化參數時,其中有3個初始化參數有特殊意義

  • config:使用逗號隔開的字符串,每一個字符串都是一個XML配置文件的位置。Struts2框架將自動加載該屬性指定的系列配置文件。
  • actionPackages:使用逗號隔開的字符串,每一個字符串都是一個包空間,Struts2框架將掃描這些包空間下的Action。
  • configProviders:配置本身的ConfigurationProvider類

還能夠在此處配置Struts2常量

<init-param>
    <param-name>常量名</param-name>
    <param-value>常量值</param-value>
</init-param>
相關文章
相關標籤/搜索