最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

struts1登錄示例代碼_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

 更新時(shí)間:2017年08月31日 11:08:59   作者:lfsf802  
這篇文章主要介紹了struts1登錄示例代碼,需要的朋友可以參考下

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)行一下。

相關(guān)文章

  • SpringBoot使用工具類實(shí)現(xiàn)獲取容器中的Bean

    SpringBoot使用工具類實(shí)現(xiàn)獲取容器中的Bean

    這篇文章主要為大家詳細(xì)介紹了SpringBoot如何使用工具類實(shí)現(xiàn)獲取容器中的Bean,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • 用java等語言仿360首頁拼音輸入全模糊搜索和自動(dòng)換膚

    用java等語言仿360首頁拼音輸入全模糊搜索和自動(dòng)換膚

    這篇文章主要為大家詳細(xì)介紹了仿360首頁支持拼音輸入全模糊搜索和自動(dòng)換膚的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • 使用Java獲取Json中的數(shù)據(jù)簡(jiǎn)單示例

    使用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
  • java圖形界面編程之模擬血壓計(jì)

    java圖形界面編程之模擬血壓計(jì)

    本文主要介紹了java基于圖形處理的模擬血壓計(jì),創(chuàng)新實(shí)驗(yàn)項(xiàng)目的部分代碼,作為平時(shí)練習(xí)用。
    2014-02-02
  • 詳解springMVC—三種控制器controller

    詳解springMVC—三種控制器controller

    本篇文章主要介紹了springMVC—三種控制器controller,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • Spring Boot集成 Spring Boot Admin 監(jiān)控

    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
  • Aspectj與Spring AOP的對(duì)比分析

    Aspectj與Spring AOP的對(duì)比分析

    這篇文章主要介紹了Aspectj與Spring AOP的對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java基礎(chǔ)之TreeMap實(shí)現(xiàn)類全面詳解

    java基礎(chǔ)之TreeMap實(shí)現(xiàn)類全面詳解

    這篇文章主要為大家介紹了java基礎(chǔ)之TreeMap實(shí)現(xiàn)類全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Java注解方式之防止重復(fù)請(qǐng)求

    Java注解方式之防止重復(fù)請(qǐng)求

    這篇文章主要介紹了關(guān)于Java注解方式防止重復(fù)請(qǐng)求,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • JDK9為何要將String的底層實(shí)現(xiàn)由char[]改成了byte[]

    JDK9為何要將String的底層實(shí)現(xiàn)由char[]改成了byte[]

    String 類的源碼已經(jīng)由?char[]?優(yōu)化為了?byte[]?來存儲(chǔ)字符串內(nèi)容,為什么要這樣做呢?本文就詳細(xì)的介紹一下,感興趣的可以了解一下
    2022-03-03

最新評(píng)論

达孜县| 宁强县| 尉犁县| 江阴市| 海阳市| 辽宁省| 山东省| 临澧县| 江孜县| 丹巴县| 宜州市| 南郑县| 贺州市| 绥德县| 固始县| 如皋市| 新田县| 贡嘎县| 丹寨县| 万全县| 保康县| 临邑县| 个旧市| 吴旗县| 长乐市| 皋兰县| 文登市| 兴仁县| 巴彦淖尔市| 长乐市| 柳江县| 怀宁县| 舞钢市| 乐平市| 古丈县| 三河市| 庆阳市| 府谷县| 新蔡县| 江北区| 荣昌县|