Search code examples
javasessionserializableresourcebundlecustom-tags

How to store ResourceBundle object In session java?


I have taglibrary for localization that contains 3 tags and one tag file:

SetLocale - set selected Locale to session
SetBundle - tag set's bundle to session by using Locale which Is aquired by session

<?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>

I have tag file for setting Locale to Session

   <%@ 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);
    %>

Tag Handler for setBundle tag is :

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);
    }
}

How can I store ResourceBundle in Session?It works but IDE tell me that ResourceBundle dont implement Serializable so I need to do it, but how?


Solution

  • It is theoretically possible to store all keys and values from a ResourceBundle in a serializable object, but you don’t need to. You can just store the Locale and the name of the ResourceBundle in your session.

    A web application has the same classpath for its entire lifetime, so the ResourceBundle will always be available by that name.