struts1登錄示例代碼_動力節(jié)點(diǎn)Java學(xué)院整理
Struts1框架實(shí)例—登錄實(shí)例:
1、實(shí)例開始工作—導(dǎo)入jar包,在官網(wǎng)上下載struts1框架包,解壓之后導(dǎo)入工程的:
2、之后配置web.xml(這里的具體配置方法可以參見struts1框架包中的實(shí)例文件夾webapps中的實(shí)例代碼中web.xml文件的配置方法):
具體如下:
<span style="font-size:18px;"><?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>
首先這個(gè)配置文件中最主要的就是做了兩件的事情,一個(gè)是配置ActionServlet,一個(gè)是初始化struts-config.xml配置文件參數(shù)。
3、配置完了web.xml文件,之后我們就要開始進(jìn)入項(xiàng)目代碼階段了。
登錄頁面:
<%@ 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>
切記那個(gè)action后面的路徑一定要是.do開頭的,因?yàn)槲覀冊趙eb.xml中配置的是*.do。這里依舊不介紹為什么隨后博客會深入分析。
4、建立兩個(gè)異常類,一個(gè)是用戶名未找到、一個(gè)是密碼錯(cuò)誤:
①用戶名未找到
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 } }
②密碼錯(cuò)誤
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類,這個(gè)類繼承ActionForm類,簡單說一下這個(gè)類,這個(gè)類主要是負(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類的建立,這個(gè)是負(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", "密碼錯(cuò)誤"); } return mapping.findForward("error"); } }
8、既然有轉(zhuǎn)向,那么我們還要建立兩個(gè)頁面,一個(gè)是登錄成功頁面,一個(gè)登錄失敗頁面。
①登錄成功頁面
<%@ 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"
"
<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>
經(jīng)過配置之后實(shí)例就已經(jīng)做完了,感興趣童鞋可以自己手動運(yùn)行一下。
相關(guān)文章
Java利用移位運(yùn)算將int型分解成四個(gè)byte型的方法
今天小編就為大家分享一篇關(guān)于Java利用移位運(yùn)算將int型分解成四個(gè)byte型的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12springboot yml中profiles的巧妙用法(小白必看多環(huán)境配置)
這篇文章主要介紹了springboot yml中profiles的巧妙用法,非常適合多環(huán)境配置場景,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Spring6.x對調(diào)度和異步執(zhí)行的注解支持示例詳解
這篇文章主要為大家介紹了Spring6.x對調(diào)度和異步執(zhí)行的注解支持示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Springboot之@Async不執(zhí)行原因及分析
這篇文章主要介紹了Springboot之@Async不執(zhí)行原因及分析,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09SpringBoot如何通過webjars管理靜態(tài)資源文件夾
這篇文章主要介紹了SpringBoot如何通過webjars管理靜態(tài)資源文件夾,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Java設(shè)計(jì)模式之建造者模式(Builder模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之建造者模式(Builder模式)介紹,本文講解了為何使用建造者模式、如何使用建造者模式、Builder模式的應(yīng)用等內(nèi)容,需要的朋友可以參考下2015-03-03Gson中@JsonAdater注解的幾種方式總結(jié)
這篇文章主要介紹了Gson中@JsonAdater注解的幾種方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08詳解spring-boot actuator(監(jiān)控)配置和使用
本篇文章主要介紹了spring-boot actuator(監(jiān)控)配置和使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09