Struts 2使用JSTL而不是s:text访问消息

时间:2014-05-24 15:48:58

标签: jsp struts2 jstl

在struts 2中

<s:property value="#pageTitle" />

(相当于......)

JSTL: ${pageTitle}

我使用更紧凑的JSTL版本。

我们可以用应用程序消息做同样的事吗?!要从消息资源中获取文本,我们按以下方式执行:

<s:text name="label.sample" />

现在,是否有任何JSTL版本而不是<s:text/>


与此同时,我试图找到一种方法将textprovider传递给jsp。但我找不到方法

public class BaseActionSupport extends ActionSupport{

  //Same as ActionSupport
  private TextProvider Provider; //with setter and getter

  public TextProvider getSampleTextProvider() {

            TextProviderFactory tpf = new TextProviderFactory();
            if (container != null) {
                container.inject(tpf);
            }
            return  tpf.createInstance(getClass(), this);
  }

}

在jsp中:

${provider.text("label.password")} //Error The function text must be used with a prefix when a default namespace is not specified

这是一种正确的方法吗?!

PS:在http://notsoyellowstickies.blogspot.com/2011/05/making-struts-2-and-jstl-share-message.html提到我可以某种方式分享struts 2和jstl消息,但我仍然应该使用<fmt:message key="sample.label"/>

1 个答案:

答案 0 :(得分:2)

公开了操作对象,可以使用其方法:

${action.getText('sample')} 

这是JSTL方法调用,您需要JSTL 2.2(例如:tomcat 7)

请参阅: http://mail-archives.apache.org/mod_mbox/struts-user/201405.mbox/%3CCAMopvkNtiTDB0MV97s-tD4JhhZnfJ2bOMcZXu3NJOvyDu9SVaA@mail.gmail.com%3E

(您的操作必须扩展ActionSupport,以便在上面使用)

相关问题