jsp中读取properties文件 (jsp中读取properties文件,并把值设到js变量中)
方法一:
1>创建web_stderr.properties到src 下
2>web_stderr.properties文件内容:
1. common.username.error.null = UserName can not be null.
2. common.username.error.invalid = UserName is invalid.
3. common.password.error.null = Password can not bu null.
4. common.password.error.invalid = Password is invalid.
5. common.login.error.user = The user can not been stored or password is wrong.
6. common.request.parameter.error = Request parameter error.
7. common.function.title.null = The tilte of function or is null.
8. common.session.timeout = The session has already expired.Please login again.
3>.存储properties文件内容的js变量(common-checkmessage.jsp):
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8" import="java.util.ResourceBundle"%>
3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4. <html>
5. <head>
6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7. <title></title>
8. <%ResourceBundle res = ResourceBundle.getBundle("web_stderr"); %>
9. <script type="text/javascript">
10. var check_message_username_null = '<%=res.getString("common.username.error.null")%>';
11. var check_message_username_invalid = '<%=res.getString("common.username.error.invalid")%>';
12. var check_message_password_null = '<%=res.getString("common.password.error.null")%>';
13. var check_message_password_invalid = '<%=res.getString("common.password.error.invalid")%>';
14.</script>
15. </head>
16. <body>
17. </body>
18. </html>
此时只要在其他jsp中包含该jsp文件,就可以在jsp和js文件中使用这些变量。
4>可能出现的问题:
1) Can't find bundlefor base name web_stderr, locale zh_CN
解决方法:1.把web_stderr.properties放到src 下
2.重新加载reload tomcat
5>参考网址:http://blog.csdn.net/mrliu20082009/article/details/5317941?reload