初学JSP:配置第一个Struts的配置过程

http://tech.ddvip.com   2007年09月06日    社区交流

内容摘要:这篇文章主要针对有一定jsp编程经验的爱好者初学struts,如何配置struts过程的一个简单练习。

   4、在webjx/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到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"/>

责编:豆豆技术应用

正在加载评论...