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

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中有多个方法时,应该如何实现调用。

【SSH】——Struts2中的动态方法调用(一)的更多相关文章

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

    当action中的方法有很多时,那应该怎么调用呢?上次我们提到的UserAction类中只有一个execute方法,如果我们需要增加用户的增删改查方法,如下: public class UserAct ...

  2. Struts2中DMI(动态方法调用)的错误问题(There is no Action mapped for namespace [/xxx] and action name [xxx!yyy] a)

    默认的Struts.xml中是这样的 <constant name="struts.enable.DynamicMethodInvocation" value="f ...

  3. Struts2学习四----------动态方法调用

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2动态方法调用 - 默认:默认执行方法中的execute方法,若指定类中没有该方法,默认返回success <package nam ...

  4. 第三章Struts2 Action中动态方法调用、通配符的使用

    01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...

  5. Struts2 Action中动态方法调用、通配符的使用

    一.Struts2执行过程图: 二.struts2配置文件的加载顺序 struts-default.xml---struts-plugin.xml---struts.xml 具体步骤: 三.Actio ...

  6. Struts2 动态方法调用

    01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...

  7. JavaWeb_(Struts2框架)struts.xml核心配置、动态方法调用、结果集的处理

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  8. struts2.3.15.3中动态方法调用默认是关闭的

    初学ssh,用的struts2.3.15.3,使用了如下表单: <form action="/spring3/index/login.action" method=" ...

  9. Struts2.5.12中动态方法调用问题

    使用版本:struts-2.5.12-all 出现问题:在开启动态方法调用后,找不到没有匹配的路径映射 <constant name="struts.enable.DynamicMet ...

随机推荐

  1. c#本地缓存实现

    用了一段时间java,java实现服务端程序很简单,有很多公共开源的组件或者软件.但是c#的很少. 现在准备自己写点东西,学习下新的东西,总结下c#的内容以及我们经常用的内容,抽离成类,组件,模型.方 ...

  2. 小a的强迫症(组合数学)

    问题描述: 小a是一名强迫症患者,现在他要给一群带颜色的珠子排成一列,现在有N种颜色,其中第i种颜色的柱子有num(i)个.要求排列中第i种颜色珠子的最后一个珠子,一定要排在第i+1种颜色的最后一个珠 ...

  3. 【模板】string中substr函数的运用

    substr有两种用法: 假设:string s = "0123456789" ;  //下标从0开始 ① string a = s.substr(5)               ...

  4. Vmware+CentOs7+共享目录

    首先下载VMware和CentOs7的镜像 安装VMware我是参考的https://www.cnblogs.com/wuyx/p/7246675.html,已经非常详细了 然后需要配置网络了 参考 ...

  5. CentOS7 安装 Docker,10分钟入门!

    本次安装是在VM虚拟机的CentOS 7环境下,仅为了学习和测试的简单安装,如果在真实生产环境还需要考虑安全策略的其他问题. 1.Linux内核版本需要 3.10.0 以上并且是64位 [root@l ...

  6. 【路由和交换之H3C自导自演】

    H3C配置自导自演 显示和维护及恢复 1:display display history-command    :查看历史命令记录 display diagnostic-information :查看 ...

  7. grep用法小结

    用法 grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] -e PATTERN ... [FILE...] grep [OPTIONS] -f FILE . ...

  8. error:control reaches end of non-void function [-Werror=return-type]

    在做LeetCode上的题目时,出现了这个错误, 原代码如下: class Solution { public: vector<int> twoSum(vector<int>& ...

  9. LeetCode:19. Remove Nth Node From End of List(Medium)

    1. 原题链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2. 题目要求 给出一个链表,请 ...

  10. React实现最完整的百度搜索框

    import React,{Component} from 'react' import ReactDOM,{render} from 'react-dom' import 'bootstrap/di ...