温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Struts2下怎么配置Action

发布时间:2021-08-20 03:15:44 来源:亿速云 阅读:108 作者:chen 栏目:编程语言

本篇内容主要讲解“Struts2下怎么配置Action”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Struts2下怎么配置Action”吧!

Action基本配置

Struts 2使用package包来组织Action,在struts.xml中通过使用package下的action元素来配置Action。在配置Action时,需要指定action元素的name和class属性。

  1. name属性:指定Action的名字,即指明该Action所处理的请求的URL,例如,若name属性值为login,则请求该Action的URL是login.action

  2. class属性:指定Action的实现类,该属性不是必须的,如果没有指定class属性的值,则默认使用ActionSupport类。

Action基本配置代码如下:

<package name="default" namespace="/" extends="struts-default">
  <action name="example" class="com.example.struts.action.expAction">
</package>

Action只是一个逻辑控制器,不直接对用户请求生成任何相应。因此,Action处理完用户请求后需要将指定的视图资源呈现给用户,即配置Action时,应该配置逻辑视图和物理视图资源之间的映射。

配置逻辑视图和物理视图之间的映射关系是通过<result>元素来定义的,每个<result>元素定义逻辑视图和物理视图之间的一个映射:

<package name="default" namespace="/" extends="struts-default">
  <action name="example" class="com.example.struts.action.expAction">
  <result name = "success">/success.jsp</result>
  <result name = "error">/error</result>
</package>

动态方法调用

有时一个Action内需要包含多个控制处理逻辑。例如,对于同一个表单,当用户通过不同的提交按钮进行提交时,系统需要使用Action的不同方法进行处理用户请求,此时就需要让Action中包含多个控制处理逻辑。

Struts 2框架允许一个Action中包含多个处理逻辑。在Struts 2中请求一个Action中的不同处理逻辑方法的方式成为DMI(Dynamic Method Invocation,动态方法调用),其请求格式如下:

(ActionName)!(methodName).action
  1. ActionName是Action的名字,即struts.xml中配置的Action的name属性值;

  2. methodName是Action实现类中处理逻辑的方法名。

动态方法调用示例

//访问product中的edit()方法
product!edit.action

productList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商品列表</title>
</head>
<body>
  <table border="1">
    <tr>
      <th>商品ID</th>
      <th>商品名称</th>
      <th>数量</th>
      <th colspan="2">操作</th>
    </tr>
    <tr>
      <td>1001</td>
      <td>小米手机</td>
      <td>128</td>
      <td><a href="product!edit.action?productId=1001" rel="external nofollow" >编辑</a></td>
      <td><a href="product!del.action?productId=1001" rel="external nofollow" >删除</a></td>
    </tr>
    <tr>
      <td>1002</td>
      <td>佳能相机</td>
      <td>100</td>
      <td><a href="product!edit.action?productId=1002" rel="external nofollow" >编辑</a></td>
      <td><a href="product!del.action?productId=1002" rel="external nofollow" >删除</a></td>
    </tr>
  </table>
</body>
</html>```

上述代码中,商品列表中的每个商品使用超链接进行编辑、删除操作。超链接中href属性值采用动态方法调用的方式进行链接请求,并将产品ID作为参数传递给Action。

ProductAction.java代码如下:

package com.qst.chapter03.action;

import com.opensymphony.xwork2.ActionSupport;

public class ProductAction extends ActionSupport {
  private int productId;

  public int getProductId() {
    return productId;
  }

  public void setProductId(int productId) {
    this.productId = productId;
  }

  // 编辑商品
  public String edit() {
    System.out.println("编辑商品" + productId);
    // ...省略一些编辑商品的业务
    return "edit";
  }

  // 删除商品
  public String del() {
    System.out.println("删除商品" + productId);
    // ...省略一些删除商品的业务
    return "del";
  }
}

上述代码创建了两个业务方法edit()和del()方法。当用户单击不同的链接时,系统将交给对应的方法处理。

接下来编写edit.jsp和del.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>编辑商品</title>
</head>
<body>
  ${param.productId}商品编辑
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>删除商品</title>
</head>
<body>
  ${param.productId}商品删除成功!
</body>
</html>

在struts.xml中配置ProductAction代码如下所示:

<?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>
  <!-- 指定Struts2处于开发阶段,可以进行调试 -->
  <constant name="struts.devMode" value="true" />
  <constant name="struts.enable.DynamicMethodInvocation" value="true" />

  <!-- Struts2的Action都必须配置在package里,此处使用默认package -->
  <package name="default" namespace="/" extends="struts-default">
    <!-- 定义一个名为user的Action,实现类为com.qst.chapter03.action.LoginAction -->
    <action name="product" class="com.qst.chapter03.action.ProductAction">
      <result name="edit">/edit.jsp</result>
      <result name="del">/del.jsp</result>
    </action>
  </package>

</struts>

上述配置文件配置了常量struts.enable.DynamicMethodInvocation的值为true,这样Struts 2才会开启动态方法调用,否则默认不会开启动态方法调用。

使用method属性及通配符

除了动态方法调用之外,Struts 2还提供了另一种处理方法,即将Action处理类定义成多个逻辑Action。此时,在配置<action>元素时,需要指定name、class和method属性。这样就可以让Action调用指定方法,而不是execute()方法来处理用户请求。

例如可以将ProductAction类定义成两个逻辑Action,即将该类中的edit()和del()方法映射成不同的Action,示例代码如下:

<action name="editproduct" class="com.qst.chapter03.action.ProductAction" method = "edit">
  <result name="edit">/edit.jsp</result>
</action>
<action name="delproduct" class="com.qst.chapter03.action.ProductAction" method = "del">
  <result name="del">/del.jsp</result>
</action>

上述代码定义了editproduct和delproduct两个逻辑Action,这两个Action对应的处理类都是ProductAction,但处理逻辑不同。分别对应的是edit()和del()方法。

上面的这种方式虽然能够实现,但两个定义绝大部分是相同的,带来冗余问题。Struts 2还提供了通配符“ * ”来解决这个问题。利用通配符在定义Action的name属性时使用模式字符串(即用“ * ”代表一个或多个任意字符串),接下来就可以在class、method属性以及<result>子元素中使用{N}的形式代表前面第N个星号“ * ”所匹配的子串。

* 通配符

<struts>
  <!-- 演示通配符的使用方法 -->
  <package name="product" extends="struts-default">
    <action name=" * product" class="com.qst.chapter03.action.ProductAction" method = "{1}">
      <result name="edit">/edit.jsp</result>
      <result name="del">/del.jsp</result>
    </action>
  </package>

</struts>

上述代码Action的name属性值为“ * product”,使用了通配符,此时定义的不是一个普通的Action,而是定义了一系列的逻辑Action,只要用户请求的URL符合“ * product.action”的模式,都可以通过ProductAction处理。此外,必须要指定method属性,method属性用于指定用户请求的方法。在method属性中使用表达式{1},代表该表达式就是name属性值中第一个“ * ”指代的值。通过上述配置规则可以达到与动态调用同样的运行效果。

此外Struts 2允许在class属性和method属性中同时使用表达式,例如:

复制代码 代码如下:


<action name = " *_* " class = "com.qst.chapter03,action.{1}Action" method = " {2} ">

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

到此,相信大家对“Struts2下怎么配置Action”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI