In a javaWeb project, some commonly used data can be placed in a.properties file for easy modification and maintenance. What I need to implement is to display the configuration information for the.properties file in the.jsp for front-end display. So far I know of only two ways to do this: 1, use ResourceBundle 2, and use the JSTL tag FMT :message

To prepare

  • First you need to create a.properties file
  • The self-created configuration file is stored in the same way as the configuration file in the projectresourcesdirectory
  • The configuration file is conf-my.properties
  • The details of the configuration file are as follows
Name = hao version = o1.0
Copy the code

1. Use ResourceBundle

  • Introduce a ResourceBundle in the JSP page

<%@ page language="java" import="java.util.ResourceBundle" %>

  • Load the properties file using ResourceBundle

ResourceBundle resource = ResourceBundle.getBundle("conf-my"); // No suffixes required

  • Reading configuration Values

resource.getString("version"); The key / / properties

Concrete example

<%@ page language="java" import="java.util.ResourceBundle" %> <% ResourceBundle resource = ResourceBundle.getBundle("conf-my"); // do not need suffix %><! DOCTYPEhtml>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <! Resource. GetString (key) -->
        <title><%=resource.getString("name") %></title>
    </head>
    <body>
    	<p>version:<%=resource.getString("version") %></p>
    	
        <script type="text/javascript">
        	 // How to use js
            var version= 'resource.getString("version")';
        </script>
    </body>
</html>
Copy the code




2. Use the JSTL tag FMT :message

  • Introduce FMT tags in JSTL

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

  • Load the properties file using FMT :setBundle (basename: filename, var: variable name)

<fmt:setBundle basename="conf-my" var="conf-my" />

  • Read configuration values using FMT :message (key configuration file key, var: variable name, bundle reference configuration file)

<fmt:message key="varsion" var="v" bundle="${conf-my}" />

  • Read configuration values using EL expressions

${v}

Concrete example

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<! Load systemInfo config file -->
<fmt:setBundle basename="conf-my" var="conf-my" />
<! -- read config value varsion -->
<fmt:message key="varsion" var="v" bundle="${conf-my}" />
<fmt:message key="name" var="name" bundle="${conf-my}" />

<! DOCTYPEhtml>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <! Print configuration values directly -->
      <title>${name}</title>
    </head>

    <body>
    	 <p>version:${v} %></p>
    	
        <script type="text/javascript">
        	 // How to use js
            var version= 'resource.getString("version")';
        </script>
    </body>
</html>

Copy the code




The code problem

Some people read Chinese characters in the properties file, and there will be garbled characters. I am one of them. I found some solutions in Baidu, but most of them are processed in the background, and I don’t want to be so troublesome. Finally I found a simple solution

1. Convert Chinese characters in the.properties configuration file to Unicode

\ U963F \ U8C6a, after conversion about this

nameO = haoversion=1.0
Copy the code

Modified to

name=\u963f\u8c6a
version=1.0
Copy the code

It will display normally.

2. Try this if you use IDEA development tools

  • The encoding may not be UTF-8 when the file is created;
  • You need to set this up in your IDEA
  • Ctrl + Alt + sOpen the Settings
  • If it still doesn’t work, delete the.properties file and create a new one