2019独角兽企业重金招聘Python工程师标准>>>

动态方法调用
在Struts2中动态方法调用有三种方式,动态方法调用就是为了解决一个Action对应多个请求的处理,以免Action太多

第一种方式:指定method属性
这种方式我们前面已经用到过,类似下面的配置就可以实现
<action name="chainAction" class="chapter2.action.Chapter2Action"
method="chainAction">
<result name="chainAction" type="chain">redirect</result>
</action>  
<action name="plainText" class="chapter2.action.Chapter2Action"
method="plainText">
<result name="plainText" type="plainText">/WEB-INF/JspPage/chapter2/plaintext.jsp</result>
</action>

第二种方式:感叹号方式(需要开启),官网不推荐使用这种方式,建议大家不要使用.
用这种方式需要先开启一个开关
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
将此常量设置为true,这种方式才能使用,使用见示例
Action
package chapter3.action;

public class Chapter3Action {
public String result1(){
   return "result1";
}

public String result2(){
   return "result2";
}
}

Jsp中访问方式
<body>
    <a href="${basePath}/chapter3/chapter3Action!result1">result1</a><br>
    <a href="${basePath}/chapter3/chapter3Action!result2">result2</a><br>
</body>
如果配置了后缀,必须这样写:
/chapter4/chapter4Action!create.action
XML中配置方式
<package name="chapter3" namespace="/chapter3" extends="struts-default">
   <action name="chapter3Action" class="chapter3.action.Chapter3Action">
    <result name="result1">/WEB-INF/JspPage/chapter3/result1.jsp</result>
    <result name="result2">/WEB-INF/JspPage/chapter3/result2.jsp</result>
    <result name="chapter3">/WEB-INF/JspPage/chapter3/chapter3.jsp</result>
   </action>
</package>

第三种方式:通配符方式(官网推荐使用)
首先得关闭开关
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
这一种方式是由第一种转变过来的,我们可以看到,第一种方式有很多重复的代码,那么我们可以进行变形,看下面的代码
<action name="chapter3_*" class="chapter3.action.Chapter3Action"
method="{1}">
<result name="test">/…/test.jsp</result>
</action>
chapter3_*这里的*就是你呆会要匹配的字符串,即你在后面的请求中得这样写
http://...../ chapter3_create 或 http://...../ chapter3_update
注意,这时你action中必须有create和update方法与之匹配,甚至还可以这样匹配
<action name="chapter3_*" class="chapter3.action.Chapter3Action"
method="{1}">
<result name="test">/…/{1}.jsp</result>
</action>
但是这时一定要有对应的JSP页面存在,并且相应的路径不能错,这就对我们的命名进行了强制性的规定,一定要规范.

课堂示例:
Action
public class Chapter4Action extends ActionSupport {
public String list(){
   return "list";
}

public String create(){
   return "create";
}

public String index(){
   return "index";
}
}
XML:
<action name="chapter4_*" class="chapter4.action.Chapter4Action" method="{1}">
    <result name="{1}">/WEB-INF/JspPage/chapter4/chapter4_{1}.jsp</result>
</action>

访问Servlet API
有时我们需要用到Request, Response, Session,Page, ServletContext这些我们以前常用的对象,那么在Struts2中怎么样使用到这些对象呢,通常有三种方式.
间接访问1
//向Session中放
   ActionContext.getContext().getSession().put("wdpc", "Session中的WDPC");
  
   //向request中放
   ActionContext.getContext().put("wdpc","request中的WDPC");
  
   //向application中放
   ActionContext.getContext().getApplication().put("wdpc", "Application中的WDPC");
   
取值方式:
ActionContext.getContext().getSession().get("wdpc");
间接访问2

Struts2中提供了一个静态类,他里面的方法可以获取到我们的HttpServletResponse, HttpServletRequest, 然后呢就可以还原到我们以前的使用方式了.

直接访问
虽然Struts2提供了ActionContext来访问Servlet API,但是这种方式毕竟不能直接获取Servelt API实例,为了在Action中直接访问Servlet API,Struts2还提供了一系列接口
ServletContextAware   实现此接口后,可以取得ServletContext
ServletRequestAware   实现此接口后,可以取得HttpServletRequest
ServletResponseAware 实现此接口后,可以取得HttpServletResponse
SessionAware         实现此接口后,可以取得HttpSession,注意,这里有点特殊,取得的是一个Map<String,Object> session,拦截器负责将session中存储的键值进行解析,并一一对应.

所以我们通常的做法是:
public class BaseAction implements ServletResponseAware, ServletRequestAware,
   SessionAware {

protected HttpServletResponse response;
protected HttpServletRequest request;
protected Map<String, Object> session;

public void setServletResponse(HttpServletResponse response) {
   this.response = response;
}

public void setServletRequest(HttpServletRequest request) {
   this.request = request;
}

public void setSession(Map<String, Object> session) {
   this.session = session;
}

public HttpServletResponse getResponse() {
   return response;
}

public void setResponse(HttpServletResponse response) {
   this.response = response;
}

public HttpServletRequest getRequest() {
   return request;
}

public void setRequest(HttpServletRequest request) {
   this.request = request;
}

public Map<String, Object> getSession() {
   return session;
}
}

为了让BaseAction能有验证的功能,并且不能被实例化,开发中我们会这样做:
public abstract class BaseAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware, SessionAware
然后让我们每个模块的Action来继承这个BaseAction类,然后我们就可以在Action中直接使用Servelt的API了

转载于:https://my.oschina.net/Charlie1/blog/133525

Struts2动态方法调用相关推荐

  1. Struts2 动态方法调用(十三)

    附件中有完整的案例! 1.动态方法调用 在业务逻辑处理action中,可以包含一个或者多个逻辑处理方法. 例如:在jsp文件中的同一个form表单中 有多个用来提交的表单值的按钮,可当用户通过不通的按 ...

  2. Struts2动态方法调用(DMI)的三种方法

    1.使用Action中的method属性 UserAction.java package org.bigjava.web.action;import com.opensymphony.xwork2.A ...

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

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

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

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

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

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

  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. java银行柜面发起授权功能_java银行自主柜员程序设计
  2. mysql编译参数查看_查看 apache,nginx,mysql 安装时的编译参数
  3. Quantconnect
  4. mysql.user表中Host为%的含义
  5. 【转载】Linux系统挂载NTFS文件系统
  6. Java文本框只有一行数据,Java只允许输入数目字的文本框
  7. 通过 PL/SQL Developer 建表
  8. arm-linux-gcc 硬浮点,ARMCC和GCC编译ARM代码的软浮点和硬浮点问题 【转】
  9. Django之Model操作
  10. redis linux工具安装,redis Linux版本的安装,以及一些基本的认识
  11. Win32下对多个的线程句柄的关闭的控制(上)
  12. 【洛谷2822】组合数问题(组合数的递推公式和杨辉三角)
  13. idea 搜索不到gsonformat_IDEA开发工具插件之GsonFormat
  14. 如何更改电脑IP地址 哪个IP转换器比较好用
  15. Semantic Parsing on Freebase from Question-Answer Pairs【论文笔记】
  16. Linux下通过开源软件fail2ban进行远程登录防护
  17. 人工智能数学基础-内积和外积
  18. 一起实践神经网络INT8量化系列教程(一)
  19. vue-cli中mock本地json数据踩雷:报错404 (GET http://localhost:8080/goods 404 (Not Found) )
  20. 【JavaScript 】for 循环

热门文章

  1. MySQL8.0 - 新特性 - 临时表改进
  2. 如何脱颖而出?成为优秀的人
  3. 转mosquitto auth plugin 编译配置
  4. 区块链应用 | 区块链将永久改变法律行业的七种方式
  5. Python学习笔记011——内置函数exec()
  6. Postgres多版本控制
  7. (转)WEB第三方打印控件[ASP.NET常用工具]
  8. C# 给PDF添加图片背景
  9. 树莓派安装samba共享文件
  10. DataTable转换成IList