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元素