Emaborsa
Emaborsa

Reputation: 2860

Why <s:text> tag name does not support runtime expressions?

I need to print out a text within the current year.

My code:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<jsp:useBean id="now"
  class="java.util.Date"
/>
<fmt:formatDate 
  var="year"
  value="${now}"
  pattern="y"
/>
<p>
  <s:text 
    name="%{getText('app.footer', {${year}})}"
  />
</p>

The error is:

"name" does not support runtime expressions.

How could I arrange it without creating additional classes or variables in actions?

Upvotes: 2

Views: 1776

Answers (2)

Roman C
Roman C

Reputation: 1

By default <fmt:formatDate> tag creates a variable in the page scope. To access this variable in OGNL you should use #attr prefix.

If you are using getText() to get the message, you don't need a <s:text> tag.

<s:property value="%{getText('app.footer', {#attr.year})}" />

Also you should know that JSP EL expressions evaluation is disable in Struts tag attributes to prevent double evaluation of expressions by different expression engines. If Struts find such expressions in attributes, it will report an error.

Upvotes: 0

Aleksandr M
Aleksandr M

Reputation: 24396

You can do it using Struts2 tags only, there is no need to use fmt tags.

<s:bean var="date" name="java.util.Date" />
<s:date var="year" name="#date" format="y" />
<p>
    <s:text name="app.footer">
        <s:param value="#year" />
    </s:text>
</p>

You cannot use ${} inside S2 tags. The <s:text> tag will render a I18n text message, no need to use getText method inside it.

Upvotes: 3

Related Questions