Struts2自定义方法最佳实践

自定义方法实现

在 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 方法混乱。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章