zoukankan      html  css  js  c++  java
  • Action的内容

    一:Actionresult的各种转发类型

    <action
    name="helloworld"  class="cn.itcast.action.HelloWorldAction">
        <result name="success">/WEB-INF/page/hello.jsp</result>
    </action>

    result配置类似于struts1中的forward,但struts2中提供了多种结果类型,常用的类型有: dispatcher(默认值)、 redirect 、 redirectAction 、 plainText。

    redirect

    实现传递参数 中文类型

    //中文传递 先编码处理
    this.username=URLEncoder.encode("传智博客","UTF-8");
    //获得数据处理:
    URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8"),"UTF-8")

    redirectAction:

     结果类型的例子,如果重定向的action中同一个包下

    <result type="redirectAction">helloworld</result>
    //如果重定向的action在别的命名空间下:
    <result type="redirectAction">
        <param name="actionName">helloworld</param>
        <param name="namespace">/test</param>
    </result>

    plaintext:

    显示原始文件内容,例如:当我们需要原样显示jsp文件源代码 的时候,我们可以使用此类型。

    <result name="source" type="plainText ">
        <param name="location">/xxx.jsp</param>
        <param name="charSet">UTF-8</param><!-- 指定读取文件的编码 -->
    </result>

    全局result配置:

    <global-results>
            <result name="message">/message.jsp</result>
        </global-results>

    二:为Action的属性注入值

    Struts2为Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入值。注意:属性必须提供setter方法。

    public class HelloWorldAction{
        private String savePath;
    
        public String getSavePath() {
            return savePath;
        }
        public void setSavePath(String savePath) {
            this.savePath = savePath;
        }
           ......
    }
    
    
    <package name="itcast" namespace="/test" extends="struts-default">
        
    <action name="helloworld" class="cn.itcast.action.HelloWorldAction" >
            <param name="savePath">/images</param>
            <result name="success">/WEB-INF/page/hello.jsp</result>
    </action>
    </package>


     

    上面通过<param>节点为action的savePath属性注入“/images”

    三:动态方法调用

    如果Action中存在多个方法时,我们可以使用!+方法名调用指定方法。如下:

    public class HelloWorldAction{
        private String message;
        ....
        public String execute() throws Exception{
            this.message = "我的第一个struts2应用";
            return "success";
        }
        
        public String other() throws Exception{
            this.message = "第二个方法";
            return "success";
        }
    }

    假设访问上面action的URL路径为: /struts/test/helloworld.action

    要访问action的other() 方法,我们可以这样调用:

    /struts/test/helloworld!other.action

    如果不想使用动态方法调用,我们可以通过常量struts.enable.DynamicMethodInvocation关闭动态方法调用。

    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>

    四:使用通配符定义action

    <package name="itcast" namespace="/test" extends="struts-default">
        <action name="helloworld_*" class="cn.itcast.action.HelloWorldAction" method="{1}">
            <result name="success">/WEB-INF/page/hello.jsp</result>
        </action>
    </package>
    public class HelloWorldAction{
        private String message;
        ....
        public String execute() throws Exception{
            this.message = "我的第一个struts2应用";
            return "success";
        }
        
        public String other() throws Exception{
            this.message = "第二个方法";
            return "success";
        }
    }
    
    要访问other()方法,可以通过这样的URL访问:/test/helloworld_other.action
  • 相关阅读:
    sentinel使用内置规则检测威胁——自定义规则是使用的KQL
    在Azure Sentinel中使用威胁情报——可以自己订阅,自己创建一条indicator来使用基于情报的检测
    sm2国密算法的纯c语言版本,使用于单片机平台(静态内存分配)
    JDK-8180048 : Interned string and symbol table leak memory during parallel unlinking
    CMS垃圾收集器小实验之CMSInitiatingOccupancyFraction参数
    记spring boot线上项目内存优化
    springboot 配置log4j2日志,并输出到文件
    SpringBoot 日志管理之自定义Appender
    Linux 上 定时备份postgresql 数据库的方法
    linux下执行sh脚本,提示Command not found解决办法
  • 原文地址:https://www.cnblogs.com/feimo/p/2943650.html
Copyright © 2011-2022 走看看