首先我们来看一个简单的调用:

1、在web.xml中配置拦截器StrutsPrepareAndExecuteFilter。StrutsPrepareAndExecuteFilter实现了filter接口,在执行action之前,利用filter做一些操作。

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

2、提供Struts2的配置文件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="Struts2_006" extends="struts-default" ><action name="user" class="com.struts2.UserAction"><result>/add_success.jsp</result></action></package></struts>

注:<result>标签的默认值是success,此处省略。

3、页面显示部分。

index.jsp页面,转向到action中,调用action中的方法。

<body><a href="user.action">调用</a>
</body>

调用完后,跳转到成功页面,并显示message中的消息。

 <body>我的操作:${message} <br></body>

4、编写Action类 UserAction。

public class UserAction extends ActionSupport{//消息字符串,用来显示调用结果private String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}/**** execute方法*/public String execute() throws Exception{message="执行execute方法";return SUCCESS;}
}

注意:这里我们让UserAction继承自ActionSupport类,从源码中可以看到ActionSupport类实现了Action接口。在ActionSupport类中也处理了execute()方法,但他并没有做什么操作,只是返回SUCCESS。因而,如果我们在UserAction中不写execute方法,也不会报错。

public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {protected static Logger LOG = LoggerFactory.getLogger(ActionSupport.class);private final ValidationAwareSupport validationAware = new ValidationAwareSupport();private transient TextProvider textProvider;private Container container;/*** A default implementation that does nothing an returns "success".* <p/>* Subclasses should override this method to provide their business logic.* <p/>* See also {@link com.opensymphony.xwork2.Action#execute()}.** @return returns {@link #SUCCESS}* @throws Exception can be thrown by subclasses.*/public String execute() throws Exception {return SUCCESS;}
}

如果在UserAction中不写execute方法,message中没有值。

这篇博客介绍了Struts2的简单的方法调用,下篇博客将继续介绍,当action中有多个方法时,应该如何实现调用。

转载于:https://www.cnblogs.com/saixing/p/6730265.html

【SSH】——Struts2中的动态方法调用(一)相关推荐

  1. struts2静态方法和动态方法调用

    1 jsp页面 <%@ page language="java" import="java.util.*" pageEncoding="UTF- ...

  2. action中的动态方法调用

    action中的动态方法调用有两种形似 一.不指定method方法 <%@page contentType="text/html" pageEncoding="UT ...

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

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

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

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

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

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

  6. Struts2动态方法调用

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

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

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

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

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

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

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

最新文章

  1. 整合hive基于cdh6_Flink 作为现代数据仓库的统一引擎:Hive 集成生产就绪!
  2. winform中构造函数与Form_Load
  3. ExtJs FormPanel布局
  4. xdebug怎样在php中配置,教你在PHPStorm中配置Xdebug
  5. 利用 Docker 在不同宿主机做 CentOS 系统容器 | 原力计划
  6. python格式化读取文件_python文本读写与格式化
  7. java编程计算加减乘除_Java程序完成加减乘除四则运算
  8. Delphi中Chrome Chromium、Cef3学习笔记(三)
  9. 零基础学python还是c语言-为什么大一先学C语言,而不是Python,你知道为什么吗?...
  10. 关于 html 中 table 表格 tr,td 的高度和宽度
  11. java源码编译指令
  12. 竞品分析文档撰写总结
  13. 双三次插值算法(bicubic interpolation)与图形学和计算方法的关系
  14. 【352】矩阵转置性质
  15. java设置列宽_java用POI设置Excel的列宽
  16. QQ公众号微信公众号,左右互搏?
  17. python爬取下厨房网站首页图片request+bs4
  18. OpenJDK源码赏析之四(jli_util中的工具函数)
  19. TCPIP详解Protocol 读书笔记(八) Traceroute程序
  20. codecamp_波特兰(和华盛顿西南部)CodeCamp 2006

热门文章

  1. error C2146: syntax error : missing ';' before identifier 'fd'
  2. Win7下使用DbgPrint
  3. LeetCode每日一题 116. 填充每个节点的下一个右侧节点指针
  4. php 正则获取某个div,php正则匹配html中带class的div并选取其中内容的方法
  5. Ubuntu : 未找到 ‘nm-tool‘ 命令
  6. 人工神经网络_验证码破译(数据挖掘入门与实践-实验9)
  7. c语言 指针 数组还能这么用 ?
  8. vant组件搜索并选择_借助PARTsolutions 选型助手,轻松快速地找到组件。
  9. linux tomcat 开启apr,tomcat开启APR
  10. svn switch 导致个别文件switch失败