开发者

Spring Portlet MVC: Forward Request parameters to model

I got two views:

  1. Displays some common information about an entity
  2. Displays more details about this entity

The second view has a link back to the first one. To create the link I need some informations, lets say two ids ("id1" and "id2"). Theses ids are passed to the controller of the second view by request parameters.

To create the back link in the second view, I have to forward the request parameters to the model manualy. This is not very convenient.

Is there any 开发者_开发百科approach to do this forwarding automatically?

Here an example:

Link to details in the first view:

<portlet:renderURL var="detailsUrl">
    <portlet:param name="action" value="showDetails" />
    <portlet:param name="id1" value="${entity.id1}" />
    <portlet:param name="id2" value="${entity.id2}"/>
</portlet:renderURL>
<a href="${detailsUrl}">Details</a>

Render method in the second controller:

@RenderMapping(params = "action=showDetails")
public String showDetails (
        @RequestParam("id1") int id1,
        @RequestParam("id2") int id2,
        Model model) {
    // The current unconvenient approach
    model.addAttribute("id1", id1);
    model.addAttribute("id2", id2);

    return "showDetails";
}

Back link in the second view:

<portlet:renderURL var="entityUrl">
    <portlet:param name="action" value="showEntity" />
    <portlet:param name="id1" value="${id1}" />
    <portlet:param name="id2" value="${id2}"/>
</portlet:renderURL>
<a href="${entityUrl}">Back</a>

I searched the internet for houres to find something like a trick I missed. But the only thing I found was: "Spring dose it automatically for you". But I can't confirme this.

Thanks for your help...


Ok, if @ModelAttribute does not help how about a bit more complex approach:

Make a class that implements org.springframework.web.portlet HandlerInterceptor. You need to override a bunch of methods, but in one of them postHandleRender, do the setting of your id's to the model.

Make sure that every implementing method that returns a boolean, returns true. Otherwise your portlets methods might not get called (if, for example, preHandleRender returns false).

The interceptor must be introduced to your portlet in order for it to work, ofcourse. This can be done by adding the following to the portlet's context-config:

<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <bean class="some.package.to.my.interceptors.MyInterceptor"/>
    </property>         
</bean>


Try marking your id's with @ModelAttribute:

@RenderMapping(params = "action=showDetails")
public String showDetails (
        @ModelAttribute @RequestParam("id1") int id1,
        @ModelAttribute @RequestParam("id2") int id2,
        Model model) {
    // The current unconvenient approach
    //model.addAttribute("id1", id1);
    //model.addAttribute("id2", id2);

    return "showDetails";
}

Notice, that i commented out the explicit setting of id's to the model. I did not test this, so please do report back here if it works..


You can simply write a method like below and add generate a Map. Add it to the Model Map.

private Map<String, String> getParamterMap(PortletRequest request){
        Map<String, String> parameters = new HashMap<String, String>();

        Set<String> keySet = request.getParameterMap().keySet();
        for(String key : keySet){
            parameters.put(key, request.getParameter(key));
        }

        return parameters;

    }

Call this method from your RenderMapping method to add all your request parameters to the ModelMap.

modelMap.addAllAttributes(getParamterMap(request));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜