今年八月,當已經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:
- <?xml version="1.0" encoding="GBK"?>
- <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">
- <!-- 定義Struts2的核心Filter -->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <!-- 讓Struts2的核心Filter攔截全部請求 -->
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
這裡邊是定義了一個Struts2的核心攔截器,Struts2的核心就是環繞攔截器而生的
然後。我們寫一個登陸的頁面:login.jsp
- <%@ page language="java" contentType="text/html; charset=GBK"
- pageEncoding="GBK"%>
- <%@taglib prefix="s" uri="/struts-tags"%>
- <!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=GBK">
- <title><s:text name="loginPage"/></title>
- </head>
- <body>
- <s:form action="login">
- <s:textfield name="username" key="user"/>
- <s:textfield name="password" key="pass"/>
- <s:submit key="login"/>
- </s:form>
- </body>
- </html>
這裡用到了struts2的標籤庫。在以後會做介紹
在login.jsp我們為form表單定義了一個action,是在點選login之後跳轉的作用。在學servlet的時候應該都用過。
這個action我們能夠在struts.xml中定義。這個struts.xml我們通常是放在src資料夾下邊。也能夠變化(只是也要改變對應的路徑)
struts.xml:
- <?xml version="1.0" encoding="GBK"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
- "http://struts.apache.org/dtds/struts-2.1.7.dtd">
- <!-- 指定Struts 2配置檔案的根元素 -->
- <struts>
- <!-- 指定全域性國際化資原始檔 -->
- <constant name="struts.custom.i18n.resources" value="mess"/>
- <!-- 指定國際化編碼所使用的字符集 -->
- <constant name="struts.i18n.encoding" value="GBK"/>
- <!-- 全部的Action定義都應該放在package下 -->
- <package name="lee" extends="struts-default">
- <action name="login" class="org.crazyit.app.action.LoginAction">
- <!-- 定義三個邏輯檢視和物理資源之間的對映 -->
- <result name="input">/login.jsp</result>
- <result name="error">/error.jsp</result>
- <result name="success">/welcome.jsp</result>
- </action>
- </package>
- </struts>
這裡其他的東西不用管,我們看下<package></package>標籤中的內容,這裡邊定義了一個action,設定name屬性為“login”,class屬性為:LoginAction
然後下邊是三個result,分別有各自的name屬性。
這個struts.xml的作用就是:當頁面發來請求。由struts.xml處理,跳轉到對應的java處理程式,然後通過java處理程式跳轉到對應的result頁面
然後,我們看LoginAction.java:
- package org.crazyit.app.action;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction extends ActionSupport
- {
- //定義封裝請求引數的username和password屬性
- 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;
- }
- //定義處理使用者請求的execute方法
- public String execute() throws Exception
- {
- //當username為crazyit.org。password為leegang時即登入成功
- if (getUsername().equals("crazyit.org")
- && getPassword().equals("leegang") )
- {
- ActionContext.getContext().getSession()
- .put("user" , getUsername());
- return SUCCESS;
- }
- else
- {
- return ERROR;
- }
- }
- }
在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就能夠執行相關的操作了
版權宣告:本文博主原創文章,部落格,未經同意不得轉載。