Struts2学习四----------动态方法调用
© 版权声明:本文为博主原创文章,转载请注明出处
Struts2动态方法调用
- 默认:默认执行方法中的execute方法,若指定类中没有该方法,默认返回success
<package name="default" extends="struts-default" namespace="/">
<action name="add" class="org.struts.dynamicmethod.action.DynamicMethod">
<result>/add.jsp</result> <!-- 若没有execute方法,默认执行该result -->
<result name="error">/error.jsp</result>
</action>
</package>
- 指定method属性:执行method属性中定义的方法,没有该方法,页面报错
<package name="default" extends="struts-default" namespace="/">
<action name="add" method="add" class="org.struts.dynamicmethod.action.DynamicMethod">
<result>/add.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
- 通配符方式:使用*作用通配符,若没有配置method,默认执行execute,没有execute方法,默认返回success;
若配置method,执行method属性中定义的方法,没有该方法,则报错
<package name="default" extends="struts-default" namespace="/">
<action name="add_*" method="{1}" class="org.struts.dynamicmethod.action.DynamicMethod">
<result>/{1}.jsp</result> <!-- 若未配置method属性,并且没有execute方法,默认执行该result -->
<result name="error">/error.jsp</result>
</action>
</package>
- 感叹号方法:在!(感叹号)后面指定方法名,需在package中设置strict-method-invocation="false",并且开启动态方法调用(老版本只需开启动态方法调用即可)
<package name="default" extends="struts-default" namespace="/" strict-method-invocation="false">
<action name="add" class="org.struts.dynamicmethod.action.DynamicMethod">
<result>/add.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package> <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
实例
1.项目结构
2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.struts</groupId>
<artifactId>Struts2-DynamicMethod</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Struts2-DynamicMethod Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<!-- 统一Struts2的版本 -->
<struts.version>2.5.10</struts.version>
<!-- 统一源代码与编译的编码格式 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies> <build>
<finalName>Struts2-DynamicMethod</finalName>
</build> </project>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"> <filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
4.DynamicMethod.java
package org.struts.dynamicmethod.action; import com.opensymphony.xwork2.ActionSupport; public class DynamicMethod extends ActionSupport { private static final long serialVersionUID = 1L; public String add() { return "add"; } public String modify() { return "modify"; } public String delete() { return "delete"; } }
5.struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts> <!-- 只有感叹号动态调用方法时需设置strict-method-invocation="false" -->
<package name="dynamic" extends="struts-default" namespace="/" strict-method-invocation="false">
<!-- 默认,无execute方法,执行name="success"的result -->
<action name="search" class="org.struts.dynamicmethod.action.DynamicMethod">
<result>/search.jsp</result>
<result name="error">/error.jsp</result>
</action>
<!-- 指定method属性 -->
<action name="add" method="add" class="org.struts.dynamicmethod.action.DynamicMethod">
<result name="add">/add.jsp</result>
<result name="error">/error.jsp</result>
</action>
<!-- 感叹号方式 -->
<action name="modify" class="org.struts.dynamicmethod.action.DynamicMethod">
<result name="modify">/modify.jsp</result>
<result name="error">/error.jsp</result>
</action>
<!-- 通配符方式 -->
<action name="delete_*" method="{1}" class="org.struts.dynamicmethod.action.DynamicMethod">
<result name="delete">/{1}.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package> <!-- 开启动态方法调用,只有使用感叹号动态调用方法时需要开启 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/> </struts>
6.index.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>动态方法调用测试</title>
</head>
<body>
<a href="search">查询</a><br/>
<a href="add">新增</a><br/>
<a href="modify!modify">修改</a><br/>
<a href="delete_delete">删除</a>
</body>
</html>
7.delete.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>Insert title here</title>
</head>
<body>
This is delete.jsp
</body>
</html>
8.add.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>Insert title here</title>
</head>
<body>
This is add.jsp
</body>
</html>
9.modify.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>Insert title here</title>
</head>
<body>
This is modify.jsp
</body>
</html>
10.search.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>Insert title here</title>
</head>
<body>
This is search.jsp
</body>
</html>
11.效果预览
11.1 主界面
11.2 查询(默认方式,类中无execute方法,默认返回name="success"的result对应的界面)
11.3 新增(指定method属性,执行add方法)
11.4 修改(感叹号方式,执行modify方法)
11.5 删除(通配符方式,执行delete方法)
参考:http://www.imooc.com/video/8999
Struts2学习四----------动态方法调用的更多相关文章
- 第三章Struts2 Action中动态方法调用、通配符的使用
01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...
- Struts2 Action中动态方法调用、通配符的使用
一.Struts2执行过程图: 二.struts2配置文件的加载顺序 struts-default.xml---struts-plugin.xml---struts.xml 具体步骤: 三.Actio ...
- 【SSH】——Struts2中的动态方法调用(一)
首先我们来看一个简单的调用: 1.在web.xml中配置拦截器StrutsPrepareAndExecuteFilter.StrutsPrepareAndExecuteFilter实现了filter接 ...
- 【SSH】——Struts2中的动态方法调用(二)
当action中的方法有很多时,那应该怎么调用呢?上次我们提到的UserAction类中只有一个execute方法,如果我们需要增加用户的增删改查方法,如下: public class UserAct ...
- 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 ...
- go微服务框架go-micro深度学习(四) rpc方法调用过程详解
上一篇帖子go微服务框架go-micro深度学习(三) Registry服务的注册和发现详细解释了go-micro是如何做服务注册和发现在,服务端注册server信息,client获取server的地 ...
- Struts2学习---基本配置,action,动态方法调用,action接收参数
首先我们先来直接配置,然后再来讲原理: 第一步:jar包的引入: 我们可以到struts2的官网上下载: http://struts.apache.org/download.cgi#struts251 ...
- Struts2学习笔记 - Action篇<动态方法调用>
有三种方法可以使一个Action处理多个请求 动态方法调用DMI 定义逻辑Acton 在配置文件中使用通配符 这里就说一下Dynamic Method nvocation ,动态方法调用,什么是动态方 ...
- struts2视频学习笔记 11-12(动态方法调用,接收请求参数)
课时11 动态方法调用 如果Action中存在多个方法时,可以使用!+方法名调用指定方法.(不推荐使用) public String execute(){ setMsg("execute&q ...
随机推荐
- iOS 初始化(init、initWithNibName、initWithCoder、initWithFrame)
很多朋友如果是初学iOS开发,可能会被其中的几个加载方法给搞得晕头转向的,但是这几个方法又是作为iOS程序员必须要我们掌握的方法,下面我将对这几个方法做一下分析和对比,看看能不能增加大家对几个方法的理 ...
- Vue.js笔记 — vue-router路由懒加载
用vue.js写单页面应用时,会出现打包后的JavaScript包非常大,影响页面加载,我们可以利用路由的懒加载去优化这个问题,当我们用到某个路由后,才去加载对应的组件,这样就会更加高效,实现代码如下 ...
- BZOJ 3538 == 洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's
P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题目描述 Farmer John has recently purchased a new car online, but ...
- 教你怎么使用Windows7系统自带的备份与还原的方法
原文发布时间为:2010-09-09 -- 来源于本人的百度文章 [由搬家工具导入] 继续单击“下一步”按钮,在其后界面中检查上述备份设置是否正确,如果不正确的话可以直接单击“取消”按钮,重新设置备份 ...
- C#中的继承与覆盖
原文发布时间为:2009-03-03 -- 来源于本人的百度文章 [由搬家工具导入] //using System;//using System.Collections.Generic;using S ...
- android的布局-----FrameLayout(帧布局)
(-)帧布局简介 帧布局容器为每个加入的其中的组件创建一个空白的区域称为一帧每个子组件占据一帧,这些帧都会根据gravity的属性执行自动对齐 (二)属性 foreground:这是帧布局的前景图像 ...
- Python 函数的一般形式及参数
#!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/11/01 21:46 # @Author : lijunjiang # @Fi ...
- 洛谷 P1784 数独[DFS/回溯]
To 洛谷.1784 数独类似题:CODEVS.4966 简单数独(4*4数独) CODEVS.2924 数独挑战) 题目描述 数独是根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行 ...
- Spring中使用集成MongoDB Client启动时报错:rc: 48
一定是所在的服务器也装了MongoDB导致端口冲突,解决方法:kill掉全部MongoDB的进程. ps aux | grep mongod PID 参考: http://blog.csdn.net/ ...
- 基于Bootstrap的页面排版知识
标题: Bootstrap定义了所有HTML的标题样式,<h1>...<h6>标签或者在标签内加入.h1 class等可以得到一样的效果 效果: 副标题: 标签<smal ...