Strust2中的Aaction默认访问方法为exeucte()方法,当一个Acton中有多个方法时,我们应该如何来做配置?Struts提供了三种方式来实现

    书写Action方法时要注意的事项

        1、在action里面的方法有返回值时,在配置文件中没有配置,会出现错误。

        2、在action里面的方法有返回值时,返回值的类型必须是String。

        3、在action里面的方法也可以没有返回值,没有返回值的时候 result标签不需要配置

                    1)没有返回值可写成 public void add();

                    2)或者让返回值返回"none" 推荐用这种方式

    Action的三种访问方法的实现

        1、使用action标签的method属性,在这个属性里面写执行的action的方法。

        2、使用通配符方式实现。

        3、动态访问实现(不用)。

    

      使用action标签method访问多个方法

       第一步创建类和多个方法

package com.netxintai.action;

import com.opensymphony.xwork2.ActionSupport;

public class Person extends ActionSupport {

	public String add() throws Exception {

		System.out.println("add()....");
		return NONE;
	}

	public String update() throws Exception {
		
		System.out.println("update().....");
		return NONE;
	}

	
}

      第二步配置struts.xml文件

<?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>
    <package name="default" namespace="/" extends="struts-default">

    <!-- 实现Person类中的add()方法 -->
    <action name="addAction" class="com.netxintai.action.Person" method="add"></action>
    
    <!-- 实现Person类中的update()方法 -->
    <action name="updateAction" class="com.netxintai.action.Person" method="update"></action>
    
    </package>

</struts>

       总结:action每个方法都需要配置,如果类里面有多个方法,就要配置多个action


    使用通配符配置实现(推荐!)   

      1、在action标签里面name属性加入*号,加入匹配值。

             1)*理解:表示匹配任意内容

<?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>
    <package name="default" namespace="/" extends="struts-default">

    <!-- 通配符实现Person类中的方法 -->
    <action name="person_*" class="com.netxintai.action.Person" method="{1}"></action>
    
   <!-- 
   		name属性值里面写符号* mehtod属性写method="{1}" 1代表*
   			(1)执行aciton里面的add方法,访问person_add 使用person_*可匹配到。*相当于变成了add
   			(2)执行action里面的update方法,访问person_update,使用person_*可以匹配到。*相当于变成update

    -->
    
    </package>

</struts>

    

3 Thoughts on “Struts2教程–如何访问Action类中的方法

  1. Induced deletion of p27 Kip1 in neonates led to 5 fold increased BrdU labeling in neonatal p27 L L; CreER vestibular macula relative to negative controls mean p27 L L; CreER 1, 948 BrdU cells mm 2 SE, mean negative controls 375 BrdU cells mm 2 SE, Sup canadian pharmacy cialis 20mg Ultimately, the Court took a middle ground approach, declining to adopt the scope of the patent rule espoused by the Eleventh Circuit, and also rejecting the quick look presumption that was advocated by the FTC and the plaintiffs bar and adopted by the Third Circuit in K Dur

  2. They re good at explaining the information about the treatment and cost real cialis no generic Department of Energy DOE 2020

  3. You actually make it seem really easy along with your presentation but I
    find this topic to be actually something that I feel I’d never understand.
    It kind of feels too complex and extremely broad for
    me. I am looking forward in your subsequent post, I will try
    to get the hang of it! Lista escape roomów

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Post Navigation