1 通过method配置(有点low)

  • 建立前端JSP:demo4.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>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Action的配置方法1:method配置</title>
 </head>
 <body>
 <h1>Action的访问</h1>
 <h3>1通过method的方式</h3>
 <a href="${pageContext.request.contextPath }/userFind1.act">查找用户</a><br/>
 <a href="${pageContext.request.contextPath }/userUpdate1.act">修改用户</a><br/>
 <a href="${pageContext.request.contextPath }/userDelete1.act">删除用户</a><br/>
 <a href="${pageContext.request.contextPath }/userSave1.act">保存用户</a><br/>
 </body>
 </html>
  • 建立与之对应的Action类,采用继承ActionSupport的方式
import com.opensymphony.xwork2.ActionSupport;

/**
 * Action访问方式一:method配置
 * return NONE 不跳转
 */
public class UserAction1 extends ActionSupport {
    public String find1(){
        System.out.println("userFind1....");
        return NONE;
    }
    public String update1(){
        System.out.println("userUpdate1....");
        return NONE;
    }
    public String delete1(){
        System.out.println("userDelete1....");
        return NONE;
    }
    public String save1(){
        System.out.println("userSave1....");
        return NONE;
    }
}
  • 建立配置文件structs_demo4.jsp
<?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">
<!-- 通过method配置-->
<struts>
<package name="demo4" extends="struts-default" namespace="/">
<action name="userFind1" class="com.itheima.Structs.demo4.UserAction1" method="find1"></action>
<action name="userUpdate1" class="com.itheima.Structs.demo4.UserAction1" method="update1"></action>
<action name="userDelete1" class="com.itheima.Structs.demo4.UserAction1" method="delete1"></action>
<action name="userSave1" class="com.itheima.Structs.demo4.UserAction1" method="save1"></action>
</package>
</struts>
  • 将配置文件用include标签添加到struts.xml文件中

<include file="com/itheima/Structs/demo4/struts_demo4.xml"></include>

2 通过通配符的方式(开发常用)

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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Action访问方式2:通配符</title>
</head>
<body>
<h3><a href="${pageContext.request.contextPath }/product_find1.action">查询商品</a></h3><br/>
<h3><a href="${pageContext.request.contextPath }/product_update1.action">更新商品</a></h3><br/>
<h3><a href="${pageContext.request.contextPath }/product_delete1.action">删除商品</a></h3><br/>
<h3><a href="${pageContext.request.contextPath }/product_save1.action">保存商品</a></h3><br/>
</body>
</html>

Action类:

package com.itheima.Structs.demo4;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 通配符配置Action
 *
 */
public class ProductAction1 extends ActionSupport {
    public String find1(){
        System.out.println("查询商品");
        return NONE;
    }
    public String update1(){
        System.out.println("更新商品");
        return NONE;
    }
    public String delete1(){
        System.out.println("删除商品");
        return NONE;
    }
    public String save1(){
        System.out.println("保存商品");
        return NONE;
    }
}

配置文件:

<?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 name="product_*" class="com.itheima.Structs.demo4.ProductAction1" method="{1}"></action>
</package>
</struts>

3 通过动态配置的方式

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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动态方法配置Action</title>
</head>
<body>
<h3>动态方法配置Action</h3>
<h3><a href="${pageContext.request.contextPath }/customer!find.action">查询客户</a></h3>
<h3><a href="${pageContext.request.contextPath }/customer!update.action">更新客户</a></h3>
<h3><a href="${pageContext.request.contextPath }/customer!delete.action">删除客户</a></h3>
<h3><a href="${pageContext.request.contextPath }/customer!save.action">保存客户</a></h3>
</body>
</html>

Action类:

package com.itheima.Structs.demo4;

import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction1 extends ActionSupport {
     public String find(){
         System.out.println("用户查询");
         return NONE;
     }
     public String update(){
         System.out.println("用户更新");
         return NONE;
     }
     public String delete(){
         System.out.println("用户访问");
         return NONE;
     }
     public String save(){
         System.out.println("用户保存");
         return NONE;
     }
}

配置文件:

<?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>
<!--开启动态方法  -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="demo4" extends="struts-default" namespace="/">
<!-- 动态方法 -->
<action name="customer"
class="com.itheima.Structs.demo4.CustomerAction1"></action>
</package>
</struts>

五 Action访问方法,method配置,通配符(常用),动态的更多相关文章

  1. struts 中自定义action访问方法

    struts中action类继承了ActionSupport  默认实现了execute()方法 struts.xml配置文件中 然后可以配置如下映射: <package name =" ...

  2. Struts2学习笔记(五)——Action访问Servlet API

    在Strut2中访问Servlet API有三种方式: 1.通过ActionContext访问Servlet API,推荐使用这种,但是这种方案它获取的不是真正的事Servlet API. 步骤: 1 ...

  3. struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  4. 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  5. {Django基础十之Form和ModelForm组件}一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 ModelForm

    Django基础十之Form和ModelForm组件 本节目录 一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 Model ...

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

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

  7. struts配置通配符*来匹配方法,实现动态调用

    01:web.xml中配置,启动struts2 <?xml version="1.0" encoding="UTF-8"?> <web-app ...

  8. Struts(五)Action的访问

    在struts开发中,Action作为框架的核心类,实现对用户的请求的处理,Action被称为业务逻辑控制器.一个Action类代表一次请求或调用.Action就是用来处理一次用户请求的对象 Acti ...

  9. struts2 正确配置通配符方式访问,报错解决

    今天遇到正确配置通配符访问action的方法,但是还是报错,原因struts 2.3 以后会内部会验证是否允许该方法,而我用的刚好是2.5的版本 要action配置中加上<allowed-met ...

随机推荐

  1. 其他 - 02. poolmon 安装

    1. 概述 遇到 win10 的内存泄露 32G 内存都能给吃光 2. 思路 rammap 对整体内存做一个诊断 主要是内存分配 用途 状态 poolmon 确认内存的用途 比 rammap 更精确 ...

  2. C语言实例-大小写字母间的转换

    初学C语言都会遇到要求写大小写转换的题目 这类题目主要通过ASCII(美国信息交换标准代码)码差值实现,A对应ASCII码十进制数字是65,a对应ASCII码十进制数字是97,即大小写字母之间ASCI ...

  3. jmeter实现SMTP邮件协议压测

    实现目的 通过jmeter的SMTP取样器,调用SMTP协议,批量进行邮件的发送,已达到压测的目的. 脚本实现 User Defined Variables定义用户变量 编辑SMTP Sampler取 ...

  4. winform 多个datagridview 之间同步滚动

    1.添加Scroll事件 2.注意需判断数据长度,避免溢出 private void dgYY_Scroll(object sender, ScrollEventArgs e) { ) { dgFee ...

  5. 蓝桥杯2016年省赛C/C++大学A组

    网友年龄 某君新认识一网友. 当问及年龄时,他的网友说: "我的年龄是个2位数,我比儿子大27岁, 如果把我的年龄的两位数字交换位置,刚好就是我儿子的年龄" 请你计算:网友的年龄一 ...

  6. Springmvc-crud-06(路径忘记加上“/”错误)

    错误: 原因:自己马虎忘记加"  /  ",罚继续写代码┭┮﹏┭┮ 前端代码: <h1>添加功能</h1> <form action="te ...

  7. esp8266(wifi)模块调试记录

    1.要注意usb转TTL接口上的晶振 如果晶振是12Mhz,可能就收不到反馈,因为12Mhz波特率会有误差.

  8. JAVA基础学习(6)之使用对象

    6使用对象 6.1字符类型 6.1.1字符类型 char和int互相转换 //a比A大32 Scanner in=new Scanner(System.in); char c='B'; char c1 ...

  9. 牛客新年AK场之模拟二维数组

    链接:https://ac.nowcoder.com/acm/contest/3800/D来源:牛客网 题目描述 Rinne 喜欢使用一种奇怪的方法背单词,现在这些单词被放在了一个 n×mn \tim ...

  10. 【Python与线程】

    "   目录 一.全局解释器锁GIL 二.Python线程模块的选择 三.线程的创建 三.锁机制 四.信号量 五.事件 六.条件 七.定时器 八.线程队列 九.线程池 补充:线程安全 imp ...