Struts2方法调用的三种方式(有新的!调用方法的说明)

在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

 

posted @ 2018-04-20 23:22  城南少年与猫  阅读(1078)  评论(0编辑  收藏  举报