[英]How to iterate over a backing bean list of strings in JavaScript
I need to pass an array of strings from the backing bean to the JSP. 我需要将字符串数组从后备bean传递到JSP。 I can use ah:dataTable and display each string as ah:column entry with no problem.
我可以使用ah:dataTable并将每个字符串显示为ah:column条目而没有问题。 But I need to display this list of strings in an alert box instead of embedding it into the JSP itself.
但是我需要在警报框中显示此字符串列表,而不是将其嵌入到JSP本身中。 How would I pass the list to a JavaScript function and iterate over it?
如何将列表传递给JavaScript函数并对其进行迭代? I then want to append each string to a variable and show it.
然后,我想将每个字符串附加到变量中并显示它。
Here is the original data table: 这是原始数据表:
<h:dataTable id="scriptCommandTable" value="#{viewScriptRules.scriptCommands}" var="scriptCommand" columnClasses="scriptCommandSequence,scriptCommandRule" rendered="#{viewScriptRules.scriptCommandsSize > 0}">
<h:column>
<h:outputText value="#{scriptCommand.sequence}."/>
</h:column>
<h:column>
<h:outputText value="#{scriptCommand.rule}" escape="false"/>
</h:column>
</h:dataTable>
Just print it as an array of JS objects. 只需将其打印为JS对象数组即可。
<script>
var commands = [
<c:forEach items="#{viewScriptRules.scriptCommands}" var="command" varStatus="loop">
{
'sequence': '<h:outputText value="#{command.sequence}"/>',
'rule': '<h:outputText value="#{command.rule}" escape="false" />'
}
<h:outputText value="#{!loop.last ? ',' : ''}" />
</c:forEach>
];
for (var i = 0; i < commands.length; i++) {
var command = commands[i];
alert(command.sequence + "," + command.rule);
}
</script>
Much better, however, is to provide it as a webservice which returns a JSON string. 但是,更好的方法是将其作为可返回JSON字符串的Web服务提供。 In a JavaScript library like jQuery you can seamlessly access them.
在像jQuery这样的JavaScript库中,您可以无缝访问它们。
<script>
$.getJSON('someurl', function(data) {
$.each(data, function(index, command) {
alert(command.sequence + "," + command.rule);
});
});
</script>
You can use a plain vanilla Servlet for this ( example here ) or a JAX-RS. 您可以为此使用普通的Servlet( 此处为示例 )或JAX-RS。
Include another property in your backing bean that is a string composed of the concatated items from the list. 在您的后备bean中包括另一个属性,该属性是一个由列表中合并的项目组成的字符串。 Property can be set when the list is populated.
填充列表时可以设置属性。 I know that is not java script.
我知道那不是Java脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.