2012-09-27 29 views
2

我已經實現一個簡單的CRUD應用程序的問題,我看了幾乎所有的谷歌,計算器和roseindia發現的項目,但我的問題仍然存在。我做了這個簡單的facelet:JSF 2.0命令無能爲力

<ui:composition template="./../../templates/adminTemplate.xhtml"> 

     <ui:define name="tope"> 
      <h1>Ingreso de Noticias</h1> 
     </ui:define> 

     <ui:define name="content"> 
       <h:form id="newsForm"> 
        <h:panelGrid columns="2"> 
         <h:outputLabel value="Lugar: "/> 
         <h:inputText value="#{noticiasBean.entity.lugar}"/> 
         <h:outputLabel value="Fecha: "/> 
         <h:inputText value="#{noticiasBean.entity.fecha}"> 
          <f:convertDateTime pattern="dd/MM/yyyy HH:mm"/> 
         </h:inputText> 
         <h:outputLabel value="Autor: "/> 
         <h:inputText value="#{noticiasBean.entity.autor}"/> 
         <h:outputLabel value="PreTítulo: "/> 
         <h:inputText value="#{noticiasBean.entity.pretitulo}"/> 
         <h:outputLabel value="Título: "/> 
         <h:inputText value="#{noticiasBean.entity.titulo}"/> 
         <h:outputLabel value="Comentario: "/> 
         <h:inputText value="#{noticiasBean.entity.comentario}"/> 
         <h:outputLabel value="Cuerpo: "/> 
         <h:inputTextarea value="#{noticiasBean.entity.cuerpo}"/> 
        </h:panelGrid> 
        <h:commandButton value="Guardar" action="#{noticiasBean.create}"/> 
       </h:form> 
      <h:messages style="color: red;"/> 
     </ui:define> 

    </ui:composition> 

這裏是adminTemplate:

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:p="http://primefaces.org/ui"> 

<h:head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <link href="./../resources/css/default.css" rel="stylesheet" type="text/css" /> 
    <link href="./../resources/css/cssLayout.css" rel="stylesheet" type="text/css" /> 
    <h:outputStylesheet name="primeStyles.css" library="css"/> 
    <title>Facelets Template</title> 
</h:head> 

<h:body> 

    <p:layout fullPage="true"> 
     <p:layoutUnit position="north" size="70" resizable="true" closable="true" collapsible="true"> 
      <ui:insert name="tope"> Sección de Administración</ui:insert> 
     </p:layoutUnit> 
     <p:layoutUnit position="south" size="70"> 
      <ui:insert> Pie de página </ui:insert> 
     </p:layoutUnit> 
     <p:layoutUnit position="west" size="160" header="Menu" resizable="true" collapsible="true"> 
      <h:form id="formMenu"> 
       <p:panelMenu style="width: 158px;"> 
        <p:submenu label="Home"> 
         <p:menuitem value="Admin" action="/admin/admin"/> 
         <p:menuitem value="Salir" action="#{loginController.logout}"/> 
        </p:submenu> 
        <p:submenu label="Contenido"> 
         <p:menuitem value="Cargar Noticia" action="/admin/noticias/Create"/> 
        </p:submenu> 
       </p:panelMenu> 
      </h:form> 
     </p:layoutUnit> 
     <p:layoutUnit position="center"> 
      <ui:insert name="content"> 
       Aqui va el contenido 
      </ui:insert> 
     </p:layoutUnit> 
    </p:layout> 

</h:body> 

這裏是managedBean:

@ManagedBean(name="noticiasBean") 
@RequestScoped 
public class NoticiasBean { 

private NoticiaJpaController jpaController = null; 
private DataModel items = null; 
private Noticia entity; 
/** 
* Creates a new instance of NoticiasBean 
*/ 
public NoticiasBean() { 
    System.out.println("Instanciado el bean"); 
} 

private NoticiaJpaController getJpaController() { 
    if(jpaController == null){ 
     jpaController = new NoticiaJpaController(Utils.getEntityManagerFactory()); 
    } 
    return jpaController; 
} 

public Noticia getEntity() { 
    if(entity == null){ 
     entity = new Noticia(); 
    } 
    return entity; 
} 

public String prepareCreate() { 
    entity = new Noticia(); 
    return "Create"; 
} 

public String create() { 
    System.out.println("Llegó al método create"); 
    try { 
     getJpaController().create(entity); 
     String mensaje = "Noticia creada exitósamente"; 
     FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, mensaje, mensaje); 
     FacesContext.getCurrentInstance().addMessage(null, facesMsg); 
     return prepareCreate(); 
    } catch (Exception e) { 
     String mensaje = "Error de Persistencia"; 
     FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, mensaje, mensaje); 
     FacesContext.getCurrentInstance().addMessage(null, facesMsg); 
     return null; 
    } 
} 

public String prepareList() { 
    recreateModel(); 
    return "List"; 
} 

public DataModel getItems() { 
    if (items == null) { 
     items = new ListDataModel(getJpaController().findNoticiaEntities()); 
    } 
    return items; 
} 

private void recreateModel() { 
    items = null; 
} 

}

這裏是web.xml f ILE:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
<context-param> 
    <param-name>javax.faces.PROJECT_STAGE</param-name> 
    <param-value>Development</param-value> 
</context-param> 
<context-param> 
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name> 
    <param-value>true</param-value> 
</context-param> 
<context-param> 
    <description>Usado para evitar que ingresen sin estar autenticado</description> 
    <param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESION</param-name> 
    <param-value>false</param-value> 
</context-param> 

<error-page> 
    <exception-type>javax.faces.application.ViewExpiredException</exception-type> 
    <location>/faces/index.xhtml</location> 
</error-page> 

<servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>/faces/*</url-pattern> 
</servlet-mapping> 
<session-config> 
    <session-timeout> 
     30 
    </session-timeout> 
</session-config> 
<welcome-file-list> 
    <welcome-file>faces/index.xhtml</welcome-file> 
</welcome-file-list> 

正如你所看到的,沒有其它儀器特別,但我每次按H:的commandButton,保存數據,總是返回到同一頁面Create.xhtml沒有任何錯誤消息,也不會將信息保存在數據庫中,更糟糕的是,會丟失對樣式文件syle.css的引用。

+0

檢查您的NoticiaJpaController代碼,我想創建方法是在try/catch塊拋出一個異常,你是返回一個空,這將重新顯示在同一頁面。 – Ravi

+0

Ravi,謝謝你的快速回答。我把「e.printStackTrace()」中的搭上線,結果是一樣的:沒有消息,沒有錯誤,也沒有記錄。 I'll繼續進行測試,如果我找到了一個解決方案,我會通知這裏 –

+0

Roseindia.net是世界上最糟糕的網站,當涉及到代碼段中所示的「最佳實踐」。當您從那裏複製解決方案/示例時,請特別小心。 – BalusC

回答

2

幾個讀數和調查後,我解決了問題閱讀本information @BalusC的。我從來沒有在我的facelet中使用兩個h:表單元素,所以我不知道爲什麼現在正在工作。只是我改變了H:的commandButton爲Primefaces' P:命令按鈕和‘瞧’,everyyhing現在工作得很好。

+0

你確實有兩種形式:'newsForm'和'formMenu'。 – BalusC

+0

沒錯,我是在自己的回答後意識到的。謝謝 –