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

  • 相关阅读:
    vs调试技巧
    ubuntu中安装Docker
    Docker的简单认知
    mysql笔记01
    5款实用的硬盘、SSD固态硬盘、U盘、储存卡磁盘性能测试工具绿色版
    怎么用HD Tune检测硬盘坏道
    Dreamweaver_CS6安装与破解
    Microsoft Office 2013 (64位) 免费完整版(安装 + 激活)
    Microsoft office2007免费版下载(安装 + 破解)
    OneNote如何使用
  • 原文地址:https://www.cnblogs.com/linlf03/p/11462424.html
Copyright © 2011-2022 走看看