Extract AJAX JSON data (portlet + Liferay + JSON + Spring)

What I want to do is just send the data to the server and get the data from the server using JSON and AJAX. I am converting an old AJAX project to AJAX. This is the spring ibatis jquery portlet. The submission of the form was successful, and I could not get the data from the server as JSON. I used the spring-mvc-jquery-autocomplete-example sample to learn Jackson for JSON. It was very easy for me to not even think about JSON. I just copy-paste two jar files into my build path and comment on @ResponseBody in my method. But still the answer is a full html page. Why?

JSP code here

   <portlet:actionURL var="formAction">
        <portlet:param name="action" value="submit"/>
    </portlet:actionURL>


    <c:set var="formPortletNamespace">form<portlet:namespace/></c:set>

    <form:form method="post" action="${formAction}" commandName="uiState" id="${formPortletNamespace}" cssClass="travelInsurancePortletForm jqCustomStyle" autocomplete="off">
        <% /* Selected action as a parameter */ %>
        <input type="hidden" name="portletAction" id="portletAction"/>

        <form:hidden path="quote.quotingWebApp" />

JS is here. This code sends the form to the server

function doPortletAction(actionName) {
   jQuery('form#form<portlet:namespace/> input#portletAction').val(actionName); 

   jQuery('form#form<portlet:namespace/> input#<portlet:namespace/>-posted').val('true');
//jQuery('form#form<portlet:namespace/>').submit();


jQuery.ajax({
    url: jQuery('#form<portlet:namespace/>').attr("action"),
    type: 'POST',
    datatype:'json',
    data: jQuery('#form<portlet:namespace/>').serialize(),
    success: onAjaxSubmitReturn
});

}

Controller coding

@Controller
@RequestMapping("VIEW")
public class MyController{

    @ActionMapping(params="portletAction=myAction")
    public @ResponseBody UiState myAction( 
            PortletSession session, 
            ActionResponse response, 
            @RequestParam(value="endDate", required=false) Date endDate,
            @ModelAttribute("uiState") UiState requestUiState,
            BindingResult errors,
            ModelMap mm) throws Exception {

    UiState uiState=new UiState ();
        return uiState;

}
+4
1

, Action . Render. , .

-, Spring 4.1 @ResponseBody . 4.2, .

, @RenderMapping, @ResourceMapping ( JSR-286/Portlet 2.0).

- @ResourceMapping. :

@ResourceMapping("myAction")
public void myAction(
        PortletSession session,
        ResourceResponse response,
        @RequestParam(value="endDate", required=false) Date endDate,
        @ModelAttribute("uiState") UiState requestUiState,
        BindingResult errors,
        ModelMap mm) throws Exception {

    UiState uiState = new UiState ();

    //TODO extra logic

    //explicitly set JSON as response type
    response.setContentType("application/json");

    //write JSON into output
    response.getWriter().write(serializeJSON(uiState));
}

Whereis serializeJSON - .

( Jackson ):

public static String serializeJSON(Object o) throws IOException {
    return new ObjectMapper().writeValueAsString(o);
}

: JSP (-):

<portlet:resourceURL var="formAction" id="myAction"/>
+2

All Articles