这篇文章主要针对有一定jsp编程经验的爱好者初学struts,如何配置struts过程的一个简单练习。
首先下载Struts软件包,到http://struts.apache.org/下载 Struts,Struts各版本的差异很大,这里已Struts1.2.9版本为例,解压缩包内容如下:
1、在tomcat安装目录下的webapps目录中建立一个webjx目录。这样就可以通过 访问"http://localhost:8080/webjx"访问"webjx"这个目录。
2、在你创建的目录webjx中,建立WEB-INF目录,在WEB-INF中建立 classes、lib和tld文件夹。将压缩包struts-1.2.9-binlib文件夹中的commons-*.jar(*代表任意位任意字符) 和struts.jar文件拷贝到建立的webjx/WEB-INF/lib目录下,然后将Struts中的标签库文件struts-*.tld(*代表 任意位任意字符)拷贝到webjx/WEB-INF/tld目录下
3、在 webjx/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、在webjx/WEB-INF/目录下建立一个struts-config.xml文件,文件内容如下:
<?xmlversion="1.0"encoding="ISO-8859-1"?& gt;
<!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到 webjx/WEB-INF下修改对应配置。
5、然后在WEB- INF/classes中建立ApplicationResources.properties文件,其中输入:
index.title=MyStruts
6、在webapps/webjx目录建立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/webjx/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"/>