最佳实践struts / jsp / i18n / resourceBundle

时间:2012-07-09 16:04:13

标签: java jsp internationalization struts2 resourcebundle

我正在使用Struts 2和Apache磁贴,我对两者都不熟悉。我试图“清理”一些对我来说感觉不正确的现有来源(告诉我,如果我错了)。

我有以下结构:

  • 在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>
    

主要内容部分根据单击的项目菜单显示各种jsp /动作。 菜单部分直接在jsp中使用一些java代码,通过迭代列表生成许多子文件夹。

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

我想知道是否有更好的方法来实现相同的结果,而没有jsp中的java代码。从概念的角度来看,从jsp中删除java代码是否重要?

该应用程序使用struts i18n拦截器进行语言更改。有没有办法让菜单以某种方式使用struts i18n拦截器?

2 个答案:

答案 0 :(得分:1)

我肯定会更多地了解JSTL核心(使用foreach的C标记)和fmt(用于语言环境),使用这两个库,您应该能够安全地将嵌入的Java代码移除到页面中并使用更一致/安全的方法

以下是JSTL的oracle网页,您可以参考

JSTL Reference

如果您有任何其他问题,请随时询问。

答案 1 :(得分:0)

我将在上一篇文章中添加一些答案:

没有办法让这个菜单使用struts i18n拦截器,最重要的是,它没用,因为这个拦截器不会设置ResourceBundle的Locale或fmt:bundle的Locale。有必要明确指定Locale(就像我在代码中所做的那样)=&gt;这部分不能改变。

我仍然不确定从jsp中删除这个java代码的重要性。

相关问题