java 如何操作 JSF 标记中的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7901875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:54:33  来源:igfitidea点击:

How can I manipulate a String in a JSF tag?

javajspjsf

提问by MemoryLeak

Given this code,

鉴于此代码,

<rich:dataTable id="list" value="#{testBeen.dataModel}" var="test" rows="#{testBeen.dataModel.pageSize}">
    ...
    <h:outputText value="#{test.WEEK}" />  

I need to manipulate the #{test.WEEK}and replace character )with ], how can I do this?

我需要操作#{test.WEEK}和替换字符)],我该怎么办呢?

I tried the following, but it does not work:

我尝试了以下方法,但不起作用:

<%String a = test.WEEK; a.replace("a", "b"); %>
<%=a %>

How can I get the string from JSF and pass it back to JSF?

如何从 JSF 获取字符串并将其传递回 JSF?

回答by BalusC

For this particular simple purpose, I'd just use the JSTLfunctionstaglib. There's a fn:replace()function.

对于这个特别简单的目的,我只使用JSTL函数taglib。有一个fn:replace()功能。

E.g.

例如

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<h:outputText value="#{fn:replace(test.WEEK, ')', ']')}" />  

You should for sure neveruse scriptlets<% %>in JSF pages.

您绝对应该在 JSF 页面中使用scriptlet<% %>

回答by stacker

You could write a custom Converter and parameterize it by search and replace string. See this introduction JSF for nonbelievers: JSF conversion and validation

您可以编写自定义转换器并通过搜索和替换字符串对其进行参数化。请参阅非信徒的 JSF简介:JSF 转换和验证

回答by kgautron

Good practice would be to do this in the bean rather than the facelet.

好的做法是在 bean 中而不是在 facelet 中执行此操作。