【问题标题】:How to store ResourceBundle object In session java?如何在会话 java 中存储 ResourceBundle 对象?
【发布时间】:2021-08-24 19:08:15
【问题描述】:

我有一个包含 3 个标签和一个标签文件的本地化标签库:

SetLocale - 将选定的区域设置为会话
SetBundle - 使用会话获取的语言环境将标签集的捆绑包到会话

<?xml version="1.0" encoding="UTF-8"?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
        version="2.1">

    <tlib-version>1.0</tlib-version>
    <short-name>myshortname</short-name>
    <uri>http://mycompany.com</uri>

    <tag>
        <name>setBundle</name>
        <tag-class>com.example.FinalProjectPM.web.handlers.BundleHandler</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>basename</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>LValueParam</name>
        <tag-class>com.example.FinalProjectPM.web.handlers.LValueParamHandler</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>params</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>message</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>checkParam</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <type>boolean</type>
        </attribute>
    </tag>
    <tag>
        <name>localeValue</name>
        <tag-class>com.example.FinalProjectPM.web.handlers.LocalValueHandler</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>key</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag-file>
        <name>setLocale</name>
        <path>/WEB-INF/tags/setLocale.tag</path>
    </tag-file>
  
</taglib>

我有标签文件用于设置区域设置为会话

   <%@ tag import="java.util.Locale" %>
    <%@ tag language="java" body-content="empty" pageEncoding="utf-8" %>
    <%@ attribute name="name" required="true" rtexprvalue="true" type="java.lang.String" %>
    <%
    Locale locale = new Locale(name);
    session.setAttribute("tagLocale",locale);
    %>

setBundle 标签的标签处理程序是:

public class BundleHandler extends SimpleTagSupport {
    private String basename;

    public void setBasename(String basename) {
        this.basename = basename;
    }
    @Override
    public void doTag() {
        PageContext pageContext = (PageContext)getJspContext();
        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
        HttpSession session = request.getSession();
        Locale gotLocale = (Locale)session.getAttribute("tagLocale");
        ResourceBundle bundle = ResourceBundle.getBundle(basename,gotLocale);
        session.setAttribute("Bundle",bundle);
    }
}

如何将 ResourceBundle 存储在 Session 中?它可以工作,但 IDE 告诉我 ResourceBundle 没有实现 Serializable,所以我需要这样做,但是如何?

【问题讨论】:

  • 与其将整个 ResourceBundle 存储在会话中,为什么不只存储 Locale 和/或 ResourceBundle 的名称? ResourceBundle 不是对整个 Web 应用程序都可用吗?
  • @VGR 好主意,感谢您的回复。您可以发布您的答案,以便我将您的答案标记为正确

标签: java session serializable resourcebundle custom-tags


【解决方案1】:

理论上可以将 ResourceBundle 中的所有键和值存储在可序列化对象中,但您不需要这样做。您可以只在会话中存储 Locale 和 ResourceBundle 的名称。

Web 应用程序在其整个生命周期内都具有相同的类路径,因此 ResourceBundle 将始终以该名称可用。

【讨论】:

    猜你喜欢
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 2012-01-26
    • 2014-01-17
    相关资源
    最近更新 更多