0

当我将“name”属性保存为简单的字符串数据时,以下代码有效,但现在我想将“name”保存为 JSON 数据,例如 {"en":"White Rice","cn":"白米", "th":"ข้าวขาว"},并根据locale语言显示名称值。我能怎么做?

<s:iterator value="categorylist">
    <li class="mn_menu-active">
        <a href="#"> 
              <span><s:property value="name" /></span>

              <div class="clear"></div>
              </a>
              <ul>
                <s:iterator value="children">
                    <li>
                        <a href="#">
                         <span class="ms_txt"><s:property value="name" /></span>
                         <div class="clear"></div>
                         </a>
                     </li>

                </s:iterator>
              </ul>

          </li>
</s:iterator>
4

1 回答 1

0

听说我实现了一个类来处理它。

包 com.tsd.json;

导入 javax.servlet.http.HttpServletRequest;导入 javax.servlet.http.HttpServletResponse;

导入 org.apache.struts2.ServletActionContext;

导入 com.opensymphony.xwork2.ActionContext;导入 com.opensymphony.xwork2.util.ValueStack;

导入 java.util.Locale;

导入 net.sf.json.JSONObject;

公共类 LocaleHandler {

public static String Locale(String name)
{
    ValueStack stack = ActionContext.getContext().getValueStack();


    String result="";
    HttpServletRequest request = ServletActionContext.getRequest();
    Locale locale = (java.util.Locale)request.getSession().getAttribute("WW_TRANS_I18N_LOCALE");
    String jsonData =(String)stack.findValue(name);
    JSONObject json=JSONObject.fromObject(jsonData); 
     if(locale ==null)
     {
         HttpServletResponse response=ServletActionContext.getResponse();
         locale=response.getLocale();
     }
     if (locale instanceof Locale)
     {
         result= json.getString(locale.getLanguage()+"_"+locale.getCountry());
     }
    return result;
}

}

并修改jsp如下:

 <s:iterator value="categorylist">
              <li class="mn_menu-active">
                  <a href="#"> 
                     <span>
                       <%=LocaleHandler.Locale("name") %>
                     </span>

                     <div class="clear"></div>
                  </a>
                  <ul>
                    <s:iterator value="children">
                        <li>
                            <a href="#">
                             <span class="ms_txt"> <%=LocaleHandler.Locale("name") %></span>
                             <div class="clear"></div>
                             </a>
                         </li>

                    </s:iterator>
                  </ul>

              </li>
          </s:iterator>
于 2013-11-05T06:07:35.110 回答