环境为:Eclipse3.3+Myeclipse+
新建一个工程 取名(如:struts_jstl)
导入struts(Myeclipse-->project capabilities-->add struts capabilities) 选择属性文件存放的包路径(这里是com)
之后 struts-config.xml 会自动将其加载(<message-resources parameter="com.ApplicationResources"/>)
com包下会自动生成一个属性文件(ApplicationResource.properties)。将其更名为:ApplicationResource_zh_CN.properties (中文)
然后再新建一个属性文件 命名为:ApplicationResource_en_US.properties (英文)。为了方便,建议安装国际化插件,资源已上传。
然后导入jstl 导入方法和struts相同,新建一个JSP页面(index.jsp),内容如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index</title>
</head>
<body>
<center>
<c:if test="${not empty param.lan}">
<c:if test="${param.lan=='zh'}">
<fmt:setLocale value="zh_CN" scope="session"/>
</c:if>
<c:if test="${param.lan=='en'}">
<fmt:setLocale value="en_US" scope="session"/>
</c:if>
</c:if>
<fmt:setBundle basename="com.ApplicationResources"/>
<fmt:message key="label.username"/>
<hr>
<form action="" name="form1" method="post">
<input type="radio" name="lan" value="zh" οnclick="javascript:form1.submit()">中文
<input type="radio" name="lan" value="en" οnclick="javascript:form1.submit()">英文
</form>
<hr>
<a href="next.jsp"><fmt:message key="label.next"/></a>
</center>
</body>
</html>
相信都能看懂,不再赘述。
【注意】编写完这个国际化小程序并发布后有可能出现错误,如下:
页面不能正常显示,报错如According to TLD or attribute directive in tag file, attribute test does not accept any expressions等等
这可能是你使用的JSP版本和jstl版本不兼容造成的。
通常情况是jstl1.0和JSP1.X搭配,但现在一般都是JSP2.0了,所以导入jstl时JSP如果版本是2.0的话一定要导入jstl1.1.
还有可能版本兼容了,但还有错,那出错位置一般是你的jstl标签处,如:<c:if test="${not empty param.lan}">这可能是EL没有打开,处于关闭状态(因为${not empty param.lan}是EL的)。这时需将jstl标签库中的c.tld打开找到
<name>test</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
后 将<rtexprvalue>false</rtexprvalue>中的false改成true就可以了。