struts1登錄示例代碼_動力節(jié)點Java學(xué)院整理
Struts1框架實例—登錄實例:
1、實例開始工作—導(dǎo)入jar包,在官網(wǎng)上下載struts1框架包,解壓之后導(dǎo)入工程的:

2、之后配置web.xml(這里的具體配置方法可以參見struts1框架包中的實例文件夾webapps中的實例代碼中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>
首先這個配置文件中最主要的就是做了兩件的事情,一個是配置ActionServlet,一個是初始化struts-config.xml配置文件參數(shù)。
3、配置完了web.xml文件,之后我們就要開始進入項目代碼階段了。
登錄頁面:
<%@ 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類,簡單說一下這個類,這個類主要是負責(zé)收集表單數(shù)據(jù)的,在這里一定要注意表單的屬性必須和actionForm中的get和set方法的屬性一致。這里依舊不深入解釋,隨后博客都會涉及到。
import org.apache.struts.action.ActionForm;
/**
* 登錄ActionForm,負責(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類的建立,這個是負責(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
* 負責(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、最后要進行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)過配置之后實例就已經(jīng)做完了,感興趣童鞋可以自己手動運行一下。
相關(guān)文章
SpringBoot使用工具類實現(xiàn)獲取容器中的Bean
這篇文章主要為大家詳細介紹了SpringBoot如何使用工具類實現(xiàn)獲取容器中的Bean,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
Spring Boot集成 Spring Boot Admin 監(jiān)控
這篇文章主要介紹了Spring Boot集成 Spring Boot Admin 監(jiān)控,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
java基礎(chǔ)之TreeMap實現(xiàn)類全面詳解
這篇文章主要為大家介紹了java基礎(chǔ)之TreeMap實現(xiàn)類全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
JDK9為何要將String的底層實現(xiàn)由char[]改成了byte[]
String 類的源碼已經(jīng)由?char[]?優(yōu)化為了?byte[]?來存儲字符串內(nèi)容,為什么要這樣做呢?本文就詳細的介紹一下,感興趣的可以了解一下2022-03-03

