struts1登錄示例代碼_動(dòng)力節(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)槲覀冊(cè)趙eb.xml中配置的是*.do。這里依舊不介紹為什么隨后博客會(huì)深入分析。
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類,簡(jiǎn)單說一下這個(gè)類,這個(gè)類主要是負(fù)責(zé)收集表單數(shù)據(jù)的,在這里一定要注意表單的屬性必須和actionForm中的get和set方法的屬性一致。這里依舊不深入解釋,隨后博客都會(huì)涉及到。
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)做完了,感興趣童鞋可以自己手動(dòng)運(yùn)行一下。
- struts1之ActionServlet詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- Java中struts2和spring MVC的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- Java Struts圖片上傳至指定文件夾并顯示圖片功能
- 詳解JAVAEE——SSH三大框架整合(spring+struts2+hibernate)
- 實(shí)例詳解java Struts2的配置與簡(jiǎn)單案例
- java+jsp+struts2實(shí)現(xiàn)發(fā)送郵件功能
- java中的Struts2攔截器詳解
- 復(fù)選框和Struts2后臺(tái)交互代碼詳解
相關(guān)文章
SpringBoot使用工具類實(shí)現(xiàn)獲取容器中的Bean
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何使用工具類實(shí)現(xiàn)獲取容器中的Bean,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
用java等語言仿360首頁拼音輸入全模糊搜索和自動(dòng)換膚
這篇文章主要為大家詳細(xì)介紹了仿360首頁支持拼音輸入全模糊搜索和自動(dòng)換膚的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
使用Java獲取Json中的數(shù)據(jù)簡(jiǎn)單示例
開發(fā)過程中經(jīng)常會(huì)遇到j(luò)son數(shù)據(jù)的處理,而單獨(dú)對(duì)json數(shù)據(jù)進(jìn)行增刪改并不方便,下面這篇文章主要給大家介紹了關(guān)于使用Java獲取Json中的數(shù)據(jù),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
Spring Boot集成 Spring Boot Admin 監(jiān)控
這篇文章主要介紹了Spring Boot集成 Spring Boot Admin 監(jiān)控,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
java基礎(chǔ)之TreeMap實(shí)現(xiàn)類全面詳解
這篇文章主要為大家介紹了java基礎(chǔ)之TreeMap實(shí)現(xiàn)類全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
JDK9為何要將String的底層實(shí)現(xiàn)由char[]改成了byte[]
String 類的源碼已經(jīng)由?char[]?優(yōu)化為了?byte[]?來存儲(chǔ)字符串內(nèi)容,為什么要這樣做呢?本文就詳細(xì)的介紹一下,感興趣的可以了解一下2022-03-03

