在eclipse中使用Struts框架簡例 頂 原

永久更新地址:https://my.oschina.net/bysu/blog/edit/1783513

1.下載Struts2框架

http://struts.apache.org/download.cgi

下載完整版

在Struts下有5 個下載選項。
Full Distribution: 下載Struts 2 的完整版,包括Struts 2 框架的jar 包、示例以及依賴
步驗的jar包等;
Example Applications: 下載Struts 2 的示例應用,便於學習Struts 2,完整版已經包含
該選項;
Essential Dependencies Only: 僅下載Struts 2 的核心庫,完整版已包含該選項;
Documentation:下載Struts 2的相關文檔,包括Struts 2 的開發文檔、參考手冊以及
API文檔等,完整版已包含該選項;
Source: 下載Struts 2 的全部源代碼,完整版已包括該選項。

下載解壓後,目錄如下:


Struts 2 框架壓縮包解壓後的主要文件結構介紹如下:
apps——該文件夾下包含了5 個war文件,都是基於Struts 2 的Web應用示例,程序員可以通過這些示例來學習、研究Struts 2 技術;
docs——該文件夾下包含了Struts 2 的開發文檔、參考手冊以及API 文檔,開發時需要經常查閱這些幫助文檔;

lib——該文件夾下包含了Struts 2 框架的核心類庫以及Struts 2 框架的第三方類庫,開發基於Struts 2 框架的Web 應用時,需要將該目錄下的一些jar包複製到Web 應用的“WEB-INF/lib"路徑下;

src——該文件夾下包含了Struts 2 框架的全部源代碼。


在開發基於Struts2框架的Web應用時,並不需要應用到Struts 2 的全部功能,因此沒必要直接將Struts 2 框架壓縮包下的lib 子目錄中的所有jar 包複製到Web 應用的WEB-INF/lib 路徑下,但Struts 2 框架所需要的一些基本類庫必須增加到Web 應用中。
通常,Struts2框架壓縮包下的apps 子目錄中提供一個基於Struts 2 框架的空項目struts2-blank.war,該空項目中WEB-INF/ib 目錄下的jar 包就是Struts 2 框架所需要的一些基本類庫.

2.創建一個空的Web應用

創建步驟見《用eclipse創建動態web項目時沒有生成web.xml的補救措施》這一博文。

創建好之後,將Struts2框架中的基本類庫中的jar包複製到該項目的WEB-INF/lib路徑下。如下圖:

3.配置StrutsPrepareAndExecuteFilter

打開Web應用的配置文件web.xml(如果項目沒有該文件,則參考上面提到的那篇博文),在該配置文件中增加Struts2(不同Struts2版本<filter-class>中類的路徑不同)的核心控制器StrutsPrepareAndExecuteFilter的配置信息。配置StrutsPrepareAndExecuteFilter的代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>chapter02</display-name>
  <!-- 配置Struts 2框架的核心Filter -->
	<filter>
		<!--過濾器名 -->
		<filter-name>struts2</filter-name>
		<!--  配置Struts 2的核心Filter的實現類  -->
        <!--  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  -->
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<!--  讓Struts 2的核心Filter攔截所有請求  -->
	<filter-mapping>
		<!--過濾器名 -->
		<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>input.jsp</welcome-file>
  </welcome-file-list>
</web-app>

在web.xml中配置StrutsPrepareAndExecuteFilter過濾器後,Web應用就具備了Struts2框架的基本功能支持。

4.創建輸入視圖

在項目的WebContent根目錄下創建input.jsp輸入頁面,用於接收用戶輸入的數據。代碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<form method="post" action="user.action">
用戶名:<input type="text" name="userName" /><br/>
地址:<input type="text" name="address" /><br/>
電話:<input type="text" name="telephone" /><br/>
郵箱:<input type="text" name="email" /><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>

上述的頁面代碼定義了一個表單,表單中有4個文本輸入框和一個提交按鈕。該表單提交後,交由user.action處理。

5.創建業務控制器

在項目的src目錄下,創建com.bysu包,並添加用於處理用戶數據的業務控制器UserAction類,如下圖:

UserAction類代碼如下:

package com.bysu;

public class UserAction {

	private String userName;
	private String address;
	private String telephone;
	private String email;
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String execute() throws Exception{
		if(userName.length() > 0 && address.equals("青島")) {
			return "success";
		}else {
			return "error";
		}
	}
}


上述代碼中,UserAction 是個POJO,定義了4 個屬性: userNamedrss、telephone和email,這4 個屬性名必須跟input.jsp 頁面中的輸人文本框的name 屬性值對應; 然後,對這4 個屬性提供相應的getter/settert 方法。如此,當用戶提交input.jsp 頁面中的表單時,表單中輸人的數據會通過stter方法設置到相應的屬性中。在業務處理方法execute()方法中,判斷數據是否符合要求,並返回“success”或“error”字符串。

6.配置業務控制器

在項目src目錄下,添加Struts2的配置文件struts.xml。如下圖:

打開struts.xml配置文件,在該文件中配置UserAtion,代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<!-- 指定Struts2處於開發階段,可以進行調試 -->
	<constant name="struts.devMode" value="true" />

	<!-- Struts2的Action都必須配置在package裏,此處使用默認package -->
	<package name="default" namespace="/" extends="struts-default">
		<!-- 定義一個名爲user的Action,實現類爲com.bysu.UserAction -->
<action name="a" class="com.bysu.UserAction">
			<!-- 配置execute()方法返回值與視圖資源之間的映射關係 -->
			<result name="success">/result.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>

</struts>

在struts.xml配置文件中,配置了一個名爲a的Action,並指明該Action所對應的實現類爲com.bysu.UserAction。其實該Action跟上面input.jsp中的表單中的action屬性也有所關聯。在<result>元素中指明execute()方法返回值與視圖頁面資源之間的映射關係。

7.創建結果視圖

在項目的WebContent根目錄下創建result.jsp結果視圖。代碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
用戶名:${param.userName}</br>
地址:${param.address}</br>
電話:${param.telephone}</br>
郵箱:${param.email}</br>
</body>
</html>

上述頁面中使用EL表達式顯示用戶信息。

錯誤頁面error.jsp代碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>

經過以上的搭建,現在只差運行了。可就是這個運行搞了我整整一個下午。現在先把運行的步驟圖解一下吧!異常處理放後面。

運行:

1.啓動tomcat服務器。

雙擊tomcat安裝目錄\bin\startup.bat文件即可運行。

2.選中項目,右鍵—Run As—Run on Server(可能需要在這步之前創建一個Server)

創建Server的步驟,如下圖:

如果運行成功,則出現如下界面:

 

如果運行失敗的話,看你的具體報錯吧~我的報錯界面(外部瀏覽器、內部瀏覽器皆如此)如下:

運行後發現在tomcat的安裝目錄下的webapps並沒有出現所建立的工程名字,但是在外部瀏覽器上打開http://localhost:8080出現所期望的小貓畫面,證明我的tomcat配置應該沒有問題。

經網上查找:原來eclipse不像MyEclipse默認將項目部署到tomcat安裝目錄下的webapps中,而默認部署到工作目錄下的.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps中,tmp1文件夾裏面存放着該項目的信息。

針對以上問題,網上給出瞭解決方案,如下:

爲了使項目默認部署到tomcat安裝目錄下的webapps中,show view—>servers—>找到需要修改的tomcat—>右擊 ①停止eclipse內的Tomcat服務器(stop) ②刪除該容器中部署的項目(add and remove) ③清除該容器相關數據(clean) ④打開tomcat的修改界面(open)

⑤找到servers location,選擇第二個(User tomcat Installation) ⑥修改deploy path爲webapps ⑦保存關閉

需要說明的是①②③必須操作,否則下面的步驟會被置灰無法操作。

以上完成後,最後重啓一下eclipse。否則就算設置好了,好像也不生效。我就是隻是重啓了tomcat的服務,然後就繼續鼓搗,鼓搗了整整一個下午。搞了很多地方,重啓eclipse之後就可以了。所以也不敢確定具體是被我怎麼鼓搗好的。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章