I have created an application in Spring MVC for testing the rollback and commit functionality. I am using Transaction using. Here in the service i have created another contact objects without specifying employee id which is a required field. The application works fine when exception is comming while saving and rollback is working.
But the problem is the Exception is printing in my jsp page.
Can anyone please tell me some solution for preventing displaying he exception in te view
Controller
@RequestMapping(value="/saveContact", method=RequestMethod.POST)
public String create(@ModelAttribute("newContact")Contacts contact, BindingResult result, SessionStatus status)
{
validator.validate(contact, result);
if (result.hasErrors())
{
return "newContact";
}
contactsDAO.save(contact);
status.setComplete();
return "redirect:viewAllContacts.do";
}
Service
public int save(Contacts contact)
{
int i = 0;
try
{
i = (Integer) sessionFactory.getCurrentSession().save(contact);
Contacts contacts =new Contacts();
contacts.setAddress("ABCD");
contacts.setMobile("8181");
i = (Integer) sessionFactory.getCurrentSession().save(contacts);
}
catch(Exception exception)
{
exception.printStackTrace();
}
return i;
}
EDIT
@RequestMapping(value="/saveContact", method=RequestMethod.POST)
public String create(@ModelAttribute("newContact")Contacts contact, BindingResult result, SessionStatus status) throws SQLException
{
validator.validate(contact, result);
if (result.hasErrors())
{
return "newContact";
}
try {
contactsDAO.save(contact);
}
catch (Exception ex) {
System.out.println("enrtered");
result.reject("DUPKEY");
ex.printStackTrace();
return "redirect:saveContact.do";
}
status.setComplete();
return "redirect:viewAllContacts.do";
}
@Transactionalthere is no need of defining Transaction explicitly. so when exception occurs it throws out and shows that exception to the user, rather how can i tell user that some thing has went wrong@Transactional- surround with try/catch? But i am not getting any solution for this, can you please specify some code relevant for the above code