SpringMVC, jquery, tiles and partial re-rendering

I am trying to partially render my website using spring mvc 3.1, tiles 2 and jquery.

Here is 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 can see there is a nested attribute named "productSearchResults". This is a results page, and I want this page to be redisplayed using 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 the .js file included in 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 I change the value of the "searchCriteria.resultsSort" selector on the productsSearchResults.jsp page, I want the results tile to reload (without reloading the entire page).

( html-...), , .

, ? , ?

+5
1

, , ...

javascript :

$('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);
    }
});
});

!

- , "searchCriteria.resultsSort" , ,

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

! , .

+4

All Articles