Struts2三种action类的写法

本文详细介绍了在Struts框架中实现Action的三种主要方式:普通方式、接口方式和继承方式。通过具体的代码示例,展示了如何使用这些不同的方法来处理请求,并解释了每种方式的特点和应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.普通的方式

package com.it.struts2;

public class ActionStruts1 {
	public String execute() {
		System.out.println("普通的action方式");
		return "struts";
	}
}
<action name="hello" class="com.it.struts2.ActionStruts1">
			<result name="struts" >/hello.jsp</result>
		</action>

2.接口的方式

package com.it.struts2;

import com.opensymphony.xwork2.Action;

public class ActionStruts2 implements Action{

	@Override
	public String execute() throws Exception {
		System.out.println("接口的方式");
		return "hello2";
	}

}
<action name="hello2" class="com.it.struts2.ActionStruts2">
			<result name="hello2">/hello.jsp</result>
		</action>

3.继承的方式

package com.it.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class ActionStruts3 extends ActionSupport{
	@Override
	public String execute() throws Exception {
		System.out.println("继承的方式");
		return null;
	}
	public String save() {
		System.out.println("save方法");
		return null;
	}
}
<action name="hello3" class="com.it.struts2.ActionStruts3" method="save">
		</action>

action的三种访问方式

package com.it.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class ActionStruts4 extends ActionSupport {
	public String save() {

		System.out.println("save");
		return null;
	}

	public String delete() {

		System.out.println("delete");
		return null;
	}

	public String find() {

		System.out.println("find");
		return null;
	}

	public String edit() {

		System.out.println("edit");
		return null;
	}
}
<!-- action的3种访问方式 -->
		<!-- 普通的method方式 -->
		<!-- <action name="produte_save"  class="com.it.struts2.ActionStruts4" method="save"/>
		<action name="produte_delete"  class="com.it.struts2.ActionStruts4" method="delete"/>
		<action name="produte_find"  class="com.it.struts2.ActionStruts4" method="find"/>
		<action name="produte_edit"  class="com.it.struts2.ActionStruts4" method="edit"/> -->
		<!-- 通配符方式 
	 			*: save
	 			   delete
	 			   update
	 			   find
	 			   
	 			{1} :第一个星号的值
	 	
	 	-->	
	 	<!-- <action name="produte_*"  class="com.it.struts2.ActionStruts4" method="{1}"/> -->
	 	<!--  动态方法方式(了解) 
	 			1 动态方法方式默认是关闭的  需要开启   default.properties
	 			2 页面要使用特殊写法
	 			使用动态方法
			<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
			***页面地址也需要改变为:produte!save
			<a href="${pageContext.request.contextPath}/produte!save">商品保存</a>
			
			-->

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Exception.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值