您好,登錄后才能下訂單哦!
今天用struts2寫了一個簡單的登錄操作;將步驟寫出來;
1、寫登錄頁面;
之前我還傻傻的用ajax來實現(xiàn)按鈕的提交操作,后來一直不能實現(xiàn);百度了一下說ajax是異步無跳轉(zhuǎn)的操作,他的意思就是說包含了ajax的頁面是不會進(jìn)行頁面跳轉(zhuǎn)的,即使我們采用了struts2進(jìn)行跳轉(zhuǎn),他也不會進(jìn)行跳轉(zhuǎn)。所以我最終采用了form表單的方式進(jìn)行數(shù)據(jù)提交及頁面跳轉(zhuǎn)的操作;針對用戶名,密碼處,需寫上name屬性,這樣才可以將數(shù)據(jù)從前臺傳到后臺;
<%@ 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中無需進(jìn)行數(shù)據(jù)提交,他可以通過屬性的get,set方法獲取頁面中的數(shù)據(jù),而當(dāng)我們需要在前臺獲取action中的參數(shù)時,那么該參數(shù)也必須提供一個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中的參數(shù),必須給該變量設(shè)置get,set方法 * @return */ public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
4、最后登錄成功的頁面
采用${value}的方式獲取后臺的數(shù)據(jù)
<%@ 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、測試的結(jié)果
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。