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

本文详细介绍了如何在Tomcat中配置Struts1.2.9,包括创建虚拟目录、复制库文件、配置web.xml和struts-config.xml,以及解决Cannot find message resources错误的方法。通过实例展示了ApplicationResources.properties文件的设置和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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.youkuaiyun.com/neonlight 转载请注明出处,谢谢!2006年08月12日

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值