• struts2 配置详解


    一 配置详解

    1.核心控制器

    需要在web.xml中进行配置

    对框架进行初始化,以及处理所有的请求

    filter>
    	<filter-name>struts2</filter-name>
    	<filter-class>
    		org.apache.struts2.dispatcher.ng.filter.
    					StrutsPrepareAndExecuteFilter
    	</filter-class>
    </filter>	
    <filter-mapping>
    	<filter-name>struts2</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>
    
    2.struts.xml
    核心配置文件,主要负责管理Action
    通常放在WEB-INF/classes目录下,在该目录下的struts.xml文件可以被自动加载

    <struts>
      	<constant name="" value=""/>
    	<package name="" namespace="" extends="">
    		<action name="" class="">
    			<result name=""></result>			
    		</action>
    	</package>
    </struts>
    
    constant 代表常量:常量配置在核心包下:/org/apache/struts2/default.properties里面,constant元素
    配置常量,可以改变Struts 2框架的一些行为
    name属性表示常量名称,value属性表示常量值 

    <constant name="struts.i18n.encoding" value="UTF-8" />指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker、velocity的输出
    <constant name="struts.action.extension" value="do" /> 改变action的后缀
    <constant name="struts.configuration.xml.reload" value="true" />当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段最好打开,这样不用重启服务器。
    <constant name="struts.devMode" value="true" /> 开发模式下使用,这样可以打印出更详细的错误信息
    <constant name="struts.multipart.maxSize" value="10701096" /> 上传文件的大小限制,value值表示总大小,不是单个文件。
    <constant name="struts.objectFactory" value="spring"/> 与spring集成时,指定由spring负责action对象的创建
    <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
    该属性设置struts2是否支持动态方法调用,该属性的默认值是true。如需关闭,则设置value="false"。

    package元素 
    包的作用:简化维护工作,提高重用性
    包可以“继承”已定义的包,并可以添加自己包的配置
    name属性为必需的且唯一,用于指定包的名称  是自己定义的便于管理Action
    extends属性指定要扩展的包  必须要继承/struts-default.xml  struts提供的xml文件
    namespace属性定义该包中action的命名空间 ,为可选属性   就是你要访问的路径可写为“/”  访问用:http://localhost:8089//struts01/login.jsp

    若写为“/user” 访问:http://localhost:8089//struts01/user/login.jsp

    struts-default.xml 
    Struts 2默认配置文件,会自动加载
    struts-default包在struts-default.xml文件中定义
    struts-plugin.xml 
    Struts 2插件使用的配置文件 

    <struts>
      	<constant name="" value=""/>
    	<package name="default" namespace="/" extends="struts-default">
    		<action name="" class="" method="execute">
    			<result name=""></result>			
    		</action>
    	</package>
    </struts>
    

    加载顺序
    struts-default.xml→struts-plugin.xml → struts.xml 

    Action的作用封装工作单元、数据转移的场所 、返回结果字符串 。

    method属性:实现Action中不同方法的调用
    特点:
                             避免动态方法调用的安全隐患
                              导致大量的Action配置   默认method是excute()方法,但是自己可以指定调用的方法,注意不要加(),class:是调用Action的全路径,而Action是jsp页面要提交的Action,不一定和Action名字相同,因为后面class属性已经指定了要调用的类的路径。

    二 方法调用

    DMI动态方法调用:作用:减少Action数量
    使用:actionName!methodName.action 
    禁用:将属性struts.enable.DynamicMethodInvocation设置为false 必须配置常量 并且将值设为true

    struts.xml

    <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE struts PUBLIC
    	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    	"http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    <constant name="struts.configuration.xml.reload" value="true"/>
    <package name="default" namespace="/" extends="struts-default">
    	<action name="demo1Action" class="struts02.Demo1Action">
    	<result name="success">/success.jsp</result>
    	</action>
    </package>
    </struts>
    Demo1Action

    public class Demo1Action extends ActionSupport {
    	@Override
    	public String execute() throws Exception {
    	
    		return "execute";
    	}
    	public String add() throws Exception {
    		System.out.println("add");
    		return SUCCESS;
    	}
    	public String delete() throws Exception {
    		System.out.println("delete");
    		return SUCCESS;
    	}
    	public String update() throws Exception {
    		System.out.println("update");
    		return SUCCESS;
    	}
    	public String find() throws Exception {
    		System.out.println("find");
    		return SUCCESS;
    	}
    
    }
    访问:url:http://localhost:8089/struts02/demo1Action!delete

    DMI其实用的不多。
    2.第二种通配符(*)的使用
    另一种形式的动态方法调用

    <action name= "*User" 
    		class="cn.jbit.houserent.action.UserAction" method="{1}">
    	  <result>/page/{1}_success.jsp</result>
    	  <result name="input">/page/{1}.jsp</result>
    </action>
    
     /loginUser.action  

     -> method="login"
    /page/login_success.jsp
    /page/login.jsp
    2.配置默认Action
    没有Action匹配请求时,默认Action将被执行
    通过<default-action-ref … />元素配置默认Action

    <struts>
      	<default-action-ref name="defaultAction"/ >
    	<package name="default" extends="struts-default">
    		<action name="defaultAction">
    			<result>error.jsp</result>			
    		</action>
    	</package>
    </struts>
    

    Result - 常用结果类型 
    dispatcher类型
    默认结果类型,后台使用RequestDispatcher转发请求 
    redirect类型 
    后台使用的sendRedirect()将请求重定向至指定的URL 
    redirectAction类型  重定向到另一个Action

    chain:转发到另一个Action

  • 相关阅读:
    [BUUCTF]REVERSE——rsa
    windows下python3.7安装gmpy2、Crypto 库及rsa
    [BUUCTF]PWN——wustctf2020_getshell1/2
    [BUUCTF]PWN——bbys_tu_2016
    [BUUCTF]PWN——xdctf2015_pwn200
    [BUUCTF]REVERSE——[GXYCTF2019]luck_guy
    [BUUCTF]REVERSE——简单注册器
    [BUUCTF]PWN——pwnable_orw
    [BUUCTF]REVERSE——findit
    [BUUCTF]PWN——jarvisoj_level1
  • 原文地址:https://www.cnblogs.com/jatpeo/p/11767558.html
Copyright © 2020-2023  润新知