Forum Stats

  • 3,768,743 Users
  • 2,252,843 Discussions
  • 7,874,705 Comments

Discussions

Date in jsp is not same as nls_date_format spfile value

Andy Frédéric
Andy Frédéric Member Posts: 50

The database is Oracle 11g2 Express.

I changed the nls_date_format to DD/MM/RRRR :

alter system set nls_date_format = "DD/MM/RRRR" scope = spfile;

  Then I restarted the database.

Now in my spring-mvc project I created a Bean :

@[email protected](name = "HR.EMPLOYEES")public class User {    @Id    @SequenceGenerator(name="EMPLOYEES_SEQ", sequenceName="EMPLOYEES_SEQ", allocationSize=1)    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="EMPLOYEES_SEQ")    @Column(name = "EMPLOYEE_ID")    private int id;        @Formula(value="FIRST_NAME || ' ' || LAST_NAME")    private String username;        @Column(name = "FIRST_NAME")    private String firstname;        @Column(name = "LAST_NAME")    private String lastname;        private String email;        @Column(name="HIRE_DATE")    @Temporal(TemporalType.DATE)    private Date hireDate; // java.util.Date        private String job_id;        @Column(name = "SALARY")    private double salary;        public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getFirstname() {        return firstname;    }    public void setFirstname(String firstname) {        this.firstname = firstname;    }    public String getLastname() {        return lastname;    }    public void setLastname(String lastname) {        this.lastname = lastname;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    public Date getHireDate() {        return hireDate;    }    public void setHireDate(Date hireDate) {        this.hireDate = hireDate;    }    public String getJob_id() {        return job_id;    }    public void setJob_id(String job_id) {        this.job_id = job_id;    }    public double getSalary() {        return salary;    }    public void setSalary(double salary) {        this.salary = salary;    }    }

Here is DAO Implementation :

public class UserDAOImpl implements UserDAO {        @Autowired    private SessionFactory sessionFactory;        public UserDAOImpl() {             }        public UserDAOImpl(SessionFactory sessionFactory) {        this.sessionFactory = sessionFactory;    }    @Override    @Transactional    public List<User> list() {                @SuppressWarnings("unchecked")        List<User> listUser = (List<User>) sessionFactory.getCurrentSession()        .createCriteria(User.class)        .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();        return listUser;    }    @Override    @Transactional    public User get(int id) {                String hql = "from User where id=" + id;        Query query = sessionFactory.getCurrentSession().createQuery(hql);                 @SuppressWarnings("unchecked")        List<User> listUser = (List<User>) query.list();                 if (listUser != null && !listUser.isEmpty()) {            return listUser.get(0);        }                 return null;            }    @Override    @Transactional    public void saveOrUpdate(User user) {        sessionFactory.getCurrentSession().saveOrUpdate(user);    }    @Override    @Transactional    public void delete(int id) {        User userToDelete = new User();        userToDelete.setId(id);        sessionFactory.getCurrentSession().delete(userToDelete);    }}

Here is the controller :

@Controllerpublic class HomeController {        @Autowired    private UserDAO userDao;    @RequestMapping("/")    public ModelAndView handleRequest() throws Exception { // to list employees        List<User> listUsers = userDao.list();        ModelAndView model = new ModelAndView("UserList");        model.addObject("userList", listUsers);        return model;    }         @RequestMapping(value = "/new", method = RequestMethod.GET)    public ModelAndView newUser() { // to add a new employee        ModelAndView model = new ModelAndView("UserForm");        model.addObject("user_details", new User());        model.addObject("titre", "Ajout");        return model;          }         @RequestMapping(value = "/edit", method = RequestMethod.GET)    public ModelAndView editUser(HttpServletRequest request) { // to edit an existing employee        int userId = Integer.parseInt(request.getParameter("id"));        User user = userDao.get(userId);        ModelAndView model = new ModelAndView("UserForm");        model.addObject("user_details", user);        model.addObject("titre", "Modif");        return model;          }         @RequestMapping(value = "/delete", method = RequestMethod.GET)    public ModelAndView deleteUser(HttpServletRequest request) { // to delete an existing employee        int userId = Integer.parseInt(request.getParameter("id"));        userDao.delete(userId);        return new ModelAndView("redirect:/");         }         @RequestMapping(value = "/save", method = RequestMethod.POST)    public ModelAndView saveUser(@ModelAttribute User user) { // to execute the dml of insert of update about an employee's data        userDao.saveOrUpdate(user);        return new ModelAndView("redirect:/");    }    }

Here is the JSP for editing an employee :

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>${titre}</title><style type="text/css">    a {        text-decoration: none;    }</style></head><body>    <div align="center">        <h1>${titre}</h1>        <table>            <form:form action="save" method="post" modelAttribute="user_details">                                <form:hidden path="id"/>                                <tr>                    <td>Firstname:</td>                    <td><form:input path="firstname"/></td>                </tr>                <tr>                    <td>Lastname:</td>                    <td><form:input path="lastname"/></td>                </tr>                <tr>                    <td>Email:</td>                    <td><form:input path="email"/></td>                </tr>                <tr>                    <td>Hiredate:</td>                    <td><form:input path="hireDate"/></td> <%-- this is the date field --%>                </tr>                <tr>                    <td>Job_id:</td>                    <td><form:input path="job_id"/></td>                </tr>                <tr>                    <td>Salary:</td>                    <td><form:input path="salary"/></td>                </tr>                <tr>                    <td>                        <input type="submit" value="Save">                    </td>                    <td><a href="<c:url value='/' />"><input type="button" value="Annuler" /></a></td>                </tr>            </form:form>        </table>    </div>     </body></html>

At runtime the value displayed in the text field for hire_date has a format RRRR-MM-DD , and the only accepted format to be able to be inserted/updated into the database is MM/DD/RRRR ! Although spfile nls_date_format is already set to DD/MM/RRRR and database is already restarted ! So why is the spfile parameter not effect !

Best Answer

Answers

This discussion has been closed.