zoukankan      html  css  js  c++  java
  • Struts2中动态方法的调用

      Struts2中动态方法调用就是为了解决一个action对应多个请求的处理,以免action太多。

      主要有一下三种方法:指定method属性、感叹号方式和通配符方式。推荐使用第三种方式。

      1.指定method属性

        LoginAction.java

    public class LoginAction extends ActionSupport{
         public String execute (){
               
                return  "success" ;
         }
         public String add(){
                return "add" ;
         }
         public String update(){
                return "update" ;
         }
    }

        struts.xml,配置多个action标签。

    <struts >
        <package name= "default" namespace ="/" extends="struts-default" >
            <action name= "LoginAction" class="com.struts2.action.LoginAction" method ="execute" >
                <result name= "success">/success.jsp </result>
            </action>
             <action name= "addAction" class="com.struts2.action.LoginAction" method ="add" >
                <result name= "add">/add.jsp </result>
            </action>
             <action name= "updateAction" class="com.struts2.action.LoginAction" method ="update" >
                <result name= "update">/update.jsp </result>
            </action>
        </package>
    </struts >

       当访问路径为http://localhost:8088/jspToAction1/LoginAction.action时,跳转到success.jsp

       当访问路径为http://localhost:8088/jspToAction1/addAction.action时,跳转到add.jsp

         当访问路径为http://localhost:8088/jspToAction1/updateAction.action时,跳转到update.jsp

      

      2.感叹号方式

      LoginAction.java不变如1所示。

      struts.xml

      struts.enable.DynamicMethodInvocation = true 表示动态方法调用

    <struts >
        <package name= "default" namespace ="/" extends="struts-default" >
            <action name= "LoginAction" class="com.struts2.action.LoginAction" method ="execute" >
                <result name= "success">/success.jsp </result>
                <result name= "add">/add.jsp </result>
                 <result name= "update">/update.jsp </result>
            </action>
        </package>
        <constant name= "struts.enable.DynamicMethodInvocation" value="true" ></constant>
    </struts >

      

      访问链接时:http://localhost:8080/struts2Demo/LoginAction!update.action,跳转到update.jsp

      访问链接时:http://localhost:8080/struts2Demo/LoginAction!add.action,跳转到add.jsp

      3.通配符方式(最常用的方式也是struts官方推荐的方式)

      LoginAction.java同样不变,struts.xml改为如下:

    <struts >
        <package name= "default" namespace ="/" extends="struts-default" >
    <!--    {1}通过*的方式传递进来 -->
            <action name= "LoginAction_*" class="com.struts2.action.LoginAction" method ="{1}" >
                <result name= "success">/{1}.jsp</result>
                <result name= "add">/{1}.jsp</result>
                 <result name= "update">/{1}.jsp</result>
            </action>
        </package>
        <constant name= "struts.enable.DynamicMethodInvocation" value="false" ></constant>
    </struts >
      "LoginAction_*"中的“*”就代表{1},通过链接的方式进行匹配、传值,从而达到一个动态访问的效果。如果name=""中有多个”*“,则第一个用{1}来表示,第二个有{2}来表示,以此类推。
      访问链接时: http://localhost:8088/jspToAction1/HelloWorld_add.action 跳转到add.jsp
      访问链接时: http://localhost:8088/jspToAction1/HelloWorld_update.action   跳转到update.jsp
      

    **************************************************************************************************

      如若转载请注明出处,谢谢。By奋斗的小蘑菇

    by奋斗的小蘑菇
  • 相关阅读:
    VB.NET 操作Json 包括嵌套的
    c#和VB混用出现的错误
    System.ArgumentException: 另一个SqlParameterCollection中已包含SqlParameter。
    centos vsftpd 安装配置
    linux centos apache+php+mysql 安装( 用包安装 非yum 安装)
    迟到的一笔 php 5 apache 2.2 webservice 创建与配置
    linux centos apache+php+mysql 安装
    php 不能连接数据库 php error Can't connect to local MySQL server through socket '/tmp/mysql.sock'
    内置过渡动画
    Input 输入框
  • 原文地址:https://www.cnblogs.com/liyuchen/p/4881630.html
Copyright © 2011-2022 走看看