Struts2学习笔记—DMI(DynamicMethodInvocation)

      今天学习了Struts2的动态方法(DMI)在这里做成笔记供给大家学习。

在struts2中默认情况下, Struts 的动态方法调用处于禁用状态 ,如果想用name必须配置struts2中的<constant>标签,配置如下:<constant name="struts.enable.DynamicMethodInvocation" value="true"/>,如果不想使用动态方法调用,我们可以通过常量struts.enable.DynamicMethodInvocation关闭动态方法调用。
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>,也就是把value的值改为false即可;


第一步:需要导入struts2的jar


下面我就下一个简单的例子供大家参考:

index.jsp:


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<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>

Action执行的时候并不一定要执行execute方法<br /> 

 可以在配置文件中配置Action的时候用method=来指定执行哪个方法  

也可以在url地址中动态指定(动态方法调用DMI)(推荐)<br /> 


   <a href="user">Struts2</a>
   
     <a  href="login!add">调用add方法</a>


   <a  href="login!dell">调用dell方法</a>
   
  </body>
</html>





web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  
  <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>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3.32//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>


<!--这是需要配置的constant 标签-->

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

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

<!--这是一般的调取method-->

<action name="user" class="com.tang.Struts2Action" method="userhello">
<result name="userhello">/HelloWorld.jsp</result>
</action>
<!--这是struts2的动态方法-->
<action name="login"  class="com.tang.Struts2Action">
           <result name="add">/add.jsp</result>
           <result name="dell">/dell.jsp</result>

     </action>
</package>


</struts>


Struts2Action.java



public class Struts2Action {

public String userhello(){


return "userhello";
}

public  String  add()


{


 return "add";


}


public  String dell()


{


 return "dell";


}


}



以上就是最简单的动态方法(DMI),点击index.jsp上的任意超链接,都可以跳到不同的页面去,你们也可以在上面在家一些复杂的逻辑。



下节我们看一下struts2 的通配符


猜你喜欢

转载自blog.csdn.net/qq_35449428/article/details/78292921