JSF - Parameter pass

Asked

Viewed 524 times

0

I have a form in JSF and I need to enter his data in the database I’ve searched several ways, but it’s not working

Could you give me a hand with what I’m doing wrong? JSF

Form

I am not able to pass the parameter of what is being typed for insertion in the database:

@ManagedBean(name="UsuarioJDCBDAO")

public void inserir(Usuario usuario) {

try {

Connection conexao = getConexao();

String value = null; Statement stmt = null; conexao.createStatement().execute("SET IDENTITY_INSERT "+"tbplan"+" ON"); stmt = conexao.createStatement(); ResultSet rs = stmt.executeQuery("select MAX(id) + 1 from tbplan");

while (rs.next()) value = rs.getString(1).toString();

System.out.println(value);

PreparedStatement pstm = conexao .prepareStatement("Insert into tbplan (data, nome, frase, id) values (?,?,?,?)");

//String name = new Usuario().getNome(); //pstm.setDate(1, new java.sql.Date(usuario.getDataCadastro().getTime())); pstm.setString(1, "26 Jun 2017 14:28:00:00");

pstm.setString(2, usuario.getNome());

// pstm.setString(2, n1.getNome()); pstm.setString(3, usuario.getFrase()); pstm.setString(4, value);

pstm.execute(); conexao.createStatement().execute("SET IDENTITY_INSERT "+"tbplan"+" OFF"); pstm.close(); conexao.close(); } catch (Exception e) { e.printStackTrace(); }

}

User-class

import java.io.Serializable; import java.util.Date;

public class Usuario implements Serializable {

private static final long serialVersionUID = -309513637403441998L;

private Long id;

private Date dataCadastro;

private String nome;

private String frase;

public Long getId() { return id; }

public void setId(Long id) { this.id = id; }

public Date getDataCadastro() { return dataCadastro; }

public void setDataCadastro(Date dataCadastro) { this.dataCadastro = dataCadastro; }

public String getNome() { return nome; }

public void setNome(String nome) { this.nome = nome; }

public String getFrase() { return frase; }

public void setFrase(String frase) { this.frase = frase; }

@Override public String toString() { return "Usuario [nome=" + nome + ", frase=" + frase + ", dataCadastro=" + dataCadastro + " id=" + id + "]"; }
}
  • You can receive this parameter in your Managedbean?

  • I put a println and could not list it

  • apparently the problem lies precisely in this passage from jsf to bean

  • You can edit your question by clicking the edit button just below the tags, then you add the code of the page where Voce is trying to pass the values

  • That one answer will help you. If you do not understand, getting home put an answer here

1 answer

-1

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    template="/paginas/default/main.xhtml">

    <ui:define name="content">
        <h1 class="page-header ">
            <i class="fa fa-comments"></i> Formulário
        </h1>
                <div class="form-group">
        <div class="col-xs-2">

        <h:form>
            <b><h:outputText value="Nome: " /></b>
            <h:inputText class="form-control" id="nome" value="" />
            <br style="clear: left;" />
        </h:form>
        <h:form>
            <b><h:outputText value="Data: " /></b>
            <h:inputText class="form-control" value="#{now}">  
                <f:convertDateTime pattern="dd/MM/yyyy HH:mm" timeZone="America/Sao_Paulo"/>  
            </h:inputText>
            <br style="clear: left;" />

        </h:form>
        </div>
        <div class="col-md-8">
        <h:form>
            <td valign="top">
            <b><h:outputText title="Ajuda" value="Frase: " /></b>
            </td>
            <h:inputTextarea class="form-control"  cols="80" rows="8" id="frase" value="" />
        </h:form>
            </div>
            </div>
        <br style="clear: left;" />
        <div class="container">
</div>

<div class="col-xs-2">

<h:form>
                        <h:commandButton id="submitBtn"  var="item" value="Incluir" class="btn btn-success" action="#{UsuarioJDCBDAO.inserir(Usuario)}">
                            <f:param name="Nome" value="#{item.nome}" />
                            <f:param name="Data" value="#{item.dataCadastro}" />
                            <f:param name="Frase" value="#{item.frase}" />
                        </h:commandButton>
                    </h:form>
</div>
    </ui:define>
</ui:composition>

Form

    <h:form>
        <b><h:outputText value="Nome: " /></b>
        <h:inputText class="form-control" id="nome" value="" />
        <br style="clear: left;" />
    </h:form>
    <h:form>
        <b><h:outputText value="Data: " /></b>
        <h:inputText class="form-control" value="#{now}">  
            <f:convertDateTime pattern="dd/MM/yyyy HH:mm" timeZone="America/Sao_Paulo"/>  
        </h:inputText>
        <br style="clear: left;" />

    </h:form>
    </div>
    <div class="col-md-8">
    <h:form>
        <td valign="top">
        <b><h:outputText title="Ajuda" value="Frase: " /></b>
        </td>
        <h:inputTextarea class="form-control"  cols="80" rows="8" id="frase" value="" />
    </h:form>
        </div>
        </div>
    <br style="clear: left;" />
    <div class="container">

</ui:define>
  • <h:form> <h:commandButton id="submitBtn" var="item" value="Include" class="btn btn-Success" action="#{Usuariojdcbdao.insert(Usuario)}"> <f:param name="Name" value="#{item.name}" /> <f:param name="Data" value="#{item.dataCadastro}" /> <f:param name="Phrase" value="#{item.phrase}" /> </h:commandButton> </h:form> </div> </ui:define> </ui:Composition>

  • You can [Edit] your question to put additional information

  • Edit the question instead of answering

Browser other questions tagged

You are not signed in. Login or sign up in order to post.