0

SpringMVC: How is model object accessed from ZUL view?

asked 2010-07-21 05:58:40 +0800

thrane gravatar image thrane
43 3

How is a Spring MVC model object accessed from a ZUL view?

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2010-07-21 10:14:16 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

You not get the answer you wanted from here?: Your other thread

link publish delete flag offensive edit

answered 2010-07-21 19:43:56 +0800

samchuang gravatar image samchuang
4084 4

updated 2010-07-21 19:47:40 +0800

Hi

From Your other thread

I think caclark has answered the question. If not, Could you provide your sample code? and explain what do you wanna achieve

link publish delete flag offensive edit

answered 2010-07-23 03:39:44 +0800

thrane gravatar image thrane
43 3

First, thanks caclark + samchuang for your replys!

What I want to achive is this:

1) Usage of SpringMVC controller to populate ModelAndView object

I'm using POJO's to implement SpringMVC controllers.
In the controllers I use the @ModelAttribute anotation to populate the ModelAndView object.
Here's an example of an controller named ReportsController,where the ModelAnView object is populated
with the attribute "reports" through the usage of the @ModelAttribute anotation:

@Controller
@RequestMapping("/reports")
public class ReportsController {

static final String REPORTS_VIEW = "reports";

@Resource
ReportRepository reportRepository;

/**
* Populates model with reports.
*
* @return Collection of reports.
*/
@ModelAttribute("reports")
public Report[] populateReports() {
return reportRepository.getReports();
}

/**
* initializes the model and view for
* reports overview.
*
* @return new model and view object which contains
* the 'reports' view name.
*/
@RequestMapping(method=RequestMethod.GET)
public ModelAndView initializeReportsForm() {

ModelAndView mav = new ModelAndView();
mav.setViewName(REPORTS_VIEW);
return mav;
}
}

The result is:
a. I have implemented the C in the MVC pattern, e.g. my ReportsController class.
b. I have implemented the M in the MVC pattern, e.g. the initialized ModelAndView object returned by the initializeReportsForm() method. And populated the model by the populateReports() method.

...the last step is the V in MVC...

2) Accessing the content of the ModelAndView from views

If I implement views using JSP I can access the "reports" attribute from the model like this:

<form:select path="reports" items="${reports}" itemLabel="name" /> e.g. the variable ${reports} is resolved from the ModelAndView object.

But (Here's the problem), if I implement the views using ZUL pages, then I can't find a way to resolve variables from the ModelAndView object. This is illustrated with the example below where the variable @{reports.reports} should (BUT DOESN'T) be resolved from "resports" attribute in the model:

<groupbox id="reportsGroup" mold="default" >
<caption label="Reports"/>
<listbox model="@{reports.reports}" selectedItem="@{selected}" mold="paging" rows="10">
<listhead >
<listheader label="Name"/>
</listhead>
<listitem self="@{each=report}" >
<listcell label="@{report.id}"/>
</listitem>
</listbox>
</groupbox>


What I can do (and currently does) is to refering directly to bean defined in my Spring application context. This is illustrated with the example below where the variable @{reportRepository.reports} refers to a Spring defined bean named reportRepository:

<groupbox id="reportsGroup" mold="default" >
<caption label="Reports"/>
<listbox model="@{reportRepository.reports}" selectedItem="@{selected}" mold="paging" rows="10">
<listhead >
<listheader label="Name"/>
</listhead>
<listitem self="@{each=report}" >
<listcell label="@{report.id}"/>
</listitem>
</listbox>
</groupbox>

..the consequence of this workaround is that the usage of the model object is degraded, since it is no longer used to transfer data to the views.

To sum it up:

The problem is that the resolvers doesn't resolve variables from the ModelAndView object but from the Spring application context.

I think it is a feature missing from ZK Spring, because it would make it much easier to migrate to ZK from another view technology while retaining your SpringMVC based application architecture. Is it possible to add a feature request for this to the ZK Spring project?

Finally, I think the solution must be implemented as described by aclark: "...but I think it puts the ModelAndView as a request attribute. If so, you could create your own DelegatingVariableResolver and have it look for the bean in the request first and then in the Spring WebApplicationContext if it doesn't find it. The class org.zkoss.zkplus.databind.AnnotateDataBinderInit is very small and could be cloned and customized easily...."

link publish delete flag offensive edit

answered 2010-07-23 07:34:30 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

Is it possible to add a feature request for this to the ZK Spring project? 

See that menu item in the upper right part of the page labeled Feature Requests?. That's the official mechanism.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-07-21 05:58:40 +0800

Seen: 715 times

Last updated: Jul 23 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More