Working with Dates in JavaWeb

2

Hello, I'm doing a project that manages all the projects in my company, I'm using Hibernate, JavaWeb, primefaces, TDD and MVC standard .

My entity is shaped like this for date variables:

 @Temporal(value = TemporalType.TIMESTAMP)
    @Column(name = "dataInicio_projeto", nullable = false)
    private Date dataInicio;

I used this in TDD to test it:

projeto1.setDataInicio(new Date());

I need to know how I do in the visual part (JSF) of the project to send the date of the user's choice to the bank, since the bank is modeled according to the entity (reverse engineering).

A friend told me to use 'Calendar' variable because it is an improvement on 'Date', I do not know what to do. I need help ...

    
asked by anonymous 22.09.2016 / 03:25

1 answer

1

JSF is a framework component base, that is, it works with components in the view. Usually a library of components is used that extends the possibility of components and facilitates the development. Home A well-known one is PrimeFaces
To do a view with a small form that among other things would receive a date you need a controller (java class that will control your view) and the view itself (where jsf is used .xhtml pages).

Here is an example of a view:

<h:form id="form">
 <p:outputLabel for="datetime" value="Datetime:" />
        <p:calendar id="datetime" value="#{testeController.data}" pattern="MM/dd/yyyy HH:mm:ss" />
 <p:commandButton value="Salvar" action="#{testController.salvar}" />
</h:form>

And the controller to manage it:

@ManagedBean
@ViewScope
public class TesteController{

private Date data;

 public Date getData() {
        return data;
    }

    public void setData(Data date) {
        this.data = date;
    }

 public void salvar(){
    System.out.println("Data inserida: " + this.data);
}


}

From this point on the save method you have to do the logic to save in the database. I will not go into detail here on it because it is a very lengthy subject, you will find more information at this link: Introduction to JPA

    
22.09.2016 / 15:19