我的.xhtml文件是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<script name="jquery/jquery.js" library="primefaces">
</script>
</h:head>
<h:form>
<h:outputScript library="js" name="calendar.js" />
<h:outputStylesheet library="css" name="accordion.css" />
<h:panelGrid id="panel" columns="2" cellpadding="5" border="1"
cellspacing="1" bgcolor="#3bbab5">
<f:facet name="header">
<h:outputText value="REQUEST" />
</f:facet>
<h:outputLabel for="title" value="TITLE" style="font-weight:bold" />
<p:inputText id="title" required="true" style="width: 97%"
value="#{requestBean.tRequest.title}" />
<h:outputLabel for="description" value="DESC"
style="font-weight:bold" />
<p:inputTextarea id="description" rows="5" cols="30" maxlength="4000"
style="width: 97%" autoResize="false" value="#{requestBean.tRequest.description}"/>
<b>EVENT TYPE</b>
<h:selectOneMenu value="#{editEventTypeBean.selectedEventType}"
required="true" panelStyle="width:150px" effect="fade" var="t"
style="width:97%" filter="true" filterMatchMode="startsWith">
<f:selectItem itemLabel="SELECT TYPE" itemValue="" />
<f:selectItems value="#{editEventTypeBean.eventTypeModel}"
var="eventType" itemLabel="#{eventType.eventTypeName}"
itemValue="#{eventType}" />
</h:selectOneMenu>
<h:outputText style="vertical-align: top; font-weight:bold;"
value="Детайли" />
<p:accordionPanel style="border:1px solid; width:500px">
<p:tab title="ADD FILTERS">
<p:selectManyCheckbox id="chkbox1"
value="#{filterTypeBean.selectedFilterTypes}"
layout="pageDirection">
<f:selectItems var="checkbox"
value="#{filterTypeBean.listBooleanFilterTypes()}"
itemLabel="#{checkbox.filterTypeName}" itemValue="#{checkbox.id}"
required="true" requiredMessage="check at least one checkbox" />
</p:selectManyCheckbox>
<h:message for="chkbox1" style="color:red" />
<h:panelGrid id="panel2" columns="2" cellpadding="5">
<c:forEach items="${filterTypeBean.listTextFilterTypes()}"
var="inputBoxes">
<h:outputText value="${inputBoxes.filterTypeName}"
style="width: 100px; white-space: normal; border: 3px" />
<h:inputText />
</c:forEach>
<h:outputText
value="${filterTypeBean.listDateFilterType().getFilterTypeName()}"
style="width: 100px; white-space: normal; border: 3px" />
<p:calendar id="cal" value="#{calendarBean.date}" mode="popup"
showOn="button" locale="bg" navigator="true" pattern="dd.mm.yyyy"
style="width: 1px" />
</h:panelGrid>
</p:tab>
</p:accordionPanel>
<f:facet name="footer">
<h:panelGroup style="display:block; text-align:center">
<h:commandButton value="SAVE" id="saveBtn"
actionListener="#{requestBean.addRequest()}"/>
</h:panelGroup>
</f:facet>
</h:panelGrid>
</h:form>
</html>
我的bean中的方法addRequest()没有被调用。
这是bean的一部分:
@ManagedBean(name = "requestBean")
@SessionScoped
public class RequestBean implements Serializable {
...
public void addRequest() {
System.out.println("RequestBean " );
EntityManager em = HibernateUtil.getEntityManager();
em.getTransaction().begin();
System.out.println("request: " + tRequest.getTitle());
TRequest newRequest = new TRequest(tRequest);
System.out.println("newRequest: " + newRequest);
newRequest.setRequestDate(new Date());
Query q = em.createQuery("select u from TUser u");
List<TUser> usersList = q.getResultList();
for (TUser user : usersList) {
System.out.println("User: " + user);
}
TUser author1 = new TUser();
for (TUser tUser : usersList) {
if (tUser.getId() == (Long.valueOf(2)))
author1 = tUser;
}
System.out.println("Author: " + author1.getName());
newRequest.setAuthor(author1);
try {
em.persist(newRequest);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
em.getTransaction().commit();
}
我创建了一个新的.xhtml文件,仅供测试使用,但是我找不到第一个问题。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<script name="jquery/jquery.js" library="primefaces">
</script>
</h:head>
<h:form>
<h:outputLabel for="title" value="Заглавие " style="font-weight:bold" />
<p:inputText id="title" required="true" style="width: 97%"
value="#{requestBean.tRequest.title}" />
<h:outputLabel for="description" value="Описание"
style="font-weight:bold" />
<p:inputTextarea id="description" rows="5" cols="30" maxlength="4000"
style="width: 97%" autoResize="false"
value="#{requestBean.tRequest.description}" />
<h:commandButton value="Запази" id="saveBtn"
actionListener="#{requestBean.addRequest()}" />
</h:form>
</html>
感谢您的帮助!
答案 0 :(得分:0)
这篇文章 http://balusc.blogspot.ch/2007/09/objects-in-hselectonemenu.html对我来说非常有用,我设法解决了我的问题。感谢作者!
答案 1 :(得分:0)
我添加了一个转换器:
<h:selectOneMenu value="#{requestBean.selectedEventType}" required="true"
panelStyle="width:150px" effect="fade" var="t" style="width:97%"
filter="true" filterMatchMode="startsWith">
<f:selectItem itemLabel="Избери тип" itemValue="#{null}" />
<f:selectItems value="#{eventTypeBean.selectItems}" var="eventType"
itemLabel="#{eventType.eventTypeName}" itemValue="#{eventType}" />
<f:converter converterId="tEventTypeConverter" />
</h:selectOneMenu>
我将这些行添加到我的bean类中:
@PostConstruct
public void init() {
System.out.println("PostConstructor!!!");
EntityManager em = HibernateUtil.getEntityManager();
Query q = em.createQuery("select u from TEventType u");
listTEventTypes = q.getResultList();
tEventTypeMap = new LinkedHashMap<Long, TEventType>();
for (TEventType eventType : listTEventTypes) {
tEventTypeMap.put(eventType.getId(), eventType);
}
selectItems = new ArrayList<SelectItem>();
for (TEventType tEventType : list()) {
selectItems.add(new SelectItem(tEventType, tEventType
.getEventTypeName()));
}
}
private static Map<Long, TEventType> tEventTypeMap;
public TEventType find(String key) {
Long keyToLong = Long.valueOf(key);
return tEventTypeMap.get(keyToLong);
}
public List<TEventType> list() {
return new ArrayList<TEventType>(tEventTypeMap.values());
}
public Map<Long, TEventType> map() {
return tEventTypeMap;
}
public TEventType getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(TEventType selectedItem) {
this.selectedItem = selectedItem;
}
public List<SelectItem> getSelectItems() {
return selectItems;
}
我添加了这个Converter类:
public class TEventTypeConverter implements Converter {
private static EventTypeBean eventType = new EventTypeBean();
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
if (value == null || value.isEmpty()) {
return null;
}
if (!value.matches("\\d+")) {
throw new ConverterException("The value is not a valid ID number: "
+ value);
}
System.out.println ("Value id: " + value);
return eventType.find(value);
}
public String getAsString(FacesContext context, UIComponent component,
Object value) {
if (value == null) {
return null; // Or an empty string, can also.
}
if (!(value instanceof TEventType)) {
throw new ConverterException("The value is not a valid TEventType: "
+ value);
}
Long id = ((TEventType) value).getId();
return (id != null) ? String.valueOf(id) : null;
}
}