您好,登錄后才能下訂單哦!
今天用struts2寫了一個簡單的登錄操作;將步驟寫出來;
1、寫登錄頁面;
之前我還傻傻的用ajax來實現按鈕的提交操作,后來一直不能實現;百度了一下說ajax是異步無跳轉的操作,他的意思就是說包含了ajax的頁面是不會進行頁面跳轉的,即使我們采用了struts2進行跳轉,他也不會進行跳轉。所以我最終采用了form表單的方式進行數據提交及頁面跳轉的操作;針對用戶名,密碼處,需寫上name屬性,這樣才可以將數據從前臺傳到后臺;
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta chaset="UTF-8"></meta> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <!-- <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript"> function tijiao(){ console.log("username:"+$("#username").val()+",password:"+$("#password").val()); $.get("denglu.action?username="+$("#username").val()+"&password="+$("#password").val(),function(data,status,xxx){ //alert("status:"+status); if(status=="success"){ alert("回來了"); var s=${message} alert("s:"+s); }}); } </script> --> </head> <body> <form id="form1" action="denglu" method="post"> <table> <tr> <td>用戶名</td> <td><input id="username" type="text" name="username"></input> </td> </tr> <tr> <td>密碼</td> <td><input id="password" type="password" name="password"></input> </td> </tr> <tr> <td><input type="submit"></input> </td> </tr> </table> <span id="span">${message}</span> </form> </body> </html>
2、寫struts.xml配置文件;
<?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="default" extends="struts-default" namespace="/"> <action name="denglu" class="action.Login" method="execute"> <result name="success">welcom.jsp</result> <result name="faile">index.jsp</result> </action> </package> </struts>
3、寫action類
在此,action中無需進行數據提交,他可以通過屬性的get,set方法獲取頁面中的數據,而當我們需要在前臺獲取action中的參數時,那么該參數也必須提供一個get方法(set方法可以不提供)
package action; import com.opensymphony.xwork2.ActionSupport; /** * 需要繼承ActionSupport * * @author Administrator * */ public class Login extends ActionSupport { private String username; private String password; private String message; 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 execute() { System.out.println("有沒有走到這里來"); String s; System.out.println("getUsername():" +getUsername()+ ";password:" + getPassword()); if (getUsername().equals("admin") && getPassword().equals("a")) { System.out.println("賬號密碼正確了"); message = "歡迎您," + username; s = "success"; } else { System.out.println("賬號密碼錯誤了"); message = "用戶名或密碼輸入錯誤"; s = "faile"; } System.out.println("message:" + message); return s; } /** * 要想獲取action中的參數,必須給該變量設置get,set方法 * @return */ public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
4、最后登錄成功的頁面
采用${value}的方式獲取后臺的數據
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'welcom.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript" src="js/jquery.min.js"></script> </head> <body> ${message} </body> </html>
5、測試的結果
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。