Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render partial JSP fragments in Spring MVC?

I am quite new to Spring MVC, and trying to figure out how to render a partial view without refreshing the whole page. I thought:

  • if I can post a request to the backend, in this case, a Spring controller method

  • And if this method fills in model attribute, and then return ModelAndView object with specified view name mapping to a JSP file, and JSP is able to generat the necessary HTML fragment.

  • The returned HTML fragment will be inserted into a DIV placeholder.

This probably wouldn't work, but I am not sure what's wrong with it? I got as far as the JSP is called upon to render the view, but for some reason, the model attribut I put into the session doesn't seem to be there.

I'd appreciate if anyone with similar experience can provide some examples on this subject, or point to me where it potentially went wrong.

I read something from Spring web flow to handle Ajax request with fragmented view, but I am not sure I get the idea on how it actually works.

like image 201
Oliver Avatar asked Oct 30 '10 03:10

Oliver


1 Answers

I wouldn't say my answer below necessarily answers your question, but I arrived at this page via Google while I was attempting to replicate the Spring AjaxEventDecoration (that only loads fragments of a page), so here is what I have now discovered in case anyone else ever arrives here:

The quick answer is to ensure you set the 'Accept' property of the XMLHttpRequest to 'text/html;type=ajax' which then tells the Spring MVC that only (comma separated) fragments specified by the URL parameter 'fragments' should be returned.

I am using Tiles server-side and jQuery client side, this is a rough overview of my setup:

An example webmvc-config.xml

...
<bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.js.ajax.tiles2.AjaxTilesView" />
</bean>
...

An example page's Tiles configuration

<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>

    <definition name="eventPage" extends="twoColumnLayout">
        <put-attribute name="title" value="My Event Page" />
        <put-attribute name="view" value="/WEB-INF/jsp/viewText.jsp" />
        <put-attribute name="objectViewer" value="/WEB-INF/jsp/viewEvent.jsp" />
    </definition>

</tiles-definitions>

I have a Java controller class that returns an instance of ModelMap that is mapped to the request 'eventPage' (.html in my case).:

@Controller
public class EventPageController
{
    /**
     * Method to handle main my events page.
     */
    @RequestMapping(value = "/eventPage.html", method = RequestMethod.GET)
    public ModelMap pageDisplayHandlerForEvents(ModelMap model, HttpServletRequest req)
    {
        ...
    }
}

And then my JavaScript client-side code (that for examples sake always loads the 'objectViewer' fragment):

Note: Your HTML elements that are loaded as fragments must have an ID attribute matching that of the fragment ID.

$(".springFragmentLoader").click(function()
{
    $.ajax(__contextRoot + "/eventPage.html?fragments=objectViewer", {
        beforeSend: function(req) {
            req.setRequestHeader("Accept", "text/html;type=ajax");
        },  
        complete : function(jqXHR)
        {
            $("#objectViewer").html(jqXHR.responseText);
        }
    });
}

For more detail see Handling Ajax Requests.

Hope that helps.

like image 102
Ed . Avatar answered Oct 21 '22 00:10

Ed .