1 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>struts2静态方法和动态方法调用</title></head><body><a href="${pageContext.request.contextPath }/methodLogin.a">通过静态方法调用进入login</a><br /><a href="${pageContext.request.contextPath }/methodLoginA.b">通过静态方法调用进入loginA</a><br /><a href="${pageContext.request.contextPath }/methodDync!login.action">通过动态方法调用进入login</a><br /><a href="${pageContext.request.contextPath }/methodDync!loginA.action">通过动态方法调用进入loginA</a><br /><a href="${pageContext.request.contextPath }/methodDync!loginB.action">通过动态方法调用进入loginB</a><br /></body>
</html>

2 method.xml页面(必须在struts2.xml里定义,定义的格式为:<include file="method.xml"></include>)

<?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><package name="methodPackage" extends="struts-default"><!-- 定义了两个静态方法      method是指定跳转到类的某个自定义方法-->      <action name="methodLogin" class="cn.etop.struts2.method.MethodAction"  method="login"><result name="test">/method/success.jsp</result></action><action name="methodLoginA" class="cn.etop.struts2.method.MethodAction" method="loginA"><result name="test">/method/success.jsp</result></action><!-- 定义了一动态方法,但在jsp请求路径中,必须在类名后加“!要执行的方法名”如:action的name为:login,动态执行的方法为test,那么jsp的路径为:login!test.action开启动态方法有三种方式第一种:在web.xml里配置参数<init-param><param-name>struts.enable.DynamicMethodInvocation</param-name><param-value>true</param-value></init-param> 第二种:在src下创建一个属性文件:struts.properties(名字struts是固定的,不能是别的名字) struts.enable.DynamicMethodInvocation = true第三种:在struts.xml里配置参数<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>                        --><action name="methodDync" class="cn.etop.struts2.method.MethodAction" ><result name="test">/method/success.jsp</result></action></package></struts>

3 java代码

package cn.etop.struts2.method;import java.io.IOException;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/*** 测试静态方法和动态方法* 自定义从xml请求的路径执行的方法* * @author Administrator**/
public class MethodAction extends ActionSupport {public String login(){HttpServletResponse response=ServletActionContext.getResponse();response.setContentType("text/html;charset=utf-8");try {response.getWriter().print("方法login");} catch (IOException e) {e.printStackTrace();}return null;  }public String loginA(){HttpServletResponse response=ServletActionContext.getResponse();response.setContentType("text/html;charset=utf-8");try {response.getWriter().print("方法loginA");} catch (IOException e) {e.printStackTrace();}return null;  }public String loginB(){HttpServletResponse response=ServletActionContext.getResponse();response.setContentType("text/html;charset=utf-8");try {response.getWriter().print("方法loginB");} catch (IOException e) {e.printStackTrace();}return null;  }public String loginC(){HttpServletResponse response=ServletActionContext.getResponse();response.setContentType("text/html;charset=utf-8");try {response.getWriter().print("方法loginC");} catch (IOException e) {e.printStackTrace();}return null;  }
}

4 在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"><!-- 定义Struts 2的FilterDispatcher的Filter --><filter><!-- 定义核心Filter的名字 --><filter-name>struts2</filter-name><!-- 定义核心Filter的实现类 --><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><!-- 配置动态方法 --><init-param><param-name>struts.enable.DynamicMethodInvocation</param-name><param-value>true</param-value></init-param></filter><!-- FilterDispatcher用来初始化Struts 2并且处理所有的Web请求 --><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>

5 在struts2.properties里配置动态方法

#设置是否启动动态方法
struts.enable.DynamicMethodInvocation = true
#设置struts2的后缀
struts.action.extension=action,a,b
#设置是否启动开发模式
struts.devMode = true
#设置国际化属性文件的basename
struts.custom.i18n.resources=message
#设置上传文件的大小
struts.multipart.maxSize=20971520

6 在struts2.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>
<!-- 表示自动扫描包结构中包含配置的字符 并带有注解的action--><constant name="struts.convention.package.locators" value="action,actions,struts,struts2"/><!-- 表示自动扫描以某个后缀结尾的类 要求必须带有注解 --><constant name="struts.convention.action.suffix" value="Action"/><!-- 表示从哪个包开始扫描 --><constant name="struts.convention.package.locators.basePackage" value="cn.et"/><!-- include是其他的xml必须在Struts里定义,相当于继承 -->  <include file="method.xml"></include>
</struts>

转载于:https://www.cnblogs.com/t0404/p/10290981.html

struts2静态方法和动态方法调用相关推荐

  1. Struts2使用!动态方法调用无效

    在Struts2的action配置中,可以使用method属性来实现方法的动态调用,除此之外还有一种方式可以实现方法的动态调用,那就是在url中 的action后跟!再跟指定的方法名,比如localh ...

  2. 【SSH】——Struts2中的动态方法调用(一)

    首先我们来看一个简单的调用: 1.在web.xml中配置拦截器StrutsPrepareAndExecuteFilter.StrutsPrepareAndExecuteFilter实现了filter接 ...

  3. JAVA框架——struts(一)struts快速入门,struts访问流程,struts配置文件详解,动态方法调用

    一. Struts2框架概述 是一种基于MVC模式的轻量级web框架.本质是一个Servlet.作为控制器建立模型与视图的数据交互.Struts2以WebWord为核心,采用拦截器的机制处理客户的请求 ...

  4. struts2学习 - action -3 动态方法调用 DMI

    Action执行的时候并不一定要执行execute方法 可以在配置文件中配置Action的时候用method=来指定执行哪个方法 也可以在url地址中动态指定(动态方法调用DMI)(推荐)   配置文 ...

  5. Struts2动态方法调用

    2019独角兽企业重金招聘Python工程师标准>>> 动态方法调用 在Struts2中动态方法调用有三种方式,动态方法调用就是为了解决一个Action对应多个请求的处理,以免Act ...

  6. Struts2学习---基本配置,action,动态方法调用,action接收参数

    首先我们先来直接配置,然后再来讲原理:  第一步:jar包的引入:  我们可以到struts2的官网上下载:  http://struts.apache.org/download.cgi#struts ...

  7. struts2的通配符和动态方法调用

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC     &quo ...

  8. struts2的动态方法调用(DMI)和通配符映射

    动态方法调用 1.Struts2默认关闭DMI功能,需要使用需要手动打开,配置常量 [html] view plaincopy struts.enable.DynamicMethodInvocatio ...

  9. Struts2笔记——通配符和动态方法调用

     通配符映射 * 一个 Web应用可能有成百上千个 action 声明. 可以利用 struts提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系 * 通配符映射规则     > 若 ...

最新文章

  1. python 遍历字典
  2. luoguP4206 [NOI2005]聪聪与可可 期望概率DP
  3. php文本分割成csv,php将文本文件转换csv输出的方法
  4. 联机分析的列式数据库 clickHouse
  5. python 编辑数学公式_用python编写数学公式
  6. 前端学习(2769):发送网络请求
  7. 卖身字节跳动的互动百科或被改名
  8. java酒店信息管理系统_java实现酒店管理系统
  9. rac one node在线relocation
  10. C++算法学习(力扣:1544. 整理字符串)
  11. 21点游戏java实现
  12. Linux内核通知链(Notifier)
  13. mqtt简介及在web端的应用(接入阿里iot)
  14. python Django 快捷键
  15. 力扣 643. 子数组最大平均数 I 滑动窗口
  16. 【解决方案】快递代收点部署视频监控,EasyCVR视频融合平台来助力
  17. 怎么做才可以把电脑上的照片给做成视频?-markdown编辑器
  18. 【号外20191127】微软刚刚更新了snipping tool
  19. 5GC PDU Session Establishment PDU会话建立流程
  20. 小骥autorun专杀工具原理初探

热门文章

  1. 获得本页面URL地址
  2. 从零开发一个 Java Web 项目要点
  3. Istio究竟是干嘛的?
  4. 分库分表?如何做到永不迁移数据和避免热点?
  5. 如何使用 Java 生成二维码?
  6. 秒杀系统必须考虑的 3 个技术问题!
  7. 全面对比 Redis 和 Memcached 的 6 点区别
  8. 记一次Java动态代理实践
  9. 我在系统设计上犯过的14个错
  10. 记录一次壮烈牺牲的阿里巴巴面试