初学Struts2,跟着Tutorials一直做,因为有做struts1项目的经验,前面都很顺利,HelloWorld从页面到action也顺利打通。可是学到读取properties文件却卡壳了,一直显示不出properties文件中设定的常量值。
properties文件是这么设定的(文件名:hello.properties):
personBean.firstName=Daisy
personBean.lastName=Last name
personBean.age=Age
personBean.email=Email
thankyou=Thank you for registering %{personBean.firstName}.
按教程指示放在了src目录下。
struts.xml中加入一句:
<constant name="struts.custom.i18n.resources" value="hello"></constant>
这句教程里没写,都是百度出来的,坑爹的教程!
index.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<s:text name="personBean.firstName"/>
</body>
</html>
按理说,到这里就应该显示出页面了。
好吧,页面是出来了,但是内容却不是我们想要的。
只显示了properties文件中的key值,却没有显示value值。
百度半天也没整明白什么原因,最后看了看struts2的例子源码,终于发现问题所在,原来是struts.xml文件的位置!
之前做struts1项目时,struts-config.xml文件都是放在WEB-INF文件夹下,所以现在学习struts2,我也习惯性把struts.xml文件放在了这个文件夹里,实际上正确的位置应该是src文件夹。
正确的目录结构如下:
把struts.xml文件放到src文件夹后,重新启动tomcat,页面正常显示出来了。