您的位置:首页 > 运维架构 > Tomcat

Tomcat与Struts1.2.9的简单例子和一个常见问题

2010-10-29 23:31 525 查看
Tomcat与Struts1.2.9的简单例子和一个常见问题(原创)

  首先说明,我要解释的问题是Cannot find message resources under key org.apache.struts.action.MESSAGE错误,如果对你没有帮助,就请不要继续看下去了。

  先讲解一下这个例子,首先下载Struts软件包,我下的是Struts1.2.9,你可以去官方网站(http://struts.apache.org/index.html)下载。

  好,下面开始配置这个简单的例子:

  我是在Tomcat中配置的,首先建立个虚拟映射目录(如果不会就参阅其他文章),比如"D:/webapps/mystruts/"映射成"/mystruts",这样你就可以通过访问"http://localhost:8080/mystruts"访问"D:/webapps/mystruts/"这个目录。

  在你创建的目录中,建立WEB-INF文件夹,在WEB-INF中建立classes、lib和tld文件夹。解压缩Struts软件包,将lib文件夹下的commons-*.jar(*代表任意位任意字符)和struts.jar文件全拷贝到你所建立的WEB-INF/lib文件夹中,将压缩包lib文件夹下的*.tld(*代表任意位任意字符)全部拷贝到WEB-INF/tld文件夹中。

  在WEB-INF目录下建立两个文件web.xml、struts-config.xml分别写入如下代码:

web.xml中写入:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app

PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"

"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<web-app>

<display-name>Struts Blank Application</display-name>

<!-- Standard Action Servlet Configuration (with debugging) -->

<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>

<!-- Standard Action Servlet Mapping -->

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<!-- The Usual Welcome File List -->

<welcome-file-list>

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

</welcome-file-list>

<!-- Struts Tag Library Descriptors -->

<taglib>

<taglib-uri>/struts-bean</taglib-uri>

<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/struts-html</taglib-uri>

<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/struts-logic</taglib-uri>

<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/struts-nested</taglib-uri>

<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/struts-tiles</taglib-uri>

<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>

</taglib>

</web-app>

struts-config.xml中写入:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE struts-config PUBLIC

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

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

<struts-config>

<form-beans>

</form-beans>

<global-forwards>

</global-forwards>

<action-mappings>

</action-mappings>

<message-resources parameter="ApplicationResources"/>

</struts-config>

  然后在WEB-INF中建立ApplicationResources.properties文件,其中输入:

index.title=MyStruts

  在mystruts(以你自己建立的名字为准)目录建立test.jsp文件,有如下内容:

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

<%@ taglib uri="/struts-logic" prefix="logic" %>

<%@ taglib uri="/struts-bean" prefix="bean" %>

<%@ taglib uri="/struts-html" prefix="html" %>

<html:html locale="true">

<head>

<html:base/>

<title>

<bean:message key="index.title"/>

</title>

</head>

<body>

你好 Struts!

</body>

</html:html>

  随后用http://localhost:8080/mystruts/test.jsp(以你自己建立的名字为准)来访问该文件,如果页面显示"你好 Struts!"字样,并且页面标题是MyStruts就是成功了。

  下面谈问题。如果出现Cannot find message resources under key org.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-resources parameter="ApplicationResources"/>

  推荐句话多插入如下位置:</action-mappings>"放到这里"</struts-config>

  第三:确保ApplicationResources.properties文件在你建立的WEB-INF/classes文件夹中,而且其中有关于index.title的设置(当然,以你要提取的key名称为准)。

  另外,我还要说明,你也可以把ApplicationResources.properties放到classes文件夹下其它目录,但struts-config.xml中的设置要改。例如:

  <message-resources parameter="test/ApplicationResources"/>

  你就要把ApplicationResources.properties放入WEB-INF/classes/test文件夹下。

  neonlight <neonlight@live.cn>,BLOG:http://blog.csdn.net/neonlight 转载请注明出处,谢谢!2006年08月12日
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: