天天看點

Struts入門第一個小示例程式

第一章 Struts入門
           

一 課程目标

1. 掌握Struts2的環境搭建

2. 掌握Struts2的運作機制

3. 使用Struts2實作Hello World執行個體程式

二 先操作,通過操作後,再來了解Struts2架構。實作Hello World執行個體程式

1.環境搭建

- 打開MyEclipse,建立一個Web Project。

- 在MyEclipse->window->preference->MyEclipse->Server->Tomcat設定Tomcat的位置

- 在MyEclipse->window->preference->java->Installed JREs設定JDK的位置

- 下載下傳Struts2,然後解壓。解壓後打開app目錄,解壓裡面的struts2-blank.war檔案。解壓以後把解壓後的示例項目中的WEB-INF->classes目錄下的struts.xml檔案拷貝到自己項目中的src下。把struts2加壓後的lib目錄裡面的jar包拷貝到自己項目中的WEB-INF->lib目錄下。

- 打開Web.xml,配置Struts2。配置代碼參考Struts2中的示例程式的web.xml

2.代碼實作

  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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">
    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
           
  • struts.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>

    <package name="default" extends="struts-default" namespace="/">
        <action name="hello" class="action.TestActionSupport">
            <result>/hello.jsp</result>
        </action>       
    </package>
</struts>
           
  • hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css" target="_blank" rel="external nofollow" >
    -->
  </head>

  <body>
        <!-- 測試ActionSupport的使用者 -->
        <form action="test1.action" method="post">
            使用者名:<input type="text" name="username" ><br>
            密碼:<input type="text" name="password" ><br>
            <input type="submit" value="送出">            
        </form>
  </body>
</html>
           

3.配置常量為開發模式,在開發過程中修改struts.xml不需要重新開機伺服器。可以立即回報結果。

<constant name="struts.devMode" value="true"></constant>
           

4.關聯struts2源代碼和javadoc文檔。關聯源代碼在我們使用Struts2中的類時,可以随時按ctrl+單擊就可以檢視源代碼。關聯javadoc文檔,在開發中可以直接在myeclipse中檢視API文檔。

5.Struts2的運作機制。

  • 浏覽器(用戶端)發送一個請求給伺服器。伺服器收到浏覽器的請求以後,會到webapps目錄裡面去尋找是否包含該應用。如果有這個應用,就會去讀取該應用下的web.xml,看該請求是否有Servlet或者Filter或者監聽器來處理。
  • 由于我們之前在web.xml中配置過struts2,是一個filter。是以會把請求交給這個struts2中的filter來處理。
  • struts2中的filter的處理方式是,第一步去讀取struts.xml檔案,查詢在請求URL中,有沒有比對的package,如果有就查詢package中有沒有處理該請求的action。如果有就交給這個action處理。
  • 處理的結果就是result。把result标簽中的頁面傳回給浏覽器。以上就是struts2的大概運作機制。

繼續閱讀