您好,登錄后才能下訂單哦!
學了一定的jsp基礎之后,我們可以試著去嘗試去實現一個簡單的struts2的動態登錄功能,具體代碼如下:
一、目錄結構
首先,lib中導入struts2所需的核心包,
下載地址:http://struts.apache.org/download.cgi#struts23281
總體目錄結構如下:
二、web.xml中
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>srtuts2</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>Struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>Struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
三、創建一個用戶類User.java
package com.ruanjian.model; public class User { private String userName; private String passworld; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassworld() { return passworld; } public void setPassworld(String passworld) { this.passworld = passworld; } }
四、創建login.jsp文件,用于登錄的主頁面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>struts2登錄頁面</title> </head> <body> <form action = "login" method = "post" > 用戶名:<input type = "text" name = "userName" /> 密碼:<input type = "text" name = "passworld" /> <input type = "submit" value = "登錄" /> </form> </body> </html>
創建success.jsp文件,代表登錄成功頁面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登陸成功</title> </head> <body> 登陸成功! </body> </html>
創建error.jsp文件,代表登錄失敗頁面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登錄失敗</title> </head> <body> 登錄失敗! </body> </html>
五、創建UserService.java用于驗證用戶登錄
package com.ruanjian.service; import com.ruanjian.model.User; public class UserService { public boolean isLogin(User user){ boolean flag = false; if("admin".equals(user.getUserName()) && "123456".equals(user.getPassworld())){ flag = true; } return flag; } }
六、創建LoginAction.java文件,后臺處理登錄
package com.ruanjian.action; import com.opensymphony.xwork2.Action; import com.ruanjian.model.User; import com.ruanjian.service.UserService; /** * struts2登錄后臺處理 * @author * */ public class LoginAction implements Action { UserService userService = new UserService(); private String userName; private String passworld; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassworld() { return passworld; } public void setPassworld(String passworld) { this.passworld = passworld; } @Override public String execute() throws Exception { User user = new User(); user.setUserName(userName); user.setPassworld(passworld); if(userService.isLogin(user)){ return SUCCESS; //如果登錄成功,返回成功 }else{ return ERROR; //否則返回失敗 } }
七、新建struts.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name = "index" extends = "struts-default"> <action name = "login" class = "com.ruanjian.action.LoginAction"> <result name = "success">/success.jsp</result> <result name = "error">/error.jsp</result> </action> </package> </struts>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。