在Struts2中方法调用概括起来主要有三种形式

第一种方式:指定method属性

<action name="heroAction" class="com.ABC123.HeroAction" method="add">
<result name="add">/add.jsp</result>
</action>

  这样Struts2就会调用heroAction中的add方法。

第二种方式:动态方法调用(DMI)

用这种方法需要设置一个常量

struts2的2.3以上版本的jar包 必须配置DMI

<constant name="struts.enable.DynamicMethodInvocation" value="true" />

  

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <package name="goToEat" namespace="/" extends="struts-default"> <action name="heroAction" class="com.ABC123.action.HeroAction" >
<result name="list" >/list.jsp</result>
<result name="gotoupd" >/upd.jsp</result> <!-- 重定向-->
<result name="add" type="redirect">heroAction!list.action</result> </action> </package> </struts>

  动态方法调用是指表单元素的action并不是直接等于某个Action的名字,而是以如下形式来指定Form的action属性

<!-- action属性为action!methodName的形式 -->
action = "action!methodName.action"

  在struts.xml中定义如下Action

<package name="goToEat" namespace="/" extends="struts-default">

	<action name="heroAction" class="com.ABC123.action.HeroAction" >
<result name="list" >/list.jsp</result>
<result name="gotoupd" >/upd.jsp</result> <!-- 重定向-->
<result name="add" type="redirect">heroAction!list.action</result> </action> </package>

  HeroAction中的方法为

package com.ABC123.action;

import java.util.ArrayList;
import java.util.List; import com.ABC123.pojo.Hero; public class HeroAction { private static List<Hero> heroList=new ArrayList<Hero>(); private Hero hero; private int id; static{
heroList.add(new Hero(1, "德玛", 12, "德玛西亚", "很难哦哦"));
heroList.add(new Hero(2, "艾希", 45, "德玛西亚", "很厉害的啊"));
heroList.add(new Hero(3, "亚索", 78, "小学生", "很牛逼"));
} public String list() { return "list"; } //增加
public String add() { System.out.println(hero); heroList.add(hero);
return "add"; } //删除
public String del() { System.out.println("删除的下标===>"+id); Hero hero2 = heroList.get(id); System.out.println(hero2); heroList.remove(hero2);
return "add";
} public String goToUpd() { Hero hero5 = heroList.get(id);
System.out.println("该条点击修改的的hero==>"+hero5); return "gotoupd"; } //修改
public String upd() {
System.out.println(hero.getId());
int id2 = hero.getId();
id2-=1;
System.out.println("id2===>"+id2); //修改前
Hero hero5 = heroList.get(id2);
System.out.println("该条点击修改的的hero==>"+hero5); //接到的值为hero hero5.setAge(hero.getAge()); hero5.setName(hero.getName()); hero5.setStory(hero.getStory()); hero5.setZhenying(hero.getZhenying()); // heroList.set(id2, hero); System.out.println("修改后的hero==>"+hero);
return "add";
} //================================ public List<Hero> getHeroList() {
return heroList;
} public void setHeroList(List<Hero> heroList) {
this.heroList = heroList;
} public Hero getHero() {
return hero;
} public void setHero(Hero hero) {
this.hero = hero;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} }

  则在JSP中用如下方式调用方法

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
<a href="/struts-01/add.jsp">去添加</a>
</br>
</br>
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
<td>zhenying</td>
<td>story</td>
<td colspan="2">操作</td>
</tr> <c:forEach items="${heroList }" var="hero">
<tr>
<td>${hero.id }</td>
<td>${hero.name }</td>
<td>${hero.age }</td>
<td>${hero.zhenying }</td>
<td>${hero.story }</td>
<td><a href="heroAction!goToUpd.action?id=${hero.id -1}">修改</a></td>
<td><a href="heroAction!del.action?id=${hero.id -1 }">删除</a></td>
</tr>
</c:forEach>
</table> <hr> <a href="/struts-01/add.jsp">去添加</a>
</br>
</br>
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
<td>zhenying</td>
<td>story</td>
<td colspan="2">操作</td>
</tr> <s:iterator value="heroList"> <tr>
<td><s:property value="id"/></td>
<td><s:property value="name "/></td>
<td><s:property value="age "/></td>
<td><s:property value="zhenying "/></td>
<td><s:property value="story "/></td>
<td><a href="heroAction!goToUpd.action?id=<s:property value='id-1'/>">修改</a></td>
<td><a href="heroAction!del.action?id=<s:property value='id-1'/>">删除</a></td>
</tr>
</s:iterator> </table> </body>
</html>

  第三种方式:通配符(推荐使用)

<action name="heroAction_*" class="com.ABC123.action.HeroAction" method="{1}">
<result name="list" >/list.jsp</result>
</action>

  这里的{1} 相当于传进来字符串进行拼接地址 比如传进来list 拼接为/list.jsp

<action name="heroAction_*" class="com.ABC123.action.HeroAction" method="{1}">
<result name="{1}" >/{1}.jsp</result>
</action>

  

<a href="heroAction_list.action">查询
<a href="heroAction_add.action">增加

  

heroAction_list.action就会调用heroAction中的list方法 然后跳转到list.jsp
heroAction_add.action就会调用heroAction中的add方法 然后跳转到add.jsp

 

Struts2方法调用的三种方式(有新的!调用方法的说明)的更多相关文章

  1. Struts2方法调用的三种方式

    在Struts2中方法调用概括起来主要有三种形式 第一种方式:指定method属性 <action name="student" class="com.itmyho ...

  2. Struts2获取Session的三种方式

    1.Map<String,Object> session =  ActionContext.getContext().getSession(); session.put("cod ...

  3. 【Struts2】Struts2获取session的三种方式

    1.Map<String,Object> map =  ActionContext.getContext().getSession(); 2.HttpSession session = S ...

  4. [OpenSource]浅谈.Net和Java互相调用的三种方式

    在很多的大型系统开发中,开发工具往往不限制于同一种开发语言,而是会使用多种开发语言的混合型开发.目前Java和.Net都声称自己占85%的市场份额,不管谁对谁错,Java和.Net是目前应用开发的两个 ...

  5. 浅谈.Net和Java互相调用的三种方式

    在很多的大型系统开发中,开发工具往往不限制于同一种开发语言,而是会使用多种开发语言的混合型开发.目前Java和.Net都声称自己占85%的市场份 额,不管谁对谁错,Java和.Net是目前应用开发的两 ...

  6. SpringBoot2.0.2 Application调用的三种方式

    一.注解 @SpringBootApplication            点开查看源码是由多个注解合成的注解,其中主要的注解有:            @SpringBootConfigurati ...

  7. 手写面试编程题- 数组去重 深拷贝 获取文本节点 设置奇数偶数背景色 JS中检测变量为string类型的方法 第6题闭包 将两个数组合并为一个数组 怎样添加、移除、移动、复制、创建和查找节点? 继承 对一个数组实现随机排序 让元素水平 垂直居中的三种方式 通过jQuery的extend方法实现深拷贝

    第1题==>实现数组去重 通过 new Set(数组名) // var arr = [12, 12, 3, 4, 5, 4, 5, 6, 6]; // var newarr1 = new Set ...

  8. 判断python对象是否可调用的三种方式及其区别

    查找资料,基本上判断python对象是否为可调用的函数,有三种方法 使用内置的callable函数 callable(func) 用于检查对象是否可调用,返回True也可能调用失败,但是返回False ...

  9. Struts2访问ServletAPI的三种方式

    web应用中需要访问的ServletAPI,通常只有HttpServletRequest,HttpSession,ServletContext三个,这三个接口分别代表jsp内置对象中的request, ...

随机推荐

  1. python int函数转换浮点型字符串的坑???

    python中的int函数可以将数字或字符串转换为整型数字类型,具体功能就不提了 最近发现一个问题,对于字符串'1.1'之类的,int转换的时候会报异常,这是为什么,个人感觉直接转换成1不就行了,干嘛 ...

  2. jmeter 调用jar包 本地加密

    1.因为加密接口是有我们自己加密方式,所有加密包由开发提供,获得加密包后方式jmeter目录/lib/ext文件夹中 2.选择引入加密包 3.添加BeanShell Sampler和Debug Sam ...

  3. 企业搜索引擎开发之连接器connector(十七)

    本文描述连接器的提供与外界交互的servlet接口,连接器与外部是通过xml格式数据交互的 1)  获取所有连接类型 提交地址:http://localhost:8080/connector-mana ...

  4. 咏南中间件V10.1更新日志

    咏南中间件V10.1 2016-8-3----------------------------------------------------开始支持DELPHI10.1(BERLIN)增加中间件业务 ...

  5. 转:Entity FrameWork利用Database.SqlQuery<T>执行存储过程并返回参数

    public IEnumerable<Statistic> GetStatistics(IEnumerable<Guid> itemIds) { var ctx = new D ...

  6. web项目开发最佳做法

    一个成熟的web项目应该具备以下基础代码或做法 1.前端基础框架: 统一的ajax 通信/表单提交及调用结果弹窗显示 统一的数据验证 统一的数据列表 2.后端基础框架: 统一的异常处理捕获,可针对具体 ...

  7. XML--将XML中数据提取出转换成表2

    DECLARE @xml XMLSET @xml = '<Students>    <Student  id="1001" name = "xu&quo ...

  8. Ubuntu 16.04.2 LTS 安装 jdk1.6 和 tomcat6 (一)

    java和tomcat环境配置已经有很多教程和文章,最近项目需要配置Ubuntu 16.04.2下的古老的java6和tomcat 6,遇到小坑,特记录和分享. 网上的教程不是太新,就是太老,还有一些 ...

  9. 【MVC】输出HTML内容,不输出HTML标签

    第一种方式: @Html.Raw("内容") 第二种方式 @(new HtmlString("<h1>abcd</h1>")) 第三种方 ...

  10. GO学习笔记 - 基本数据类型

    官方教程:https://tour.go-zh.org/basics/11 Go 的基本类型有Basic types bool string int int8 int16 int32 int64 ui ...