Announcement Announcement Module
Collapse
No announcement yet.
How to keep persitent data on JSP ( CRUD scenario under MVC ) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 :

    Controller

    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){
    				
    				doctorsFormBean.setIdConvocatoria(idConv);
    				doctorsFormBean.setIdSolicitud(idSol);
    	
    			}
    			
    		} catch (ServiceException e) {
    			log.error(e);
    			msg = "Error while retrieving data.";
    		}
    		
    	
    		model.addAttribute("doctorsFormBean", doctorsFormBean);
    		model.addAttribute("idConv", idConv);
    		model.addAttribute("idSol", idSol);
    		model.addAttribute("msg",msg);
    	}
    	
    
    	@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 {
    			 doctorService.savePersonalData(doctorsFormBean);
    			 msg= "The data has been saved successfully.";
    		    }catch (ServiceException e) {
    			   log.error(e);
    			   msg= "Error while saving personal data";
    		    }
    	   	}
    	   	else{
    		    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());
    		model.addAttribute("msg",msg);
    	}
    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)

    Code:
    @Override
    	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.name, Doc.surname, Doc.birthdate, Doc.adress, Doc.mail, Doc.sex, Doc.age, ST.estate  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(
    		       query, 
    		        args,
    		        new RowMapper<DoctorsBean>() {
    		            public DoctoresBean mapRow(ResultSet rs, int rowNum) throws SQLException {
    		            	DoctorBean perDatDoctor = new DoctorBean();
    		            	perDatDoctor.setName(rs.getString("name"));
    		            	perDatDoctor.setSurname(rs.getString("surname"));
    
    		            	if (rs.getTimestamp("fechaNacimiento") == null){
    		            		perDatDoctor.setBirthDate("");
    		            	}
    		            	else{
    		            	
    		            	SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    		            	Date bdDate = rs.getTimestamp("birthdate");
    		            	String birth_date = sdf.format(bdDate);
    		            	perDatDoctor.setBirthDate(birth_date);
    		            	}
    		            	
    		            	perDatDoctor.setAddress(rs.getString("address"));
    		            	perDatDoctor.setMail(rs.getString("mail"));
    		            	perDatDoctor.setSex(rs.getString("sex"));
    		            	perDatDoctor.setAge(rs.getInt("age"));
    		            	perDatDoctor.setState(rs.getString("state"));
    		            	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.
Working...
X