Struts2開發(fā)環(huán)境搭建 附簡單登錄功能實例
首先是搭建Struts2環(huán)境。
第一步 下載Struts2
去Struts官網(wǎng) http://struts.apache.org/ 下載Struts2組件。
截至目前,struts2最新版本為2.3.1.3,下載struts-2.3.16.3-all.zip,解壓,放著。
第二步 新建Web Project并導入jar包
在MyEclispe中新建Web Project,然后找到解壓的Struts2包,在里面apps文件夾下找到struts2-blank.war,解壓這個WAR文件,將里面WEB-INF\lib目錄下的jar文件全部復制到新建的Web Project的WebRoot\WEB-INF\lib目錄下。
第三步 配置web.xml
在項目的WebRoot\WEB-INF\目錄下找到web.xml文件,沒有就新建一個web.xml文件,在里面添加如下代碼:
<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.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/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>web1</display-name> <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> <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> </web-app>
第四步 配置struts.xml
在項目的src目錄下找到struts.xml文件,沒有就新建一個,里面代碼如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="main" extends="struts-default"> <!-- 在這里面配置action --> </package> </struts>
到此,Struts2開發(fā)環(huán)境搭建完成。
下面演示一個登錄頁面實例。
第一步 修改Index.jsp
修改Index.jsp,做出登錄界面。
下面是index.jsp的代碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>登錄</title> </head> <body> <form action="login" method="post"> 登錄<br /> 賬號:<input type="text" name="username" /><br /> 密碼:<input type="password" name="password" /><br /> <input type="submit" value="登錄" /> </form> </body> </html>
下面是Index.jsp在瀏覽器中的效果:
第二步 編寫驗證賬戶和密碼的類
新建LogAction類,讓其繼承com.opensymphony.xwork2.ActionSupport類,注意到index.jsp中兩個輸入框的name屬性分別是username和password,所以LogAction類必須包含下面兩個屬性:
private String username
private String password
而且必須寫出他們的get、set方法。
然后,編寫execute方法,在execute方法中驗證賬號和密碼并返回String類型的結果,execute方法會在該Action類被調用的時候自動執(zhí)行。
下面是LogAction.java的完整代碼:
package com.lidi.struts.action; import com.opensymphony.xwork2.ActionSupport; public class LogAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username;//賬號 private String password;//密碼 //getters & setters 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; } /** * execute方法會在該Action類被調用的時候自動執(zhí)行, * 如果 賬號="admin"并且密碼="123456",就返回SUCCESS * 否則返回ERROR */ public String execute(){ if(username.equalsIgnoreCase("admin") && password.equalsIgnoreCase("123456")){ return SUCCESS; } else return ERROR; } }
上面的返回SUCCESS和返回ERROR什么意思呢,后面再講。
第三步 配置struts.xml
第二步編寫了Action類,第三步將該Action配置到struts.xml中,打開struts.xml,在package標簽中添加如下代碼:
<action name="login" class="com.lidi.struts.action.LogAction"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action>
action標簽的name屬性為login,這個必須與index.jsp中form標簽的action屬性一致,class屬性填寫LogAction類的全稱。
<result name="success">success.jsp</result> 這個標簽的意思是當LogAction類的execute方法返回SUCCESS時,頁面跳轉到success.jsp;同理,<result name="success">success.jsp</result> 這個標簽的意思是當LogAction類的execute方法返回ERROR時,頁面跳轉到error.jsp。
完整的struts.xml代碼如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="main" extends="struts-default"> <action name="login" class="com.lidi.struts.action.LogAction"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> </package> </struts>
這里用到了success.jsp和error.jsp,在項目中新建這兩個文件,success.jsp表示登錄成功后的頁面,上面顯示登錄用的賬戶和密碼,error.jsp表示登錄失敗后的頁面,上面顯示錯誤提示就就好了,他們的代碼分別如下:
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML> <html> <head> <title>登陸成功</title> </head> <body> 歡迎<s:property value="username" />,登錄成功!<br /> </body> </html>
<%@ taglib prefix="s" uri="/struts-tags"%>表示引用struts標簽庫
<s:property value="username" />是struts標簽,用以顯示登錄頁面?zhèn)鬟f過來的賬號。
error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>登錄失敗</title> </head> <body> 登錄失??!用戶名或密碼錯誤! </body> </html>
第四步 運行
配置struts.xml后要重啟下服務器,然后在瀏覽器中查看效果。
分別輸入賬號和密碼并登錄,如果賬號和密碼分別為admin和123456,頁面就會顯示
歡迎admin,登錄成功!
否則就會顯示
登錄失??!用戶名或密碼錯誤!
第五步 程序運行原理淺析
用戶填寫賬號密碼并點擊登錄后,瀏覽器會請求form標簽action屬性里面的鏈接,也就是login。服務器中,過濾器攔截到login這個請求,就會查找struts.xml中name=login的action,再找到這個action的class屬性對應的類,也就是com.lidi.struts.action.LogAction,然后實例化一個LogAction對象,并且把參數(shù)username和password分別賦給這個對象的username和passwrod屬性(這就是為什么LogAction類的兩個屬性名稱必須和index.jsp中兩個文本框的name屬性分別相同,并且必須添加他們的get和set方法),然后執(zhí)行這個對象的execute方法,并返回一個字符串,如果返回SUCCESS字符串,就查找struts.xml中對應action的<result>標簽中name屬性等于success的<result>標簽,并將頁面轉到該標簽里面配置的頁面success.jsp上。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 防止未登錄用戶操作—基于struts2攔截器的簡單實現(xiàn)
- Struts2攔截器 關于解決登錄的問題
- 詳解Struts2中對未登錄jsp頁面實現(xiàn)攔截功能
- Struts2攔截器登錄驗證實例
- struts2與cookie 實現(xiàn)自動登錄和驗證碼驗證實現(xiàn)代碼
- Java struts2 validate用戶登錄校驗功能實現(xiàn)
- 使用MyEclipse 開發(fā)struts2框架實現(xiàn)登錄功能(結構教程)
- JQuery+Ajax+Struts2+Hibernate框架整合實現(xiàn)完整的登錄注冊
- struts2+jquery組合驗證注冊用戶是否存在
- 基于struts2和hibernate實現(xiàn)登錄和注冊功能
相關文章
Spring Cloud Gateway網(wǎng)關XSS過濾方式
這篇文章主要介紹了Spring Cloud Gateway網(wǎng)關XSS過濾方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Mybatis-Plus使用updateById()、update()將字段更新為null
本文主要介紹了Mybatis-Plus使用updateById()、update()將字段更新為null,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08springboot+zookeeper實現(xiàn)分布式鎖的示例代碼
本文主要介紹了springboot+zookeeper實現(xiàn)分布式鎖的示例代碼,文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03