JAVA后台程序以及JSP页面如何获取properties文件中的值

        在实际开发中,我们经常会将一些属性写入到properties文件中,方便我们在开发或维护中的操作。这样做的好处十分明显,当我们需求或运行环境发生变化时,只需更改相应的配置文件即可符合要求。但这样做的同时也带来了一些开发上的问题,比如我们如何获取properties文件中的值。

ApplicationProperties.properties配置文件内容:

FtpUrl = http\://192.168.0.14\:8080/



一、JSP文件中获取该属性值得方法:
 

<%@ page language="java" import="java.util.*,com.sdzn.entity.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ path + "/";
    // properties 配置文件名称
    ResourceBundle res = ResourceBundle.getBundle("ApplicationResources");
%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <base href="<%=basePath%>">
        <title>测试</title>
        <script type="text/javascript">
            // 获取properties配置文件中的属性值
            var ftpurl= <%=res.getString("FtpUrl ")%>;
        </script>
    </head>
    <body ></body>
</html>


二、Java后台程序中获取该属性值得方法:
 
import java.util.PropertyResourceBundle;

// properties 配置文件名称
PropertyResourceBundle res = (PropertyResourceBundle) PropertyResourceBundle.getBundle("ApplicationResources");
// 获取properties配置文件中的属性值
String ftpurl = res.getString("FtpUrl ");


 

Java 后台实现页面多语言需要进行以下几个步骤: 1. 在后台代码创建一个语言包类,用于存储不同语言的页面内容。这个类可以使用 Properties 类型,其每个键对对应一个页面元素的 ID 和对应语言的文本内容。 ``` public class LanguageBundle { private Properties bundle; public LanguageBundle(String locale) { bundle = new Properties(); try { bundle.load(new FileInputStream("locale/" + locale + ".properties")); } catch (IOException e) { e.printStackTrace(); } } public String getText(String key) { return bundle.getProperty(key); } } ``` 2. 在后台代码设置一个默认语言和用户选择的语言,然后根据用户的语言偏好加载相应的语言包。这可以使用 Java 的 Locale 类和 Cookie 类来实现。 ``` public class LanguageService { private static final String DEFAULT_LOCALE = "en_US"; public static LanguageBundle getLanguageBundle(HttpServletRequest request) { String locale = request.getParameter("locale"); if (locale == null) { Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("locale")) { locale = cookie.getValue(); break; } } } } if (locale == null) { locale = DEFAULT_LOCALE; } Cookie cookie = new Cookie("locale", locale); cookie.setMaxAge(365 * 24 * 60 * 60); response.addCookie(cookie); return new LanguageBundle(locale); } } ``` 3. 在 JSP 页面使用 JSTL 标签库和 EL 表达式来动态加载页面元素的文本内容。这可以使用 fmt 和 c 标签库来实现。 ``` <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> ... <fmt:message key="hello" bundle="${languageBundle}" /> ``` 总之,在 Java 后台实现页面多语言需要进行一些编码工作,同时需要考虑到用户体验和可访问性等方面的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值