Working with Javaweb Dates

Asked

Viewed 389 times

2

Hello, I am doing a project that manages all projects of my company, I am 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;

and I used it on the TDD to test her:

projeto1.setDataInicio(new Date());

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

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

  • And how’s your JSF? That one post maybe I can help you.

1 answer

1


JSF is a base Component framework, meaning it works with components in the view. Usually some component library is used that expands the possibility of components and facilitates development.
A very well known is the Primefaces
To make 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 in jsf you use pages. xhtml).

Follow an example of 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 in the save method you have to do the logic to save in the database. I will not go into detail here about it because it is a very extensive subject, you will find more information on this link: Introduction to JPA

  • Okay, I get it, it’s working, and how do I format the date when viewing ? For example, I don’t want to use minutes and hours, I just want the date. }"/>

  • You can use a convert for this, for dates jsf already has a default convert, do it like this: <h:outputText value="#{projeto.dataInicio}">&#xA; <f:convertDateTime pattern="dd-mm-yyyy" />&#xA;</h:outputText>

Browser other questions tagged

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