Announcement Announcement Module
No announcement yet.
How to keep persitent data on JSP ( CRUD scenario under MVC ) Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to keep persitent data on JSP ( CRUD scenario under MVC )

    Hello folks

    I was requested to do some changes in a code that's working perfectly (but customers are there for keep asking for improvements).

    I have no clue how to do it so any ideas might help and they'll be appreciated.

    Given Scenario:

    I do have typical Web APP with its MVC pattern.

    Customer do have a JSP with a typical form.It retrieves data and saves them.However with a new change I made for retrieving another data , I do need to figure out how to keep all data in JSP , in order to avoid customer re-typing all the information once again when he is done.

    I've controller with its procedure calling a service wich calls a DAO and this one retrieves data from SELECT. Here the changed I did (all data were in one table retrieving always data, but now it doesn't. It can retrieve data or nothing)

    The main problem is the SELECT retrieving data. All data (personal data) was being retrieved from same table, until the happy idea of needing another data in a different table.

    Better if I show the code :


    	@RequestMapping( value="/doctors/PersonalData.action", method=RequestMethod.GET)
    	public void getPersonalData (Model model,HttpServletRequest request , @ModelAttribute("doctorsFormBean") DoctorsBean doctorsFormBean){
    		log.debug("Retrieving personal data...");
    		String msg = null;
    		String idConv = doctorsFormBean.getIdConv();
    		String idSol    = doctorsFormBean.getIdSol();
    		try {
    			doctorsFormBean = doctorService.getDoctorPersonalData(idConv, idSol);
    			//result can be having a register with data or no data , with the bean (pojo) null value in all its attributes
    			if (doctorsFormBean.getIdConv()==null){
    		} catch (ServiceException e) {
    			msg = "Error while retrieving data.";
    		model.addAttribute("doctorsFormBean", doctorsFormBean);
    		model.addAttribute("idConv", idConv);
    		model.addAttribute("idSol", idSol);
    	@RequestMapping(value="/doctors/PersonalData.action", method = RequestMethod.POST)
    	public void savePersonalData (Model model, HttpServletRequest request, @ModelAttribute("doctorsFormBean") DoctorsBean doctorsFormBean, BindingResult result){
    		log.debug("saving personal data "); 
    		String msg = null;
    		new FormDoctorValidator().validatePersonalData(doctorsFormBean, result);
    	   	if (!result.hasErrors()){
    	        try {
    			 msg= "The data has been saved successfully.";
    		    }catch (ServiceException e) {
    			   msg= "Error while saving personal data";
    		    result.addError(new ObjectError("error", messageSource.getMessage("doctors.error.personalData",null , Locale.getDefault())));
    		    msg = "Error while completing form, please check out the fields.";	
    		model.addAttribute("doctorsFormBean", doctorsFormBean);
    		model.addAttribute("idConv", doctorsFormBean.getIdConv());
    		model.addAttribute("idSol", doctorsFormBean.getIdSol());
    DAO Implementation (Here the problem, since I've changed query,I have "if" code part in controller. (Because the GET now it doesnt retrieve always data as before)

    	public DoctorsBean getPersonalData(String idConv String idSol) throws DaoException{
    		String[] args = {idConv, idSol};
    //this was original query
    //		String query =  "select name, surname, birthdate,  address, mail , sex , age, estate from DOCTORS where idConv=? and idSol=?";
    //this is the new one 		
    String query = "SELECT, Doc.surname, Doc.birthdate, Doc.adress, Doc.mail,, Doc.age,  FROM STATES AS ST INNER JOIN  DOCTORS_GRANTS  AS DOCG ON ST.idState = DOCSUB .idState " 
    				     + "INNER JOIN DOC_SOLICITATIONS AS Doc ON DOCG.idConv = Doc.idConv AND DOCG.grantNumber = Doc.grantNumber "
    				     + "WHERE Doc.idConv= ? AND Doc.idSol = ? " ;
    		try {
    		return this.getJdbcTemplate().queryForObject(
    		        new RowMapper<DoctorsBean>() {
    		            public DoctoresBean mapRow(ResultSet rs, int rowNum) throws SQLException {
    		            	DoctorBean perDatDoctor = new DoctorBean();
    		            	if (rs.getTimestamp("fechaNacimiento") == null){
    		            	SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    		            	Date bdDate = rs.getTimestamp("birthdate");
    		            	String birth_date = sdf.format(bdDate);
    		            	return perDatDoctor;}
    		} catch (DataAccessException e) { 
    			return new DoctorsBean();

    When saving data clicking save button, works, but once I do another action or even go to the same action I've lost all the data filled in the form... (my doctorsFormBean (Object of the JSP in the model) is empty once I come back to get part.

    Because of the select returning empty bean , I have somehow find the way to keep all data somewhere else in order to avoid losing them when doing any other action.

    May be should I try @SesssionAttributes ? (Which yet I have no clue )

    Any help will be appreciated, thanks in advance.