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.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;
}