今年八月,當已經SSH三架完成學業。然後,他感動Android開展。三個框架已經很長的時間做無用的東西。所以,如果你想花三四天的時間來複習一下,寫在部落格。

附帶SSH整個jar包網盤下載:http://pan.baidu.com/s/1hqf5ta8

首先,我們新建立一個web專案,然後引入jar包:commons-fileupload。commons-io。freemarker,javassist。ognl。struts2-core,xwork-core。

這裡不過個簡單的struts程式。所以只引入了這些jar包

接下來,我們就是配置web.xml的內容了,web.xml:

  1. <?xml version="1.0" encoding="GBK"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  3.  
  4. <!-- 定義Struts2的核心Filter -->
  5. <filter>
  6. <filter-name>struts2</filter-name>
  7. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  8. </filter>
  9. <!-- 讓Struts2的核心Filter攔截全部請求 -->
  10. <filter-mapping>
  11. <filter-name>struts2</filter-name>
  12. <url-pattern>/*</url-pattern>
  13. </filter-mapping>
  14. </web-app>

這裡邊是定義了一個Struts2的核心攔截器,Struts2的核心就是環繞攔截器而生的

然後。我們寫一個登陸的頁面:login.jsp

  1. <%@ page language="java" contentType="text/html; charset=GBK"
  2. pageEncoding="GBK"%>
  3. <%@taglib prefix="s" uri="/struts-tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=GBK">
  8. <title><s:text name="loginPage"/></title>
  9. </head>
  10. <body>
  11. <s:form action="login">
  12. <s:textfield name="username" key="user"/>
  13. <s:textfield name="password" key="pass"/>
  14. <s:submit key="login"/>
  15. </s:form>
  16. </body>
  17. </html>

這裡用到了struts2的標籤庫。在以後會做介紹

在login.jsp我們為form表單定義了一個action,是在點選login之後跳轉的作用。在學servlet的時候應該都用過。

這個action我們能夠在struts.xml中定義。這個struts.xml我們通常是放在src資料夾下邊。也能夠變化(只是也要改變對應的路徑)

struts.xml:

  1. <?xml version="1.0" encoding="GBK"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
  4. "http://struts.apache.org/dtds/struts-2.1.7.dtd">
  5. <!-- 指定Struts 2配置檔案的根元素 -->
  6. <struts>
  7. <!-- 指定全域性國際化資原始檔 -->
  8. <constant name="struts.custom.i18n.resources" value="mess"/>
  9. <!-- 指定國際化編碼所使用的字符集 -->
  10. <constant name="struts.i18n.encoding" value="GBK"/>
  11. <!-- 全部的Action定義都應該放在package下 -->
  12. <package name="lee" extends="struts-default">
  13. <action name="login" class="org.crazyit.app.action.LoginAction">
  14. <!-- 定義三個邏輯檢視和物理資源之間的對映 -->
  15. <result name="input">/login.jsp</result>
  16. <result name="error">/error.jsp</result>
  17. <result name="success">/welcome.jsp</result>
  18. </action>
  19. </package>
  20. </struts>

這裡其他的東西不用管,我們看下<package></package>標籤中的內容,這裡邊定義了一個action,設定name屬性為“login”,class屬性為:LoginAction

然後下邊是三個result,分別有各自的name屬性。

這個struts.xml的作用就是:當頁面發來請求。由struts.xml處理,跳轉到對應的java處理程式,然後通過java處理程式跳轉到對應的result頁面

然後,我們看LoginAction.java:

  1. package org.crazyit.app.action;
  2.  
  3. import com.opensymphony.xwork2.ActionContext;
  4. import com.opensymphony.xwork2.ActionSupport;
  5.  
  6. public class LoginAction extends ActionSupport
  7. {
  8. //定義封裝請求引數的username和password屬性
  9. private String username;
  10. private String password;
  11.  
  12. public String getUsername()
  13. {
  14. return username;
  15. }
  16. public void setUsername(String username)
  17. {
  18. this.username = username;
  19. }
  20.  
  21. public String getPassword()
  22. {
  23. return password;
  24. }
  25. public void setPassword(String password)
  26. {
  27. this.password = password;
  28. }
  29. //定義處理使用者請求的execute方法
  30. public String execute() throws Exception
  31. {
  32. //當username為crazyit.org。password為leegang時即登入成功
  33. if (getUsername().equals("crazyit.org")
  34. && getPassword().equals("leegang") )
  35. {
  36. ActionContext.getContext().getSession()
  37. .put("user" , getUsername());
  38. return SUCCESS;
  39. }
  40. else
  41. {
  42. return ERROR;
  43. }
  44. }
  45. }

在LoginAction.java中我們定義了兩個屬性:username和password,而且定義了一個execute()方法,這種方法是預設的sruts處理方法

在這個execute方法中,我們定義了一個處理的邏輯,

這個邏輯規定username和password等於XXX的時候,利用ActionContext.getContext().getSession().put()函式,加入一個屬性user。內容為:getUsername()。

而且。return SUCCESS。

這裡的SUCCESS相應的就是struts.xml中的<result name="success">/welcome.jsp</result>

PS:須要注意的一點。我們能夠return不論什麼的一個字串,僅僅要在struts.xml中有與其相應的result就可以。

這裡是由於繼承了ActionSuppot,所以SUCCESS沒有加上雙引號,由於SUCCESS在ActionSuppot中默覺得“success"字串。

然後,我們執行:http://localhost:8080/Struts2Demo/login.jsp就能夠執行相關的操作了

版權宣告:本文博主原創文章,部落格,未經同意不得轉載。