一個完整的struts 2 Hello World程序(5)

2.2  一個簡單的HelloWorld

上節介紹了搭建開發Struts 2應用所需要的環境的配置,本節將以一個簡單的HelloWorld示例來介紹Struts 2給讀者帶來的體驗。

爲了使讀者能夠更清楚地瞭解示例的編寫過程,示例將採用文本編輯器的方式來開發,這樣避免了IDE集成編輯器給讀者帶來的困擾。開發一個Web應用,要建立符合規範的目錄結構,上節中已經對Web應用目錄進行了詳細的介紹,爲了開發HelloWorld應用,讀者應該先建立一個目錄。筆者建立的順序如下:

 E盤中建立一個文件夾:E:/myweb

 在此文件夾中建立WEB-INF文件夾。

 建立E:/myweb/WEB-INF/classes文件夾。

 建立E:/myweb/WEB-INF/lib文件夾。

 Tomcat默認的web.xml文件拷貝到E:/myweb/WEB-INF/目錄下,web.xml文件在Tomcat安裝目錄中的webapps/ROOT/WEB-INF目錄下。

 使用文本編輯器建立一個struts.xml文件,保存到E:/myweb/WEB-INF/classes目錄下。

2.2.1  配置web.xml文件

完成上述步驟後,一個簡單的Web目錄就完工了,接下來需要修改web.xml文件和struts.xml文件。web.xml文件內容如代碼2.4所示。

代碼2.4  HelloWorld示例的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" 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">

    
<filter>

        
<!— 配置filter-- >

        
<filter-name>struts2</filter-name>

        
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

    
</filter>

    
<filter-mapping>

            
<!—攔截所有URL用戶請求

        <filter-name
>struts2</filter-name>

        
<url-pattern>/*</url-pattern>

    
</filter-mapping>

    
<!—配置歡迎界面文件-- >

<welcome-file-list>

        
<welcome-file>index.html</welcome-file>

        
<welcome-file>index.htm</welcome-file>

        
<welcome-file>index.jsp</welcome-file>

        
<welcome-file>default.html</welcome-file>

        
<welcome-file>default.htm</welcome-file>

        
<welcome-file>default.jsp</welcome-file>

    
</welcome-file-list>

</web-app>

 

在代碼2.4所示內容中,相比原來的內容,增加了<filter><filter-mapping><filter>指定了需要加載的Struts 2核心控制器org.apache.struts2.dispatcher.FilterDispatcher,而<filter-mapping>使用通配符“/*”來攔截所有的URL請求,保證了用戶請求都被Struts 2接收處理。

2.2.2  配置struts.xml文件

編輯struts.xml文件內容,如代碼2.5所示。

代碼2.5  HelloWorld示例的struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd"
>

<!—配置struts2-->

<struts>

    
<!-- 配置包,名稱爲bookcode  -->

    
<package name="bookcode" extends='struts-default'>

    
<!-- 配置Action  -->

    
<action name="HelloWorld" class="ch2.helloworld.HelloWorld">

            
<!-- 配置返回結果  -->

            
<result name="success">/ch2/helloworld/success.jsp</result>

            
<result name="error">/ch2/helloworld/error.jsp</result>

        
</action>

    
</package>

 

</struts>

 

在默認情況下,Struts 2將會自動加載位於WEB-INF/classes目錄下的struts.xml配置文件。<package>定義了一個包空間,可以看到,該文件中只配置了一個Actionname爲“HelloWorld”,對應的class爲“ch2.HelloWorld”,也就是指定WEB-INF/classes/ch2目錄下的HelloWorld.class類文件,當然現在還沒有這個文件,後面將會編寫該文件。

說明

bookcode是本書示例的包名稱,該包繼承Struts 2框架的默認包struts-default,讀者在後面章節將會學習使用extends屬性。

 

另外一個配置就是result,即Action處理後返回給用戶的視圖資源,從配置中可以看到配置了兩個resultsuccesserror,分別對應E:/myweb/ch2目錄下的success.jsperror.jsp,後面也將會建立這兩個文件。

本節介紹的HelloWorld的思路是用戶在客戶端輸入一個字符串,由Action判斷輸入的字符串是否爲空,如果不爲空,則返回給用戶success.jsp頁面,並在控制檯打印出該字符串;如果用戶輸入爲空,則返回error.jsp頁面給用戶,提示輸入爲空。這個流程可以用圖2.16來說明。用戶輸入字符串,發送請求給Struts 2框架的核心控制器FilterDispatcherFilterDispatcher根據配置,將請求轉發給ActionAction是業務控制器,來判斷用戶輸入的內容進行相應的操作。

2.16  HelloWorld的流程圖

2.2.3  Action業務控制器

如圖2.16所示,HelloWorld應用中的Action是業務控制器。通過第1章對Struts 2的簡單介紹,讀者應該知道,Struts 2Action可以是一個普通的Java類(POJO),與Struts 1有很大的不同,這裏的HelloWorld內容如代碼2.6所示。

代碼2.6  業務控制器HelloWord

package ch2.helloworld;

public class HelloWorld {

    
//定義msg屬性

    
private String msg;

    
//msg的get方法

    
public String getMsg() {

        
return msg;

    }


    
//msg的set方法

    
public void setMsg(String msg) {

        
this.msg = msg;

    }


    
//Action的execute()處理方法

    
public String execute() {

        
//判斷條件

        
if (getMsg().equals("")) {

            
//顯示錯誤信息

            System.out.println(
"no String input!");

            
//返回錯誤結果

            
return "error";

        }
 else {

            
//顯示用戶輸入的信息

            System.out.println(getMsg());

            
//返回一個處理成功結果

            
return "success";

        }


    }


}


 

1)如代碼2.6所示,HelloWorld沒有繼承任何類,也沒有實現任何接口,是一個標準的Java類。定義了一個msg屬性,對應用戶輸入的字符串,並提供了msgget()set()方法。execute()方法是實現業務控制器的默認方法,該方法只是返回一個String,並沒有特別之處。

2)在execute()方法中,對msg內容進行判斷,如果非空,則在控制檯打印該字符串,並返回一個“success”字符串,對應代碼2.5中的success.jsp頁面。

3)如果msg爲空,則在控制檯輸出一條警告,並返回“error”,對應error.jsp頁面。讀者會發現,Struts 2中的Action變得簡單易懂,很容易對其進行測試。

說明

Struts 2的業務控制器Action是一個普通的Java類。

 

2.2.4  視圖資源

到這裏爲止,該示例只缺少success.jsperror.jsp和一個用戶輸入界面HelloWorld.jsp,讀者可以使用文本編輯器建立,也可以使用各種網頁編輯器來製作,以提高效率。

1HelloWorld.jsp內容如代碼2.7所示。

代碼2.7  輸入界面HelloWorld.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>Hello World!!</title>

</head>

<body>

    
<!—form的action指向定義的action名稱 -->

<form id="form1" name="form1" method="post" action="HelloWorld.action">

  
<p>輸入信息:

    
<label>

    
<input name="msg" type="text" id="username" />

    
</label>

  
</p>

   
<p>

    
<label>

    
<input type="submit" name="Submit" value="提交" />

    
</label>

  
</p>

 
</form>

</body>

</html>

 

在代碼2.7所示內容中,formaction指向“HelloWorld”,提交一個namemsg的字符串,這是一個普通的JSP文件。

2success.jsp內容如代碼2.8所示。

代碼2.8  輸出界面success.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"%>

<!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=ISO-8859-1">

<title>返回界面</title>

</head>

<body>

HelloWorld,Struts 2!

</body>

</html>

 

代碼2.8所示內容很簡單,只是輸出“HelloWorld,Struts 2!”提示。

3error.jsp內容如代碼2.9所示。

代碼2.9  錯誤界面error.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"

    pageEncoding
="gb2312"
%>

<!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=ISO-8859-1">

<title>錯誤界面</title>

</head>

<body>

您沒有輸入內容!

</body>

</html>

 

error.jsp也只是提示錄入錯誤信息。

說明

Struts 2框架支持多種結果類型,默認類型支持JSP

 

2.2.5  運行HelloWorld

經過上述編寫,第一個Struts 2應用已經完成,將E:/myweb文件夾複製到C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps目錄下,啓動Tomcat,準備運行程序。

 在瀏覽器中輸入http://localhost:8080/bookcode/ch2/helloworld/HelloWorld.jsp,出現如圖2.17所示界面。

 在文本框中輸入“Hello World!”,單擊“提交”按鈕,出現如圖2.18所示界面。Tomcat控制檯會輸出如下內容:

2007-11-21 21:19:56 com.opensymphony.xwork2.validator.ActionValidatorManagerFactory <clinit>

信息: Detected AnnotationActionValidatorManager, initializing it...

Hello World!

 如果用戶沒有輸入內容,直接單擊“提交”按鈕,返回頁面如圖2.19所示。同時,Tomcat控制檯輸出如下內容:

no String input!

         

2.17  HelloWorld.jsp頁面           

              

2.18  success.jsp頁面

2.19  error.jsp頁面

成功運行這個示例,是瞭解Struts 2的良好開端,這是一個極其簡單的示例,並沒有完全展示Struts 2的所有特點,但給讀者帶來了一個感性瞭解Struts 2的體驗。

說明

Struts 2框架默認URL後綴不再是Struts 1.X框架的*.do,而是*.action

 

2.2.6  HelloWorld小結

初次完成一個Struts 2Hello World示例,讀者將會了解:

web.xml中需要加入Struts 2的加載配置。

struts.xml中定義Action,其中包含Action返回視圖的定義。

Struts 2Action是一個簡單的Java類,沒有絲毫特別之處。

Actionexecute()方法只是返回一個String

發佈了47 篇原創文章 · 獲贊 4 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章