我正在寻找一种方法来在JSP页面输出时将字符串计算为el.(我想当它的toString()方法在引擎盖下调用???)
例如,在我的页面中,如果我这样做:
<title>${bean.title}</title>
Run Code Online (Sandbox Code Playgroud)
title将是bean的属性,它将返回一个字符串.我想在bean.title中存储其他EL表达式,以便对它们进行评估.
所以,如果bean.title =" This is the ${param.pageType} page
",显然会在写入页面之前进行评估.
有没有办法做到这一点?
您实际上是在错误的方向上寻找解决方案。为此,您应该使用JSTL fmt
标记库。它也支持参数化消息。您应该只更改代码以将消息存储在(国际化的).properties
文件中而不是 bean 中。
假设您在包text.properties
中的文件中有以下键值条目com.example.i18n
title = 这是第 {0} 页
然后你可以按如下方式使用它
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:setBundle basename="com.example.i18n.text" />
...
<title>
<fmt:message key="title">
<fmt:param value="${param.pageType}" />
</fmt:message>
</title>
Run Code Online (Sandbox Code Playgroud)