zoukankan      html  css  js  c++  java
  • Struts2工作原理和核心文件

    一、Struts2工作原理

    如下图:

    二、Struts2配置文件

    1、web.xml

    任何MVC框架都需要与Web应用整合,这就不得不借助于web.xml文件,只有配置了web.xml文件的Servlet才会被应用加载

    通常,所有的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>
    

      

    2、struts.xml

    struts2的核心配置文件,负责管理应用中的Action映射,以及该Action包含的Result定义等。

    内容包括:

    1) 全局属性

    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>
    

      

    3、struts.properties

    struts2框架的全局属性文件,自动加载。该文件包含很多key-value对。

    该文件完全可以配置在struts2.xml文件中,使用constant元素

  • 相关阅读:
    【gulp】Gulp的安装和配置 及 系列插件
    python函数:装饰器、修正、语法糖、有参装饰器、global与nonlocal
    python函数:函数参数、对象、嵌套、闭包与名称空间、作用域
    python函数:函数阶段练习
    python函数:函数使用原则、定义与调用形式
    python文件操作:文件指针移动、修改
    python文件操作:文件处理与操作模式
    python文件操作:文件处理案例
    python文件操作:字符编码与文件处理
    python基础:数据类型二
  • 原文地址:https://www.cnblogs.com/linlf03/p/11462424.html
Copyright © 2011-2022 走看看