|
From: Darren D. <da...@da...> - 2003-11-12 01:49:35
|
On Tuesday 11 November 2003 23:27, Darren Davison wrote:
> Is there something already in Spring MVC that lends itself to this kind
> of behaviour?
without really thinking this through much (since it's gone 1:30am and I have
to be up again in 4 hours)..
If an interface..
public interface ModelReference {
public Map getModel();
}
.. is implemented by some arbitrary business objects, then a view could be
defined as..
<bean id="myVelocityView"
class="org.springframework.web.servlet.view.velocity.VelocityView">
<property name="templateName"><value>main.vm</value></property>
<!-- standard attribs -->
<property name="attributes">
<props>
<prop key="title">A Velocity Page</prop>
</props>
</property>
<!-- new property: list of ModelReference implementing bus. objects -->
<property name="references">
<list>
<ref external="myBean"/>
<ref external="myOtherBean"/>
</list>
</property>
</bean>
AbstractView is amended with the following (part pseudo-code)..
public abstract class AbstractView extends WebApplicationObjectSupport
implements View {
//added
private Map referenceObjects = new HashMap();
...
//added
public final void setReferencesList(List references) {
for each list item
get bean from app context if exists
if bean instanceof ModelReference
call bean.getModel()
consolidate into referenceObjects field
end
end
}
//amended
public final void render(Map model, HttpServletRequest request,
HttpServletResponse response)
...
Map mergedModel = new HashMap(this.staticAttributes);
mergedModel.putAll(referenceObjects);
mergedModel.putAll(model);
...
}
}
- The controller still has final say and can overwrite model values from
the static list or the reference list
- Arbitrary beans can now add to the model that a view renders on a per
view basis taking advantage of whatever container resources or custom logic
is required
- multiple views can use the same ref objects via the hierarchical view
structure
- It's a kind of half-way house between static attributes and model data
returned by the controller but as shown isn't parameterised so reasonably
rigid
- maybe the interface can be dropped and the actual method to call for any
given bean can be specified in the config too. One less dependency for the
business object
Goodnight.
--
Darren Davison
Public Key: http://www.davison.uk.net/key.jsp
|