利用struts1怎么實現(xiàn)一個登錄功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
長島網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),長島網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為長島上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的長島做網(wǎng)站的公司定做!
1、實例開始工作—導(dǎo)入jar包,在官網(wǎng)上下載struts1框架包,解壓之后導(dǎo)入工程的:
2、之后配置web.xml(這里的具體配置方法可以參見struts1框架包中的實例文件夾webapps中的實例代碼中web.xml文件的配置方法):
具體如下:
<span ><?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app></span>
首先這個配置文件中最主要的就是做了兩件的事情,一個是配置ActionServlet,一個是初始化struts-config.xml配置文件參數(shù)。
3、配置完了web.xml文件,之后我們就要開始進(jìn)入項目代碼階段了。
登錄頁面:
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!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=GB18030"> <title>Insert title here</title> </head> <body> <form action="login.do" method="post"> 用戶:<input type="text" name="username"><br> 密碼:<input type="password" name="password"></br> <input type="submit" value="登錄"> </form> </body> </html>
切記那個action后面的路徑一定要是.do開頭的,因為我們在web.xml中配置的是*.do。這里依舊不介紹為什么隨后博客會深入分析。
4、建立兩個異常類,一個是用戶名未找到、一個是密碼錯誤:
①用戶名未找到
public class UserNotFoundException extends RuntimeException { public UserNotFoundException() { // TODO Auto-generated constructor stub } public UserNotFoundException(String message) { super(message); // TODO Auto-generated constructor stub } public UserNotFoundException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } public UserNotFoundException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } }
②密碼錯誤
public class PasswordErrorException extends RuntimeException { public PasswordErrorException() { // TODO Auto-generated constructor stub } public PasswordErrorException(String message) { super(message); // TODO Auto-generated constructor stub } public PasswordErrorException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } public PasswordErrorException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } }
5、業(yè)務(wù)處理類代碼:
public class UserManager { public void login(String username, String password) { if (!"admin".equals(username)) { throw new UserNotFoundException(); } if (!"admin".equals(password)) { throw new PasswordErrorException(); } } }
6、建立LoginActionForm類,這個類繼承ActionForm類,簡單說一下這個類,這個類主要是負(fù)責(zé)收集表單數(shù)據(jù)的,在這里一定要注意表單的屬性必須和actionForm中的get和set方法的屬性一致。這里依舊不深入解釋,隨后博客都會涉及到。
import org.apache.struts.action.ActionForm; /** * 登錄ActionForm,負(fù)責(zé)表單收集數(shù)據(jù) * 表單的屬性必須和ActionForm中的get和set的屬性一致 * @author Administrator * */ @SuppressWarnings("serial") public class LoginActionForm extends ActionForm { private String username; private String password; 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; } }
7、LoginAction類的建立,這個是負(fù)責(zé)取得表單數(shù)據(jù)、調(diào)用業(yè)務(wù)邏輯以及返回轉(zhuǎn)向信息。
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * 登錄Action * 負(fù)責(zé)取得表單數(shù)據(jù)、調(diào)用業(yè)務(wù)邏輯、返回轉(zhuǎn)向信息 * * @author Administrator * */ public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginActionForm laf=(LoginActionForm)form; String username=laf.getUsername(); String password=laf.getPassword(); UserManager userManager=new UserManager(); try{ userManager.login(username, password); return mapping.findForward("success"); }catch(UserNotFoundException e){ e.printStackTrace(); request.setAttribute("msg", "用戶名不能找到,用戶名稱=["+username+"]"); }catch(PasswordErrorException e){ e.printStackTrace(); request.setAttribute("msg", "密碼錯誤"); } return mapping.findForward("error"); } }
8、既然有轉(zhuǎn)向,那么我們還要建立兩個頁面,一個是登錄成功頁面,一個登錄失敗頁面。
①登錄成功頁面
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!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=GB18030"> <title>Insert title here</title> </head> <body> ${loginForm.username },登錄成功 </body> </html>
②登錄失敗頁面
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!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=GB18030"> <title>Insert title here</title> </head> <body> <%-- <%=request.getAttribute("msg") %> --%> ${msg } </body> </html>
9、最后要進(jìn)行struts-config.xml的配置
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm"/>
</form-beans>
<action-mappings>
<action path="/login"
type="com.bjpowernode.struts.LoginAction"
name="loginForm"
scope="request"
>
<forward name="success" path="/login_success.jsp" />
<forward name="error" path="/login_error.jsp"/>
</action>
</action-mappings>
</struts-config>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
分享題目:利用struts1怎么實現(xiàn)一個登錄功能
文章地址:http://jinyejixie.com/article38/ijgcpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站建設(shè)、用戶體驗、做網(wǎng)站、手機網(wǎng)站建設(shè)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)