struts2学笔记六 action中处理多个请求方法一

本文介绍如何在Struts2框架中通过动态方法调用(DMI)实现一个Action处理多个请求逻辑,如登录和注册功能。文章展示了具体的HTML表单和Action配置,并提供了LoginRegistAction类的代码示例。

一个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>密&nbsp;&nbsp;码:</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>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值