我有一个由Integer键入的Map.使用EL,我如何通过其键访问值?
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
Run Code Online (Sandbox Code Playgroud)
我认为这会有效,但它没有(地图已经在请求的属性中):
<c:out value="${map[1]}"/>
Run Code Online (Sandbox Code Playgroud)
跟进:我追查了问题.显然${name[1]},使用数字进行地图查找Long.我想通了这一点,当我换HashMap到TreeMap和收到的错误:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
Run Code Online (Sandbox Code Playgroud)
如果我将地图更改为:
Map<Long, String> map = new HashMap<Long, String>();
map.put(1L, "One");
Run Code Online (Sandbox Code Playgroud)
然后${name[1]}返回"一".那是什么?为什么<c:out>将数字视为一个长数.对我来说似乎违反直觉(因为int比长期更常用).
所以我的新问题是,是否有EL符号通过Integer值访问地图?
是否可以从javascript更新PrimeFaces组件,以便强制刷新?
我正在使用对话框中的此按钮进行ajax保存调用.我已经在oncomplete事件上附加了我的自定义javascript.
<p:growl life="1500" id="showmessage"/>
<p:dialog id="addMemberDialog" widgetVar="addMemberDlg">
<!-- More Code -->
<p:commandButton value="Save"
actionListener="#{memberManagedBean.save}"
oncomplete="handleSaveNewMember(xhr, status, args)"
update=":memberListForm:membersTable createupdateform "
process="@form" />
</p:dialog>
Run Code Online (Sandbox Code Playgroud)
..在保存按钮,我在这里添加一条消息,使用growl组件将其显示给客户端.
public void save(ActionEvent event) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
"Successfuly Add user", "Successfuly Add user");
FacesContext.getCurrentInstance().addMessage(null, message);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何对UI进行排序,我应该在growl组件显示meesage之前先隐藏对话框?
function handleSaveNewMember(xhr, status, args) {
addMemberDlg.hide();
//update the growl after the dialog was hidden?
}
Run Code Online (Sandbox Code Playgroud)
发生的事情是,growl组件同时显示在对话框旁边.
谢谢.
我如何在JSF 2.0中使用EL中的参数/变量/参数调用直接方法或方法?
例如,在EL中获取列表大小:
<h:outputText value="#{bean.list.size()}" />
Run Code Online (Sandbox Code Playgroud)
或者使用参数调用action方法:
<h:commandButton value="edit" action="#{bean.edit(item)}" />
Run Code Online (Sandbox Code Playgroud)
这在我的环境中似乎不起作用.它似乎不喜欢括号.
javax.el.ELException:错误解析:#{bean.list.size()}
com.sun.el.parser.ParseException:遇到"("
我有一个用例,我必须使用资源包在UI上显示各种文本.其中一些资源包条目采用参数(例如{0}),因为我使用h:outputFormat,但有时这还不够.
例如
someMessage=Display this message with param {0}
Run Code Online (Sandbox Code Playgroud)
在资源包中.
要在xhtml上显示我通常会这样做:
<h:outputFormat value="#{msg['someMessage']}"><f:param value="#{someBean.value}"/></h:outputFormat>
Run Code Online (Sandbox Code Playgroud)
当这是一个简单的案例时,这很有效,但对于更复杂的用例,这还不够.例如,如果我希望commandLink的'title'属性使用上面的资源包条目:
<h:commandLink action="logout" title="#{msg['someMessage']}">
<f:param value="#{someBean.value}" />
<h:graphicImage library="images" name="image.png" />
</h:commandLink>
Run Code Online (Sandbox Code Playgroud)
这不起作用.我也尝试过:
<h:commandLink action="logout">
<f:attribute name="title">
<h:outputFormat value="#{msg['someMessage']}"><f:param value="#{someBean.value}"/></h:outputFormat>
</f:attribute>
<h:graphicImage library="images" name="image.png" />
</h:commandLink>
Run Code Online (Sandbox Code Playgroud)
这也不起作用,因为f:attibute不允许孩子.
即使有一个黑客绕过这个(例如使用来自primefaces的悬停组件),还有其他字段可能需要参数化消息.
有没有人知道使用MessageFormat的方法,该方法在JSF组件的非值字段中获取参数?
我有bean"MyBean",它有属性HashMap - "map",其值类型是MyClass.我想使用ui:repeat在jsf中显示map的一些属性.但是这些代码:
<ui:repeat var="var" value="#{mybean.map}" >
<tr>
<td> <h:outputText value="#{var.value.property1}"></h:outputText> </td>
<td><h:outputText value="#{var.value.property2}"></h:outputText></td>
</tr>
</ui:repeat>
Run Code Online (Sandbox Code Playgroud)
但是这段代码没有显示任何内容.虽然当我尝试以这种方式在jsp中显示hashmap值时,它是成功的.哪里错了?怎么解决这个问题?
我正在使用动态创建的链接:
<h:link outcome="/page" value="#{name}">
<f:param name="name" value="#{name}"/>
</h:link>
Run Code Online (Sandbox Code Playgroud)
我想为f:param添加自定义转换器以从#{name}等中删除空格.但是f:param中没有转换器属性.
我需要在我的JSF应用程序error.xhtml页面中显示异常堆栈跟踪.我知道用JSP页面做这件事有多简单.但是使用JSF 2.0我有一个问题.
在我的web.xml我已经定义了一个JSF 2.0 Facelets页面作为错误页面:
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/faces/views/error.xhtml</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
当错误发生时,我被重定向到此页面.我需要的是在此Facelets页面中显示异常的堆栈跟踪.
我试过用:
<pre>
<h:outputText value="${exception}"/>
</pre>
Run Code Online (Sandbox Code Playgroud)
但我没有得到任何输出.我一直在网上搜索,但我找不到解决方案.如何在Facelets页面中显示异常堆栈跟踪?
编辑:
我刚尝试过:
<c:forEach var="exeption" items="${exception.stackTrace}">
<div>${exeption}</div>
</c:forEach>
<h:dataTable value="#{exception.stackTrace}"
var="exception">
<h:column>
<h:outputText value="#{exception}"/>
</h:column>
</h:dataTable>
Run Code Online (Sandbox Code Playgroud)
JSTL无法正常工作并通过数据表进行交互也无法正常工作.我确定发生异常,我在日志文件中看到它.
有没有办法用JSF EL表达式中的默认值替换null,有点像Oracle NVL函数?
我知道我可以做点什么
#{obj == null ? 'None' : obj.property}
Run Code Online (Sandbox Code Playgroud)
但希望有一种自动方式可以做到这一点,所以我不会复制/粘贴三元Elvis运算符两侧的相同表达式.
我正在寻找类似的东西
#{default(obj.property, 'None')}
Run Code Online (Sandbox Code Playgroud) 您认为将所有广泛使用的实用程序方法放在应用程序范围的bean中是一个好主意吗?
在当前执行的应用程序我的工作,所有的实用方法(用绳子,饼干操纵,检查URL,检查当前的页面,用户等)都放在一个大的请求范围豆,他们是从引用每个xhtml页面.
如果将实用程序方法放在应用程序作用域中的方法是一个好的或坏的选择,我找不到有关stackoverflow的任何信息.
为什么我遇到这个想法是需要在范围更广的bean中重用那些方法,然后再使用请求范围的bean(如视图或会话范围的bean).如果我错了,请纠正我但是你应该总是注入相同或更宽的范围bean,即你不应该在视图范围内注入请求范围的bean.
我想使用的实用方法,从应用范围的bean应该是有益的(不会有任何新对象的创建,一个对象将被创建并在所有应用重复使用),但我仍然想确认或如果有人告诉我这是一种错误的方法,为什么是错的.
我的一个支持bean中有一个静态的选择项列表:
private static List<SelectItem> countries = new ArrayList<SelectItem>();
Run Code Online (Sandbox Code Playgroud)
使用以下getter和setter:
public static List<SelectItem> getCountries() {
return countries;
}
public static void setCountries(List<SelectItem> countries) {
LoadSelectItemsBean.countries = countries;
}
Run Code Online (Sandbox Code Playgroud)
我无法通过我的XHTML页面访问静态列表.我试过的代码如下:
<ace:simpleSelectOneMenu id="countryField"
value="#{generalCarrierDataViewBean.carrierBean.countryId}">
<f:selectItems value="#{loadSelectItemsBean.countries}" />
<ace:ajax />
</ace:simpleSelectOneMenu>
Run Code Online (Sandbox Code Playgroud)
问题在于:
<f:selectItems value="#{loadSelectItemsBean.countries}" />
Run Code Online (Sandbox Code Playgroud)
结果的例外是:
javax.el.PropertyNotFoundException: /pages/GeneralCarrierData.xhtml @394,64 value="#{loadSelectItemsBean.states}": Property 'states' not found on type com.oag.reference.util.LoadSelectItemsBean
Run Code Online (Sandbox Code Playgroud)
有人可以建议如何从支持bean正确引用静态属性吗?
谢谢