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

标签 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"/>

最佳答案

Action 对象被公开,可以使用它的方法:

${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,才能在上面使用)

关于jsp - Struts 2 使用 JSTL 而不是 s 访问消息 :text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23846919/

相关文章:

java - spring-mvc Web 应用程序中的导航错误/无效

mysql - 一个sql查询的结果集可以在另一个sql查询中使用吗?

在 TokenHelper 中 setSessionToken 时抛出 java.lang.IllegalStateException

java - 在 Struts2 中访问多键映射

java - 如何使用c :if after using c:remove on session variable?

java - JSTL:从自定义标签获取变量

java - Java 服务器端(Jsp、velocity 等)日历组件?

jquery - 颜色框未正确显示内容

java - Token/TokenSession 拦截器导致 HttpSession 上出现空指针

java - 使用 JSTL 如何确定 forEach 之前哪个变量为 null