These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: mapping issue: add formdata into objects via jsp/Struts
PostPosted: Fri Nov 11, 2005 10:11 am 
Newbie

Joined: Tue Oct 25, 2005 3:20 pm
Posts: 3
I have an Event object with an EventType:

(snipped of Event.java)
private Long id;
private EventType eventType;
private String title;
private String intro;

And a snipped of the mapping file (Event.hbm.xml):

<many-to-one name="eventType"
class="com.egoactive.lego.event.model.EventType"
column="EVENT_TYPE_ID"
not-null="true"
lazy="true"/>

I have 1 JSP/Struts issue

How t enter a new event via a webform:
<%
Event event = new Event();
request.setAttribute("element", event);

//retrieve all evenTypes
List eventTypeList = EventTypeService.getInstance().getList();
request.setAttribute("eventTypes", eventTypeList);
%>
<html:form action="deleteEvent-submit.do" method="post">

<!--create option list--->
<html:select name="element" property="eventType" >
<html:options collection="eventTypes" property="id"
labelProperty="value" />
</html:select>
<html:text name="element" property="title" />
<html:submit property="submit"/>
</html:form>

The problem is the html:select bit..what should the proporty be? Or should I define my html:option bit different?

Thanks In advance.
Niels


Top
 Profile  
 
 Post subject: id
PostPosted: Fri Nov 11, 2005 12:18 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I think it should be different. The HTML select submits EventType id only therefore receiving property should be of id type, not the EventType type.

<!--create option list--->
<html:select name="element" property="eventTypeId" >
<html:options collection="eventTypes" property="id"
labelProperty="value" />
</html:select>

On the struts form you can do something like this:

setEventTypeId( id){
getEvent().setEventType( daoLayer.getEventTypeByID(id) );
}

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 5:03 pm 
Newbie

Joined: Tue Oct 25, 2005 3:20 pm
Posts: 3
Thanks...I am coming there but ..

Event event = new Event();
request.setAttribute("element", event);

So I don't have a eventTypeId in my Event object,

what I can do:
<html:select name="element" property="eventType.id" >
<html:options collection="eventTypes" property="id" labelProperty="value" />
</html:select>

But how should I map this field (eventType.id) in my struts form?

Something like...

private EventType eventType;

public EventType getEventType() {
return eventType;
}

..but I want the id of the EventType..

Thanks


Top
 Profile  
 
 Post subject: 2c
PostPosted: Mon Nov 14, 2005 1:25 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I suggest using Strut's ActionForm as a helper structure that holds relevant data, in this case we could code like this:

In the Action that extends BaseAction (assuming that base action takes care of redirecting to given functions ):

public ActionForward createNew(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
EventForm form = (EventForm) actionForm;
form.setModeCreate();
form.setEvent( new Event() );
form.setEventTypes( getListOfTypesSomehow() );
return mapping.findForward("crud");
}

public ActionForward saveNew(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
EventForm form = (EventForm) actionForm;
Event evt = form.getEvent( );
eventDao.saveEvent( evt );
form.setModeEdit();
return mapping.findForward("crud");
}


EventForm extends CRUDForm{
Event event;
List eventTypes;

Event Event()....
setEvent()....

....

Integer getEventTypeId(){
getEvent().getEventTYpe().getId();
}

void setEventTypeId( Integer id){
EventType et = findEventTypeInTheListById( id, getEventTypes() );
getEvent().setEventType( et );
}

}

Note that the code assumes that eventTyoe field of the Event is never NULL, if that is nullable then we need guards here and there.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.