1、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
<display-name>
struts2</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</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>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、struts.xml文件
這個文件的配置是最為重要的,也是比較容易出問題的地方,初學(xué)者對其中很多的參數(shù)不太明白,導(dǎo)致調(diào)試時候經(jīng)常出錯。下面先給出一個配置的代碼:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"
<struts>
<include file="struts-default.xml"/>
<package name="package1" extends="struts-default">
<action name="Hello" class="tutorial.HelloWorld">
<result>HelloWorld.jsp</result>
</action>
<action name="HelloWorld" class="package1.HelloWorld">
<result>HelloWorld.jsp</result>
</action>
</package>
</struts>
1)對于<include file="struts-default.xml"/>這行代碼,可以要也可以不要。
2)package的name是可以虛構(gòu)的,并不一定要在classes目錄下要有一個與這個name名對應(yīng)的包名;像上面的配置文件中的name="package1"可以隨便改成name="aaa"等是不影響結(jié)果的,只不過這個package名可以使得多個action聚合在一起,對大型的模塊設(shè)計的時候是很有幫助的。
3)<action name="HelloWorld" class="package1.HelloWorld">中的HelloWorld這個name并不一定要與package1包中的類名相同,比如<action name="Hello" class="tutorial.HelloWorld">這樣兩個名字完全不一樣是可以的,但是"Hello"這個name必須與輸入的jsp頁面里面的action內(nèi)容是一致的,如<s:form action="Hello">
4)從上面的配置文件可以看出,在classes目錄下面有兩個包,一個是tutorial,一個是package1。
3、struts.properties文件
這個文件最簡單,它與struts.xml文件一樣,都是位于classes目錄下面。其內(nèi)容為:
struts.devMode = true
struts.enable.DynamicMethodInvocation = false的
4、建議初學(xué)者先看看為Struts 2.0做好準備這篇文章,這里每個步驟講的非常的詳細,但是有些細節(jié)作者沒有寫出來,讓很多初學(xué)者調(diào)試的時候出很多錯誤,比如The requested resource (/項目名/HelloWorld) is not available.,java.lang.NullPointerException等等。
1)關(guān)于工具web tools platform,這是一個eclipse集成了很多web開發(fā)下所需工具的一個比較好的平臺,它在eclipse的網(wǎng)站下面
http://www.eclipse.org/webtools/有下載。打開這個頁面的右邊download欄目下面有wtp的下載,點擊這個去下載,如果不行,就再仔細找找了。
2)如果出現(xiàn)The requested resource (/項目名/HelloWorld) is not available.的話,就仔細找找看是不是輸入的時候把配置文件等等地方寫錯了,對于初學(xué)者最容易把其中的字母搞錯,比如struts是不寫成了其它的形式,反正出現(xiàn)這個錯誤就是說訪問的資源不存在,那就肯定要么是配置文件出錯了,要么是你的類文件沒有放在classes目錄下面,無外這兩種情況。錯了不要緊,沒有錯誤,哪會有進步呢。
3)如果你真的發(fā)現(xiàn)什么錯誤都沒有,但就是盡管調(diào)試了半天還是調(diào)試不成功,那就考慮你的JDK版本是不是和你下載的wtp搭配了,建議把原來低版本的JDK卸載,裝上新去下載的JDK,寫這篇博客時,最新版本是jdk-1_5_0_10。如果還不行,你就把tomcat或者其它的web服務(wù)器換版本,總之要不怕錯誤和失敗,要勤于不斷的調(diào)試。
posted on 2007-08-13 01:41
jadmin 閱讀(49)
評論(0) 編輯 收藏