清晨

新的开始

导航

DMI(Dynamic Method Invocation) 动态方法调用

Posted on 2014-06-23 20:50  gdds  阅读(422)  评论(0)    收藏  举报

创建action,内容如下:

package action;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class A extends ActionSupport {

    public String toJsp(){

       return "success";

    }

}

配置struts.xml,内容如下:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

    <package name="default" namespace="/" extends="struts-default">

         <action name="A" class="action.A">

            <result>/hello.jsp</result>

         </action>

         <!--

             DMI(动态方法调用),可以在浏览器地址栏上动态的给出action执行的方法名称

            http://localhost:8081/Test2/A!toJsp,A是action的名称,toJsp是action中的方法                   

        -->

    </package>

</struts>

创建hello.jsp,内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">    

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

  </head>

  

  <body>

    Welcome to Struts2

  </body>

</html>

在浏览器上动态访问action的方法,如下图: