Usecases for ModelDriven interface
Hi, I am using struts2 on my current project and find ModelDriven interface very inconvenient. The intent of the interface is documented as, it helps directly populating domain model. But if the domain model is little more complex than a simple bean, it becomes very inconvinient. e.g. If my domain model is as follows class Order { String orderNumber; UserInformation user; } class UserInformation { String firstName; String lastName; Address address; } class Address { String addressLine1; String city; String state; } The problem with ModelDriven is that I have to use OGNL expressions like user.address.addressLine1 in my HTML form. While this is not a bigger issue for the simple example as above, it can be awkward for little more complex domain models. What suits better for those domain models is to have a builder, which has setters for all the parameters on the form and has responsibility to build the actual domain model objects. Something like following class OrderBuilder { String orderNumber; String firstName; String lastName; String addressLine1; String city; String state; public Order build() { .. } } I can offcourse use this builder as Model, fooling struts framework like following class MyAction imeplements ModelDrivenOrderBuilder { private OrderBuilder builder; public OrderBuilder getModel() { builder = new OrderBuilder(); return builder; } public void execute() { orderBuilder.build(); // Then use order } } But I think this reads very badly. Instead, will it make more sense to have a annotation for parameter mapping strategy? Something like Instead of class MyAction implements ModelDrivenOrder have following @BeanMappingStrategy(beanName=order) //expects OGNL in parameter names to map to bean class MyAction { Order order; } or @BuilderMappingStrategy(builderName=oderBuilder) // knows that its dealing with builder, so will call build method. class MyAction { OrderBuilder orderBuilder; } What do you guys think? Thanks, Unmesh _ Drag n’ drop—Get easy photo sharing with Windows Live™ Photos. http://www.microsoft.com/india/windows/windowslive/photos.aspx
RE: Usecases for ModelDriven interface
I think what you're looking for is a more flexible Mapping which can be achieved via Spring http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/DispatcherServlet.html which would enable you to define your own DefaultAnnotationHandlerMapping http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/mvc/annotation/DefaultAnnotationHandlerMapping.html bean class=org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping property name=interceptors ... /property /beanIf your annotated class is a Web Controller then you can use Controller.htm http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/stereotype/Controller.html//Once your spring framework is achieved you can implement RequestMapping http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/bind/annotation/RequestMapping.html where Annotation for mapping web requests onto specific handler classes and/or handler methods. Provides consistent style between Servlet and Portlet environments, with the semantics adapting to the concrete environment. so as you can see there is alot of flexibility with Handlers,Annotations and Controller available with Spring Martin __ Disclaimer and Confidentiality/Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité This message is confidential. If you should not be the intended receiver, then we ask politely to report. Each unauthorized forwarding or manufacturing of a copy is inadmissible. This message serves only for the exchange of information and has no legal binding effect. Due to the easy manipulation of emails we cannot take responsibility over the the contents. Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen. Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni. From: unmesh_jo...@hotmail.com To: user@struts.apache.org Subject: Usecases for ModelDriven interface Date: Tue, 5 May 2009 17:47:09 + Hi, I am using struts2 on my current project and find ModelDriven interface very inconvenient. The intent of the interface is documented as, it helps directly populating domain model. But if the domain model is little more complex than a simple bean, it becomes very inconvinient. e.g. If my domain model is as follows class Order { String orderNumber; UserInformation user; } class UserInformation { String firstName; String lastName; Address address; } class Address { String addressLine1; String city; String state; } The problem with ModelDriven is that I have to use OGNL expressions like user.address.addressLine1 in my HTML form. While this is not a bigger issue for the simple example as above, it can be awkward for little more complex domain models. What suits better for those domain models is to have a builder, which has setters for all the parameters on the form and has responsibility to build the actual domain model objects. Something like following class OrderBuilder { String orderNumber; String firstName; String lastName; String addressLine1; String city; String state; public Order build() { .. } } I can offcourse use this builder as Model, fooling struts framework like following class MyAction imeplements ModelDrivenOrderBuilder { private OrderBuilder builder; public OrderBuilder getModel() { builder = new OrderBuilder(); return builder; } public void execute() { orderBuilder.build(); // Then use order } } But I think this reads very badly. Instead, will it make more sense to have a annotation for parameter mapping strategy? Something like Instead of class MyAction implements ModelDrivenOrder have following @BeanMappingStrategy(beanName=order) //expects OGNL in parameter names to map to bean class MyAction { Order order; } or @BuilderMappingStrategy(builderName=oderBuilder) // knows that its dealing with builder, so will call build method. class MyAction { OrderBuilder orderBuilder; } What do you guys think? Thanks, Unmesh
RE: ModelDriven interface
After working on this for a while, I beginning to think that that ModelDriven interface is not what I had thought it was. It is certainly not a replacement for the formBeans in Struts 1. I guess I just need to forget most of what I've used in Struts 1 :) Thanks all for your help.
Re: ModelDriven interface
What do you mean not what you thought it was? M-- This email message and any files transmitted with it contain confidential information intended only for the person(s) to whom this email message is addressed. If you have received this email message in error, please notify the sender immediately by telephone or email and destroy the original message without making a copy. Thank you. - Original Message - From: Matt Luce [EMAIL PROTECTED] To: user@struts.apache.org Sent: Monday, June 11, 2007 8:51 PM Subject: RE: ModelDriven interface After working on this for a while, I beginning to think that that ModelDriven interface is not what I had thought it was. It is certainly not a replacement for the formBeans in Struts 1. I guess I just need to forget most of what I've used in Struts 1 :) Thanks all for your help. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
ModelDriven interface
I'm working with Struts 2.0.6 and I'm trying to use the ModelDriven Interface. However, I seem to be having some troubles. My actionClass implements ModelDriven, but the values on the page don't fill with the values of the object returned by getModel(). If I explicitly declare the getXxx() methods for each attribute of the model within the Action, the values are populated in the .jsp form. Matt Luce Wells Fargo Funds Management, LLC 414.577.7927 414.359.3537 Fax
Re: ModelDriven interface
On 6/9/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I'm working with Struts 2.0.6 and I'm trying to use the ModelDriven Interface. However, I seem to be having some troubles. My actionClass implements ModelDriven, but the values on the page don't fill with the values of the object returned by getModel(). If I explicitly declare the getXxx() methods for each attribute of the model within the Action, the values are populated in the .jsp form. The correct way of populating the values IS declaring the getters and setters for each object you want to populate. Matt Luce Wells Fargo Funds Management, LLC 414.577.7927 414.359.3537 Fax - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: ModelDriven interface
--- Nicolás Pace [EMAIL PROTECTED] wrote: The correct way of populating the values IS declaring the getters and setters for each object you want to populate. If you implement ModelDriven then the model object returned should be on the object stack allowing non-qualified access on the form. d. Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase. http://farechase.yahoo.com/ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: ModelDriven interface
Then why have the modelDriven interface at all? What use is it? Matt Luce Wells Fargo Funds Management, LLC 414.577.7927 414.359.3537 Fax
Re: ModelDriven interface
I agree, it should be on the stack, but the values don't show up. Does anyone have an example of this that works?
Re: ModelDriven interface
--- [EMAIL PROTECTED] wrote: I agree, it should be on the stack, but the values don't show up. Does anyone have an example of this that works? I have a ScopedModelDriven example, but it's really, really boring. IIRC there's a ModelDriven example in showcase, but it's even more boring than mine--really, there isn't much that can go wrong. Are you using the default interceptor stack or have you rolled your own? d. Boardwalk for $500? In 2007? Ha! Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games. http://get.games.yahoo.com/proddesc?gamekey=monopolyherenow - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: ModelDriven interface
I'm using the default stack. What I'm trying to do is something very simple, that was incredibly straightforward in Struts 1. What I'm finding is the getModel() method is not getting called when I expect it to. It doesn't get called until after the action has been hit once. But that doesn't help, because the input page doesn't hit the action until the form is submitted. So somehow, I need to get my bean (the object returned by getModel()) on the ValueStack before the input page is rendered. I have no idea how to do that. In Struts 1, all I had to do was create the form bean in a pre-action, throw it in session, then forward to the input page. I don't know why Struts 2 doesn't allow for something that simple.
RE: ModelDriven interface
Here is the flow of my tiny app: listAction - list.jsp - selectAction - update.jsp - updateAction (implements ModelDriven - getModel() returns the object selected in list.jsp stored in session ) Once it gets to update.jsp, the form should show the values of the bean selected on list.jsp. But, since getModel() is not called until the update.jsp is posts to updateAction, the model is not on the value stack. - struts.xml: struts include file=struts-default.xml/ constant name=struts.devMode value=true/ constant name=struts.ui.theme value=simple/ constant name=struts.objectFactory value=org.apache.struts2.spring.StrutsSpringObjectFactory / package name=default extends=struts-default action name=list class=listAction result/list.jsp/result result name=error/index.jsp /result /action action name=select class=selectAction result/update.jsp/result /action action name=save class=updateAction result name=input/update.jsp/result result/update.jsp/result result name=error/update.jsp/result /action /package /struts --- ListAction: import com.wellsfargo.fmg.appaudit.domain.AuditCode; import java.util.ArrayList; import java.util.List; public class ListAction extends AppAuditAction { public String execute() { List codes = new ArrayList(); AuditCode code = new AuditCode(); code.setCode(1); code.setDescription(Really cool code); codes.add(code); this.getSession().put(codes,codes); return SUCCESS; } } - list.jsp %@ taglib prefix=s uri=/struts-tags % %@ taglib uri=/WEB-INF/c.tld prefix=c % html head titleApp Audit Maint/title /head body h2 App Audit Code Listing /h2 table s:iterator value=#attr.codes tr td s:property value=code / - s:property value=description / s:url id=selectUrl value=/select.action s:param name=code value=code / /s:url s:a href=%{selectUrl}Edit/s:a /td /tr /s:iterator /table /body /html - SelectAction: import com.wellsfargo.fmg.appaudit.domain.AuditCode; import java.util.Iterator; import java.util.List; public class SelectAction extends AppAuditAction { public String execute() throws Exception { String[] codeParam = (String[])this.getParameters().get(code); List codes = (List)this.getSession().get(codes); for (Iterator it = codes.iterator();it.hasNext();) { AuditCode c = (AuditCode)it.next(); if (c.getCode() == Integer.valueOf(codeParam[0]).intValue()) { this.getSession().put(auditCode,c); break; } } return SUCCESS; } } - update.jsp %@ taglib prefix=s uri=/struts-tags % html head titleApp Audit Maint/title /head body h2 Update Audit Code /h2 s:actionerror/ s:form action=save table tr td Code: s:property value=code/ /td /tr tr td Description: s:textfield name=description/ /td /tr /table s:submit value=submit/ /s:form /body /html - import com.opensymphony.xwork2.ModelDriven; import com.wellsfargo.fmg.appaudit.domain.AuditCode; public class UpdateAction extends AppAuditAction implements ModelDriven { public UpdateAction() { System.out.println(UPDATE action constructor); } public String execute() throws Exception { this.addActionError(Error On Execute); return INPUT; } public int getCode() { AuditCode auditCode = (AuditCode)this.getSession().get(auditCode); return auditCode.getCode(); } public String input() throws Exception { return super.input();//To change body of overridden methods use File | Settings | File Templates. } public Object getModel() { System.out.println(getModel Called); AuditCode auditCode = (AuditCode)this.getSession().get(auditCode); return auditCode; //return this.auditCode; } } the super class for all my actions: import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.interceptor.SessionAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.ParameterAware; import java.util.Map; public abstract class AppAuditAction extends ActionSupport implements SessionAware, RequestAware, ParameterAware { private Map session; private Map request; private Map parameters; public void setSession(Map map) { this.session = map; } public void setRequest(Map map) { this.request = map; } public void
[s2] Making LazyDynaBean work with ModelDriven interface
Some folks may be as pig-headed as me or may need to use LazyDynaBeans as they are translating from struts1 to struts2. After fooling around, I figured it out but it is pretty clear to me this is not the way to go. Below is a simple example. public final class MyAction extends ActionSupport implements ModelDriven{ private LazyDynaBean f = new LazyDynaBean(); public String execute() throws Exception { String[] text_field = (String[])f.get(text_field); //do something with the text_field that came from LazyDynaBean as a String array return SUCCESS; } public Object getModel(){ return f; } } s:form action=MyAction s:textfield name=map.text_field / s:submit / /s:form validators field name=map.text_field field-validator type=required messageText Field is required./message /field-validator /field /validators There were 2 things that bit me: 1. You have to get the field name from map.text field so that s:textfield can get it from the map on the valuestack and 2. The textfield seems to get a String[] rather than a String and I don't have any idea how to do it differently. mas - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] Making LazyDynaBean work with ModelDriven interface
On 12/5/06 12:25 PM, Mark Shifman [EMAIL PROTECTED] wrote: 2. The textfield seems to get a String[] rather than a String and I don't have any idea how to do it differently. I don¹t know if you can do this because I've never really worked with Struts 1. Can you make the Map in your form a MapObject,String? That might make the values be String instead of String[]. This is only an idea. I'm not sure OGNL will do this with generics. You could test it easily enough by making your model just a MapObject,String and see if that works. Mark - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
[s2] LazyDynaBean and ModelDriven interface
Does anybody have an example of using a LazyDynaBean with the ModelDriven interface? http://struts.apache.org/2.x/docs/struts-1-solutions.html implies that it should work. I have a simple action: public final class MyAction extends ActionSupport implements ModelDriven{ private LazyDynaBean f; public String execute() throws Exception { String my_field = (String)f.get(my_field); LOG.fatal(my_myfield); //nothing here :-( return SUCCESS; } public Object getModel() { return f = new LazyDynaBean(); } } I don't get anything from the LazyDynaBean. If I use a simple pojo like public class myBean { String my_field; public String getMy_field() { return my_field; } public void setMy_field(String my_field) { this.my_field= my_field } Things work. What am I missing or doing wrong? Thanks in advance mas -- Mark Shifman MD. Ph.D. Yale Center for Medical Informatics Phone (203)737-5219 [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] LazyDynaBean and ModelDriven interface
Mark Shifman wrote: Does anybody have an example of using a LazyDynaBean with the ModelDriven interface? http://struts.apache.org/2.x/docs/struts-1-solutions.html implies that it should work. I have a simple action: public final class MyAction extends ActionSupport implements ModelDriven{ private LazyDynaBean f; public String execute() throws Exception { String my_field = (String)f.get(my_field); LOG.fatal(my_myfield); //nothing here :-( Opps I made a typo for this example should be: LOG.fatal(my_field); return SUCCESS; } public Object getModel() { return f = new LazyDynaBean(); } } I don't get anything from the LazyDynaBean. If I use a simple pojo like public class myBean { String my_field; public String getMy_field() { return my_field; } public void setMy_field(String my_field) { this.my_field= my_field } Things work. What am I missing or doing wrong? Thanks in advance mas -- Mark Shifman MD. Ph.D. Yale Center for Medical Informatics Phone (203)737-5219 [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]