Action中的动态方法调用简单示例——登录注册系统

该项目有一个实现登录和注册功能的页面。

下面是该项目的文件结构图:
项目的文件结构图

1、编写视图界面(JSP页面)
登录页面
loginRegister.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Action中的动态方法调用</title>
		<script type="text/javascript">
			function register(){
				//获取页面第一个表单
				targetForm=document.forms[0];
				//动态修改表单的Action属性
				targetForm.action="loginReg";
				//表示提交函数
				document.forms[0].submit();
			}
		</script>
	</head>
	<body>
		<table width="360" align="center">
			<form  action="loginReg">
				<tr>
					<td>用户名:</td>
					<td><input name="userName" type="text" size="26"/></td>
				</tr>
				<tr>
					<td>&nbsp;&nbsp;码:</td>
					<td><input name="passWord" type="password" size="26"/></td>
				</tr>
				<tr>
					<td><input type="submit" value="登录"/></td>
					<td><input type="submit" value="注册" onclick="register()"/></td>
				</tr>
			</form>
		</table>
	</body>
</html>

success2.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>成功页面</title>
	</head>
	<body>
		<s:property value="msg"/>
	</body>
</html>

2、编写业务控制器Action
LoginRegisterAction.java

package loginRegisterAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class loginRegisterAction extends ActionSupport{
	private String userName;
	private String passWord;
	//设置返回信息
	private String msg;
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	//Action包含的注册控制逻辑
	public String regist() throws Exception{
		ActionContext.getContext().getSession().put("userName", getUserName());
		setMsg("恭喜你,"+userName+"注册成功!");
		return SUCCESS;
	}
	//Action默认包含的控制逻辑
	public String execute() throws Exception{
		if(getUserName().equals("hjw")&&getPassWord().equals("123")) {
			ActionContext.getContext().getSession().put("userName", getUserName());
			setMsg("你单击的是登录!"+"你的登录名为"+userName+",登录成功!");
			return SUCCESS;
		}
		else {
			return INPUT;
		}
	}

}

3、编写web.xml和struts.xml
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
	http://xmlns.jcp.org/xml/ns/javaeee/web-app_3_1.xsd" 
	id="WebApp_ID" 
	version="3.1" >
		<filter>
		<!-- 配置Struts2核心控制器的名字 -->
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	
	<filter-mapping>
		<!-- Struts2控制器的名字 -->
		<filter-name>struts2</filter-name>
		<!-- 拦截所有的URL请求-->
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<welcome-file-list>
		<welcome-file>loginRegister.jsp</welcome-file>
	</welcome-file-list>
	
	
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="zzf" extends="struts-default">
		<action name="loginReg" class="loginRegisterAction.loginRegisterAction" method="execute">
			<result name="success">/loginRegister/success2.jsp</result>
		</action>
		<action name="loginReg" class="loginRegisterAction.loginRegisterAction" method="regist">
			<result name="success">/loginRegister/success2.jsp</result>
		</action>
	</package>
</struts>

4、项目运行
登录成功界面:
登录成功界面
注册成功界面:
注册成功界面

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值