Search code examples
javajspinternationalizationstruts2resourcebundle

Best practice struts/jsp/i18n/resourceBundle


I am using Struts 2 and Apache tiles, and I'm new to both. I am trying to "clean" some existing sources that don't feel correct to me (tell me if I'm wrong).

I have the following structure :

  • in a layout.jsp :

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title><tiles:getAsString name="title" /></title>
        <tiles:insertAttribute name="header" />
    </head>
    <body>
        <div id="content-size-and-border">
            <s:if test="display!='nomenu'">
                <tiles:insertAttribute name="menu" />
            </s:if>         
            <div id="maincontent">
                <tiles:insertAttribute name="maincontent" />
            </div>
        </div>
    
    </body>
    

The main content part displays various jsp/action depending on the item-menu clicked. The menu part uses some java code directly in the jsp to generate a number of sub-folders by iterating on a list.

<li class="highlight sousmenu"><a href="#"><s:text
    name="menu.demarrage" /></a>
    <ul class="niveau2">

        <%
        Locale language = (Locale) session.getAttribute("WW_TRANS_I18N_LOCALE"); 
        // the attribute (used by i18n struts interceptor) 
        // set in session when the user changes the language

        if (language == null)
            language = request.getLocale() ;
        // if the language was not changed, get the default Locale from browser

        User user = ((CustomUserDetails) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getBpmUser();
            if (user != null) {
                for (Iterator iterator = user.getProcesses().iterator(); iterator
                        .hasNext();) {
                    String processToStart = (String) iterator.next();
                    String processPath = BpmUiConstantes.BPMUI_PROCESS_CONFIG_ROOT + "/" + processToStart ;
                    String processLib = "process." + processToStart + ".label";                              
        %>

        <li>
            <a href="<%=request.getContextPath()%>/restricted/DemarrerProcessAvecTache?processName=<%=processToStart%>">
                <fmt:setLocale value="<%=language%>"/>
                <fmt:bundle basename="<%=processPath%>">
                    <fmt:message key="<%=processLib%>"/>
                </fmt:bundle>
            </a>
        </li>

        <%
                }   
            }
        %>
    </ul>
</li>

I was wondering if there was a better way to achieve the same result, without the java code in the jsp. Is it important to remove the java code from the jsp from a conception point of view ?

The application uses struts i18n interceptor for the language changes. Is there a way to have the menu use a struts i18n interceptor in some way?


Solution

  • I would definitely look more into JSTL core (C tag to use foreach) and fmt(for the locale- language), with these two libraries you should be able able to safely remove the embedded java code into your page and use a more congruent/safe approach

    Here is the oracle webpage to JSTL so you can have some reference

    JSTL Reference

    Feel free to ask if you have any other questions.