Creating a blog entry
On this page a form to create new blog entries is defined. This page is build with XHTML and JSF tags explained as follows.
<h:form>
<!-- Display all JSF messages -->
<h:messages/> <!-- (4) -->
<h:outputLink value="index.xhtml">Back to blog</h:outputLink>
<!-- The fx:content tag references our type "Blog Entry" -->
<fx:content typeName="blogEntry" var="entry"> <!-- (1) -->
<!-- This renders an html input field for the type's property "Entry Title" -->
<fx:value property="entryTitle"/>
<!-- This renders an html input field for the type's property "Entry Text" -->
<fx:value property="entryText"/>
<!-- Save content using the FxContentViewBean, pass the content instance
stored in component_content via f:setPropertyActionListener -->
<h:commandLink action="#{fxContentViewBean.save}"> <!-- (2) -->
<f:setPropertyActionListener target="#{fxContentViewBean.content}" value="#{entry_content}"/> <!-- (3) -->
Publish
</h:commandLink>
</fx:content>
</h:form>
(1) <fx:content/> from the [fleXive] component library is used to create a new announcement content instance. If a type name or type id is specified, a new content instance of this type is created.
(2) The Save command link's action is bound to the save() method of the JSF managed [fleXive] system bean FxContentViewBean. If an error occurs, an error message is added.
(3) Blog entry instances created by <fx:content/> are mapped to the PropertyActionListener's value attribute. They are stored to the JSF managed FxContentViewBean. The command link's action triggers the save() method which finally persists the announcement instance.
(4) Error and info messages are rendered by <h:messages/>
JSF managed FxContentViewBean
public String save() {
try {
final FxPK pk = EJBLookup.getContentEngine().save(content.copy());
final Map<String, Object> requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
requestMap.put(REQUEST_PK, pk);
requestMap.put(REQUEST_CONTENT, content);
requestMap.put(REQUEST_ISNEW, content.getPk().getId() == -1);
if (StringUtils.isNotBlank(successMessage)) {
FxJsfUtils.addMessage(new FacesMessage(successMessage));
}
return "success";
} catch (Exception e) {
new FxFacesMsgErr(e).addToContext();
return "failure";
}
}