bean:相当于Java代码中的消息? [英] bean:message equivalent in java code?

查看:68
本文介绍了bean:相当于Java代码中的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Struts 1.2,需要在JSP页面中引用一些国际化字符串的值.通常,我会使用< bean:message>标记,但是我需要从Java代码(由<%...%>包围)中引用它.

I'm using Struts 1.2 and I need to reference the value of some internationalized strings in a JSP page. Normally I would do this using the < bean:message > tag, but I need to reference it from the Java code (surrounded by <% ... %>) instead.

我该怎么做?

例如:

<% 
person.setName("John Smith");
person.setOccupation("Chef");   // I want to internationalize this string
%>

推荐答案

我认为这是一种实现方法.

I think this is one way to do it.

在struts-config.xml中,如果您具有以下内容:

Inside struts-config.xml, if you have the following:

<message-resources parameter="ABC"/>

然后您执行以下操作:

在JSP的顶部:

<%@ page import="java.util.Locale" %>
<%@ page import="org.apache.struts.Globals" %>
<%@ page import="org.apache.struts.util.MessageResources" %>

在JSP中的某个地方:

Somewhere in the JSP:

<%    
MessageResources mr = MessageResources.getMessageResources("ABC");
Locale locale = (Locale) session.getAttribute(Globals.LOCALE_KEY);

person.setName("John Smith");
person.setOccupation(mr.getMessage(locale, "Chef"));
%>

这篇关于bean:相当于Java代码中的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆