首页 > 其他分享 >Struts2自定义方法最佳实践

Struts2自定义方法最佳实践

时间:2022-11-02 21:35:46浏览次数:47  
标签:xml 自定义 struts 最佳 Struts2 Action 方法

自定义方法实现

在 struts.xml 配置 method ,并且在对应的 Action 实现对应方法即可。

struts.xml

<action name="login2" class="space.terwer.struts23.LoginAction2" method="myExecute">
	<result name="success">/result2.jsp</result>
</action>

Action

public String myExecute() throws Exception {
	System.out.println("myExecute called");
	return SUCCESS;
}

效果

自定义方法的缺点

参数耦合,逻辑混乱。

自定义方法的优点

一个 Action 可以处理多种不同的逻辑。

最佳实现

Struts2 支持自定义方法,即在 struts.xml 的 Action 元素内定义 method 属性,属性值即为待执行的方法。其中,该方法的声明要与 execute 保持一致。 (不推荐)这种方式容易导致 Action 方法混乱。

标签:xml,自定义,struts,最佳,Struts2,Action,方法
From: https://www.cnblogs.com/tangyouwei/p/struts2-custom-method-best-practice-1ap3gz.html

相关文章