一个action内包含多个处理逻辑。
struts2有一个页面处理多个请求的action
可使用DMI调用来处理。
<%@ page language="java" contentType="text/html; charset=GBK"%>
<script>
function regist()
{
targetForm = document.forms[0];
targetForm.action = "Login!regist.action";
targetForm.submit();
}
</script>
<html>
<head>
<title>登陆页面</title>
</head>
<body>
<table width="300" align="center">
<form action="Login.action" method="post">
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td><input type="submit" value="登陆"/></td>
<td><input type="button" value="注册" onClick="regist();"/></td>
</tr>
</form>
<table>
</body>
</html>
*[登录]是进入action的execute
[注册]进入action的register
使用动态方法调用 前必须设置struts2允许动态方法调用 。
要设置struts.enable.DynamicMethodInvocation的值为true.
public class LoginRegistAction extends ActionSupport
{
public String regist() throws Exception
{
..
}
public String execute() throws Exception
{
..
}
<struts>
<package name="lee" extends="struts-default">
<action name="Login" class="lee.LoginRegistAction">
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
本文介绍如何在Struts2框架中通过动态方法调用(DMI)实现一个Action处理多个请求逻辑,如登录和注册功能。文章展示了具体的HTML表单和Action配置,并提供了LoginRegistAction类的代码示例。
978

被折叠的 条评论
为什么被折叠?



