control Inversion

control Inversion

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Locked
smiles
Posts: 1
Joined: 31 Mar 2010, 12:15

control Inversion

Post by smiles »

Hello

I 'm trying to use the inversion of control to display the Vacationtype for the classe Vacation.
HAve you got any idea of how to use??

this my DAO for both classes

Code: Select all

@SuppressWarnings("serial")
@Entity
@Table(name = "vacationtype", catalog = "proxym")
public class Vacationtype implements java.io.Serializable {

	private Integer idvacationtype;
	private String type;
	

	public Vacationtype() {
	}

	
	public Vacationtype(Integer idvacationtype) {
		this.idvacationtype = idvacationtype;
	}

	public Vacationtype(Integer idvacationtype, String type) {
		this.idvacationtype = idvacationtype;
		this.type = type;	
	}


	@Id
	@Column(name = "idvacationtype", unique = true, nullable = false)
	public Integer getIdvacationtype() {
		return this.idvacationtype;
	}

	public void setIdvacationtype(Integer idvacationtype) {
		this.idvacationtype = idvacationtype;
	}

	@Column(name = "type", length = 45)
	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
and my Vacation classe

Code: Select all

@Entity
@Table(name = "vacation", catalog = "proxym")
public class Vacation implements java.io.Serializable {

	

	public Vacation() {
	}

	public Vacation(Vacationtype vacationtype) {
		this.vacationtype = vacationtype;
	}

	public Vacation(Vacationtype vacationtype, String startDate,
			String endDate, Double duration, Integer numberOfWorkingDays,
			Integer halfDay, String createdAt, String deletedAt) {
		this.vacationtype = vacationtype;
		this.startDate = startDate;
		this.endDate = endDate;
		this.duration = duration;
		this.numberOfWorkingDays = numberOfWorkingDays;
		this.halfDay = halfDay;
		this.createdAt = createdAt;
		this.deletedAt = deletedAt;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "idvacation", unique = true, nullable = false)
	public Integer getIdvacation() {
		return this.idvacation;
	}

	public void setIdvacation(Integer idvacation) {
		this.idvacation = idvacation;
	}

	@ManyToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "vacationtype_idvacationtype")
	public Vacationtype getVacationtype() {
		return this.vacationtype;
	}

	public void setVacationtype(Vacationtype vacationtype) {
		this.vacationtype = vacationtype;
	}

	@Column(name = "Start_date", length = 65535)
	public String getStartDate() {
		return this.startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	@Column(name = "End_date", length = 65535)
	public String getEndDate() {
		return this.endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	@Column(name = "duration", precision = 22, scale = 0)
	public Double getDuration() {
		return this.duration;
	}

	public void setDuration(Double duration) {
		this.duration = duration;
	}

	@Column(name = "number_of_working_days")
	public Integer getNumberOfWorkingDays() {
		return this.numberOfWorkingDays;
	}

	public void setNumberOfWorkingDays(Integer numberOfWorkingDays) {
		this.numberOfWorkingDays = numberOfWorkingDays;
	}

	@Column(name = "half_day")
	public Integer getHalfDay() {
		return this.halfDay;
	}

	public void setHalfDay(Integer halfDay) {
		this.halfDay = halfDay;
	}

	@Column(name = "created_at", length = 65535)
	public String getCreatedAt() {
		return this.createdAt;
	}

	public void setCreatedAt(String createdAt) {
		this.createdAt = createdAt;
	}

	@Column(name = "deleted_at", length = 65535)
	public String getDeletedAt() {
		return this.deletedAt;
	}

	public void setDeletedAt(String deletedAt) {
		this.deletedAt = deletedAt;
	}

}
This my Implementation for both classes

Code: Select all

private HibernateTemplate hibernateTemplate;

	public void setSessionFactory(SessionFactory sessionFactory) {
	
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	@Override
	public void createVacation(Vacation vacation) {
		hibernateTemplate.saveOrUpdate(vacation);

	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Vacation> findallVacation() {
		 vacationList=hibernateTemplate.find("from Vacation");
		 return vacationList;
	}
	@Override
	public void deleteVacation(Vacation vacation) {
		
		
		hibernateTemplate.delete(vacation);

	}

	@Override
	public void editVacation(Vacation vacation) {
		
		hibernateTemplate.update(vacation);
	}
and for VacationtypeDAOImpl

Code: Select all

@Override
	public void createVacationtype(Vacationtype type) {

		hibernateTemplate.saveOrUpdate(type);

	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Vacationtype> findallVacationtype() {
		vacationtypeList = hibernateTemplate.find("from Vacationtype");
		return vacationtypeList;
	}

	@Override
	public Vacationtype findtype(Integer idvacationtype) {
		return (Vacationtype) hibernateTemplate
				.find("select type from Vacationtype where idvacationtype ="
						+ idvacationtype);

	}http://springrts.com/phpbb/posting.php?mode=post&f=12&sid=39b826f6d879b70f7b236fcd35164bb7

	public Vacationtype getvacationtypeById(Integer idvacationtype) {
		for (Vacationtype vacationtype : vacationtypeList) {
			if (vacationtype.getIdvacationtype() == idvacationtype)
				return vacationtype;
		}
		return null;
	}
My VacationController

Code: Select all

public class VacationController extends MultiActionController {

	private VacationDAO vacationDAO;
	private VacationtypeDAO vacationtypeDAO;
	protected final Log logger = LogFactory.getLog(getClass());
	
	
	public void setVacationDAO(VacationDAO vacationDAO) {	
		this.vacationDAO = vacationDAO;
	}
	public VacationDAO getVacationDAO() {	
		return (vacationDAO);
	}
	
	public void setVacationtypeDAO(VacationtypeDAO vacationtypeDAO) {	
		this.vacationtypeDAO = vacationtypeDAO;
	}
	public VacationtypeDAO getVacationtypeDAO() {	
		return (vacationtypeDAO);
	}
	
	 
	 protected Object formBackingObject(HttpServletRequest request) throws Exception {
	    	Vacation defaultVacation = new Vacation();
	    	defaultVacation.setStartDate("12211");
	    	defaultVacation.setEndDate("15545");
	    	defaultVacation.setDuration(20.00);
	    	defaultVacation.setNumberOfWorkingDays(4215247);
	    	defaultVacation.setHalfDay(013245);
	    	defaultVacation.setCreatedAt("11454");
	    	defaultVacation. setDeletedAt("54154");
	    	return defaultVacation;
	    }
	
    @SuppressWarnings("unchecked")
	protected Map referenceData(HttpServletRequest request) throws Exception {
    	Map<Object, Object> dataMap = new HashMap<Object, Object>();
    	dataMap.put("vacationtypeList", this.vacationtypeDAO.findallVacationtype());
    	return dataMap;
    }

    
    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    	binder.setDisallowedFields(new String[] {"vacationtype"});
    	
    	Vacation vacation = (Vacation)binder.getTarget();
    	Integer idvacationtype = null;
    	try {
    		idvacationtype = Integer.parseInt(request.getParameter("vacationtype"));
		} catch (Exception e) {}		
		if (idvacationtype != null) {
			Vacationtype vacationtype = this.vacationtypeDAO.getvacationtypeById(idvacationtype);
			vacation.setVacationtype(vacationtype);
		}    
    }

[code]
	public ModelAndView addVacation(HttpServletRequest request,
			HttpServletResponse response,Vacation vacation) throws ServletException, IOException, ParseException {
	
		//System.out.println("vacation : "+vacation.toString());
		vacationDAO.createVacation(vacation);
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("vacationList", vacationDAO.findallVacation());
		modelMap.addAttribute("vacation", new Vacation());
		return new ModelAndView("addVacation",modelMap);
	}
	
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response)  throws ServletException, IOException {
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("vacationList",vacationDAO.findallVacation());
		modelMap.addAttribute("vacation", new Vacation());
		return new ModelAndView("vacationForm", modelMap);
	}
	
	public ModelAndView deleteVacation(HttpServletRequest request,
			HttpServletResponse response, Vacation vacation) throws Exception {
		vacationDAO.deleteVacation(vacation);
		return new ModelAndView("redirect:list.htm");
	}	
	
	public ModelAndView editVacation(HttpServletRequest request,
			HttpServletResponse response, Vacation vacation) throws Exception {
		vacationDAO.editVacation(vacation);
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("vacationList", vacationDAO.findallVacation());
		modelMap.addAttribute("vacation", new Vacation());
		return new ModelAndView("editVacation",modelMap);
	}
<bean name="/vacation/*.htm" class="com.proxymit.grh.web.VacationController" >
<property name="vacationDAO" ref="myvacationDAO" />

</bean>

<bean id="myvacationtypeDAO" class="com.proxymit.grh.service.interfImpl.VacationtypeDAOImpl">
<property name="sessionFactory" ref="mySessionFactory"/>

</bean>
<bean name="/vacationtype/*.htm" class="com.proxymit.grh.web.VacationtypeController" >
<property name="vacationtypeDAO" ref="myvacationtypeDAO" />
</bean>[/code]
part of my addVacation.jsp where I call the vacationtypeList

Code: Select all

Type<br />
		<form:select path="vacationtype">
		   <form:options   items="${vacationtypeList}" itemLabel="type" itemValue="idvacationtype" />
		</form:select>
		<br /><br />
</td>
		</tr>

		<tr>
			<td colspan="2"><input type="submit" value="Register"></td>
		</tr>
	</table>
Could you help please ?? :shock:
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: control Inversion

Post by imbaczek »

this is spring the rts engine, not spring the web framework forum.
Locked

Return to “Engine”