struts2elognlstruts-tags

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?


Solution

  • 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.