這篇文章主要針對有一定jsp編程經驗的愛好者初學struts,如何配置struts過程的一個簡單練習。
首先下載struts軟件包,到http://struts.apache.org/下載struts,struts各版本的差異很大,這里已struts1.2.9版本為例,解壓縮包內容如下:
1、在tomcat安裝目錄下的webapps目錄中建立一個VeVb目錄。這樣就可以通過訪問"http://localhost:8080/VeVb"訪問"VeVb"這個目錄。
2、在你創建的目錄VeVb中,建立web-inf目錄,在web-inf中建立classes、lib和tld文件夾。將壓縮包struts-1.2.9-binlib文件夾中的commons-*.jar(*代表任意位任意字符)和struts.jar文件拷貝到建立的VeVb/web-inf/lib目錄下,然后將struts中的標簽庫文件struts-*.tld(*代表任意位任意字符)拷貝到VeVb/web-inf/tld目錄下
3、在VeVb/web-inf/目錄下建立一個web.xml文件,文件內容如下:
<?xmlversion="1.0"encoding="iso-8859-1"?>
<!doctypeweb-app
public"-//sunmicrosystems,inc.//dtdwebapplication2.2//en"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<display-name>strutsblankapplication</display-name>
<!--standardactionservletconfiguration(withdebugging)-->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.actionservlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>applicationresources</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/web-inf/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!--standardactionservletmapping-->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--theusualwelcomefilelist-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--strutstaglibrarydescriptors-->
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/web-inf/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/web-inf/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/web-inf/tld/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/web-inf/tld/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/web-inf/tld/struts-tiles.tld</taglib-location>
</taglib>
</web-app>
4、在VeVb/web-inf/目錄下建立一個struts-config.xml文件,文件內容如下:
<?xmlversion="1.0"encoding="iso-8859-1"?>
<!doctypestruts-configpublic
"-//apachesoftwarefoundation//dtdstrutsconfiguration1.2//en"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
</form-beans>
<global-forwards>
</global-forwards>
<action-mappings>
</action-mappings>
<message-resourcesparameter="applicationresources"/>
</struts-config>
說明:web.xml和struts-config.xml這兩個文件可以壓縮包struts-1.2.9-binwebappsstruts-blank.war文件直接拷貝到tomcat安裝目錄下的webapps目錄中,啟動tomcat服務器,struts-blank.war就會自動解壓縮成一個文件夾struts-blank,復制struts-blank/web-inf下web.xml和struts-config.xml到VeVb/web-inf下修改對應配置。
5、然后在web-inf/classes中建立applicationresources.properties文件,其中輸入:
index.title=mystruts
6、在webapps/VeVb目錄建立test.jsp文件,有如下內容:
<%@pagecontenttype="text/html;charset=gbk"%>
<%@tagliburi="/tags/struts-logic"prefix="logic"%>
<%@tagliburi="/tags/struts-bean"prefix="bean"%>
<%@tagliburi="/tags/struts-html"prefix="html"%>
<html:htmllocale="true">
<head>
<title>
<bean:messagekey="index.title"/>
</title>
</head>
<body>
你好 struts!
</body>
</html:html>
隨后用http://localhost:8080/VeVb/test.jsp訪問該文件,如果頁面顯示"你好struts!"字樣,并且頁面標題是mystruts就是成功了。
配置中注意事項:
如果出現“cannotfindmessageresourcesunderkeyorg.apache.struts.action.message”,是說明找不到applicationresources.properties,要注意三方面設置。
第一:在web.xml適當位置要有如下設置:
<init-param>
<param-name>application</param-name>
<param-value>applicationresources</param-value>
</init-param>
第二:在struts-config.xml中適當位置要有如下設置:
<message-resourcesparameter="applicationresources"/>
第三:確保applicationresources.properties文件在你建立的web-infclasses文件夾中,而且其中有關于index.title的設置(當然,以你要提取的key名稱為準)。
另外說明,你也可以把applicationresources.properties放到classes文件夾下其它目錄,同時修改struts-config.xml中的對應設置。例如:
將“applicationresources.properties”放入web-infclasses est文件夾下。struts-config.xml中的對應設置:
<message-resourcesparameter="test/applicationresources"/>
新聞熱點
疑難解答