Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringMVC, jquery, tiles, and partial rerendering

I'm trying to do a partial rendering of my web site, using spring mvc 3.1, tiles 2, and jquery.

Here's my spring conf :

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

My tiles conf :

<definition name="productSearch" extends="baseLayout">
    <put-attribute name="mainSection">
        <definition template="/WEB-INF/views/productSearch.jsp">
            <put-attribute name="productSearchResults" value="/WEB-INF/views/productSearchResults.jsp" />
        </definition>
    </put-attribute>
</definition>

As you see there's a nested attribute named "productSearchResults". This is the results page, and I want this page to be re-rendered via ajax.

My controller :

@RequestMapping(params = "search=true", value = "/", method = RequestMethod.POST)
public String searchHandler(@Valid final SearchFormBean searchformBean, final Model model) {
    model.addAttribute("productsList", productsService.findProducts(searchformBean.getSearchCriteria()));
    return "productsSearch";
}

My jsps :

productsSearch.jsp :

<form:form id="product-search-form" method="post" modelAttribute="productSearchFormBean">
    <input type="hidden" name="search" value="" />
    <form:input path="searchCriteria.name" />
    // AND SOME OTHER FIELDS...
    <button type="submit" onclick="this.form.search.value='true';">Search</button>
</form>

productSearchResults.jsp :

<div id="products-result">
    <div id="search-results-info" class="section-content">
            Order results :
        <form:select path="searchCriteria.resultsSort">
            <form:option value="orderByName">Name</form:option>
            <form:option value="orderByDame">Date</form:option>
        </form:select>
    </div>

    <div id="products-content" class="section-content">                 
        <c:forEach var="product" items="${productsList}">
            <article>
                // PRODUCT INFORMATIONS DISPLAYED HERE...
            </article>
        </c:forEach>
    </div>
</div>

and finally a .js file included in the productSearch.jsp :

$('select[id="searchCriteria.resultsSort"]').change(function() {
    $.ajax({
        type : "POST",
        url : "/myapp/product/search/",
        data : "search=true&fragments=productSearchResults",
        success : function(response) {
            $("#search-results").html(response);
        },
        error : function(e) {
            alert('Error : ' + e);
        }
    });
});

So here's the thing : every time that I change the "searchCriteria.resultsSort" selector value in the productsSearchResults.jsp page, I want the results tile to be reloaded (without reloading the whole page).

With the code given above, I manage to re-render the whole page (including the html tag...) but not just the portion that interest's me.

Any hint on how to achieve my goal ? Is it really possible or I misunderstood the partial rendering principle ?

like image 579
Christos Loupassakis Avatar asked Oct 08 '22 16:10

Christos Loupassakis


1 Answers

I found what was wrong, so I'm answering my own question...

I just changed the javascript code into this :

$('select[id="searchCriteria.resultsSort"]').change(function() {
$.ajax({
    type : "POST",
    beforeSend : function(req) {
        req.setRequestHeader("Accept", "text/html;type=ajax");
    },
    url : "/myapp/product/search/",
    data : "search=true&fragments=productSearchResults",
    success : function(response) {
        $("#search-results").html(response);
    },
    error : function(e) {
        alert('Error : ' + e);
    }
});
});

And now it works !

Beacause of the partial-re-rendering, it seems that the "searchCriteria.resultsSort" selector is no longer mapped to the form, once the page is reloaded to the screen, so I had also to add this line to my controller :

model.addAttribute("searchCriteria", searchformBean.getSearchCriteria());

That's all ! Hope it helps other people.

like image 125
Christos Loupassakis Avatar answered Oct 12 '22 13:10

Christos Loupassakis