注册登录功能实现struts2动态调用

login.jsp

<span style="font-size:24px;"><%@ page  contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>Action中的动态调用</title>
    <!-- 提交到名称是loginReg的Action的regist方法 -->
    <script type="text/javascript">
        function register(){
          document.form1.action="loginReg!regist";
          document.form1.submit();
        }
    </script>  	
  </head>
   <body>
   <!-- 提交到名称是loginReg的Action的execute方法 -->
     <form action="loginReg!execute" name="form1">
       <table width="360" align="center">
         <tr>
           <td>用户名:</td>
           <td><input type="text" name="userName" size="21"></td>
         </tr>
          <tr>
           <td>密  码 :</td>
           <td><input type="password" name="password" size="22"></td>
         </tr>
         <tr>
           <td></td>
           <!-- 一个表单中有两个提交按钮,使用javascript改变提交地址 -->
           <td >   <input type="submit" value=" 登 录 ">    
               <input type="button" value=" 注 册 " οnclick="register()"></td>
         </tr>
       </table>    
     </form>
  </body>
</html>
</span>

</pre><p></p><p><span style="font-size:24px;"></span></p><pre>
LoginRegAction.java

<span style="font-size:24px;">package login;

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

public class LoginRegAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String userName;
	private String password;//属性要与jsp显示组件的那么值相同
	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;
	}
	//注册调用的方法
	public String regist(){
		ActionContext.getContext().getSession().put(userName, getUserName());
		setMsg("恭喜你,"+userName+"注册成功!");
		return "success";
	}
   //登录调用的方法
	public String execute(){
		if(getUserName().equals("QQ")&&getPassword().equals("123")){
			ActionContext.getContext().getSession().put(userName, getUserName());
			setMsg("你单击的是【登录】!"+"你的登录名为"+userName+",登录成功!");
			return "success";	
		}
		else{
			return INPUT;
		}
	}
	
}
</span>
struts.xml

<span style="font-size:24px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="login" extends="struts-default">
      <action name="loginReg" class="login.LoginRegAction">
          <result name="success">/success.jsp</result>
          <result name="input">/login.jsp</result>
      </action>   
    </package>
</struts>    
</span>
success.jsp

<span style="font-size:24px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html"%>
<%@ taglib uri="/struts-tags" prefix="s" %><!-- struts2的标签库 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>成功界面</title>

  </head>
  
  <body>
           <s:property value="msg"/>
  </body>
</html>
</span>



提交到Action的方法是参数是放在URL后边传给Action,并注入到Action的属性中的


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值