Struts2工做原理和核心文件

1、Struts2工做原理html

以下圖:java

 

2、Struts2配置文件web

一、web.xmlapache

任何MVC框架都須要與Web應用整合,這就不得不借助於web.xml文件,只有配置了web.xml文件的Servlet纔會被應用加載app

一般,全部的MVC框架都須要Web應用加載一個核心控制器,對於Struts2框架而言,須要加載StrutsPrepareAndExecuteFilter,StrutsPrepareAndExecuteFilter加載Struts框架。框架

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
 id="WebApp_ID" version="3.1">
  <display-name>MyStruts2</display-name>
  
  <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>
  <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.xmljsp

struts2的核心配置文件,負責管理應用中的Action映射,以及該Action包含的Result定義等。url

內容包括:spa

1) 全局屬性xml

2) 用戶請求和響應Action之間的對應關係

3) Action可能用到的參數和返回結果

4) 各類攔截器

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
   <!-- 是否開啓動態方法調用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<package name="default" namespace="/" extends="struts-default">
		 <action name="login" class="com.example.struts2.LoginAction" method="login">
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
			<result name="result">/result.jsp</result>
		</action> 
	</package>
</struts>

  

三、struts.properties

struts2框架的全局屬性文件,自動加載。該文件包含不少key-value對。

該文件徹底能夠配置在struts2.xml文件中,使用constant元素

相關文章
相關標籤/搜索