views:

65

answers:

2

Hi all,

When i try to use spring 2.5.x DispatcherPortlet with liferay to use Ajax, i am getting this error. I have configured it using annotations.

At class level i used following annotations,

Code for controller...

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

@SuppressWarnings("unchecked")
 @RequestMapping(method=RequestMethod.GET) //this is default rendering...
 public String defaultRender(Model model, PortletRequest request) {
 ...
 return "/some/someview";
 }


@RequestMapping(method=RequestMethod.POST,params="action=something") // For ajax call...using jQuery
 public ModelAndView processTotalMatchCount(RenderRequest request, Model model) throws IOException {
  Map<String,String> hmapStockSearchParams = getSomeData(request);  
  Integer totalMatches = service.getTotal(hmapStockSearchParams);
  Map map = new HashMap();
  map.put("totalMatches", totalMatches); 
  return new ModelAndView("ajaxView",map);
 }

}

From JSP i am calling following JS function using jQuery.

function fetchTotalMatches(){ 
 var url = "<portlet:renderURL><portlet:param name='action' value='something' /></portlet:renderURL>";
  jQuery.ajax({ url: url,
    method: 'POST', 
    //dataType:'json', when i enable this it goes in error bcs response is not in JSON format...
    data: {param1:'one',param2:'two'},      
    success: function(message) {
     alert(message);
     jQuery('input:hidden[@name="total"]').val(message.totalMatches);     
     jQuery('div.secondary-nav').children('div.viewMatches').
      html(message.totalMatches);
    }, 
    error: function(xhr, ajaxOptions, thrownError) {
     alert(xhr.status + " - " + xhr.statusText);
    }
   }
  );
}

When i enable dataType:'json' in jquery call, it goes in error function. Otherwise i will get the response alert of the whole HTML of the page defined in defaultRender method (The same jsp from which i m calling this function. )

And in console i am getting this error...

please help regarding this. What am i missing?

I have used Ajax related Resolver and View as per this link...

http://www.jtraining.com/blogs/ajax-with-spring-mvc-and-jquery.html

[2010-07-02 11:13:41,703][http-8080-4][ERROR] - org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:501) - Could not complete request
 java.lang.IllegalArgumentException
 at com.liferay.portlet.MimeResponseImpl.setContentType(MimeResponseImpl.java:162)
 at org.springframework.web.portlet.DispatcherPortlet.render(DispatcherPortlet.java:1090)
 at org.springframework.web.portlet.DispatcherPortlet.doRenderService(DispatcherPortlet.java:832)
 at org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:483)
 at org.springframework.web.portlet.FrameworkPortlet.doDispatch(FrameworkPortlet.java:453)
 at javax.portlet.GenericPortlet.render(GenericPortlet.java:233)
 at com.sun.portal.portletcontainer.appengine.filter.FilterChainImpl.doFilter(FilterChainImpl.java:126)
 at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:69)
 at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:100)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
 at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:535)
 at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:472)
 at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:618)
 at com.liferay.portlet.InvokerPortletImpl.invokeRender(InvokerPortletImpl.java:700)
 at com.liferay.portlet.InvokerPortletImpl.render(InvokerPortletImpl.java:419)
 at org.apache.jsp.html.portal.render_005fportlet_jsp._jspService(render_005fportlet_jsp.java:1467)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
 at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:535)
 at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:472)
 at com.liferay.portal.util.PortalImpl.renderPortlet(PortalImpl.java:2884)
 at com.liferay.portal.util.PortalUtil.renderPortlet(PortalUtil.java:897)
 at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processPortlet(RuntimePortletUtil.java:170)
 at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processPortlet(RuntimePortletUtil.java:103)
 at com.liferay.portlet.layoutconfiguration.util.velocity.PortletColumnLogic.processContent(PortletColumnLogic.java:133)
 at com.liferay.portlet.layoutconfiguration.util.velocity.TemplateProcessor.processColumn(TemplateProcessor.java:68)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.doInvoke(UberspectImpl.java:389)
 at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:378)
 at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:270)
 at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:252)
 at org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:332)
 at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:336)
 at org.apache.velocity.Template.merge(Template.java:328)
 at org.apache.velocity.Template.merge(Template.java:235)
 at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:381)
 at com.liferay.portal.velocity.VelocityEngineImpl.mergeTemplate(VelocityEngineImpl.java:179)
 at com.liferay.portal.kernel.velocity.VelocityEngineUtil.mergeTemplate(VelocityEngineUtil.java:84)
 at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processTemplate(RuntimePortletUtil.java:237)
 at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processTemplate(RuntimePortletUtil.java:190)
 at org.apache.jsp.html.portal.layout.view.portlet_jsp._jspService(portlet_jsp.java:831)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
 at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:535)
 at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:472)
 at com.liferay.portal.action.LayoutAction.includeLayoutContent(LayoutAction.java:294)
 at com.liferay.portal.action.LayoutAction.processLayout(LayoutAction.java:471)
 at com.liferay.portal.action.LayoutAction.execute(LayoutAction.java:195)
 at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
 at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
 at com.liferay.portal.struts.PortalRequestProcessor.process(PortalRequestProcessor.java:157)
 at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
 at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
 at com.liferay.portal.servlet.MainServlet.callParentService(MainServlet.java:608)
 at com.liferay.portal.servlet.MainServlet.service(MainServlet.java:846)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
 at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
 at com.liferay.portal.servlet.FriendlyURLServlet.service(FriendlyURLServlet.java:143)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.servlet.filters.strip.StripFilter.processFilter(StripFilter.java:142)
 at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:94)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.servlet.filters.gzip.GZipFilter.processFilter(GZipFilter.java:140)
 at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.servlet.filters.secure.SecureFilter.processFilter(SecureFilter.java:282)
 at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:94)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.servlet.filters.cache.CacheFilter.processFilter(CacheFilter.java:425)
 at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.servlet.filters.autologin.AutoLoginFilter.processFilter(AutoLoginFilter.java:257)
 at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.servlet.filters.sso.opensso.OpenSSOFilter.processFilter(OpenSSOFilter.java:73)
 at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.sharepoint.SharepointFilter.processFilter(SharepointFilter.java:193)
 at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.servlet.filters.virtualhost.VirtualHostFilter.doFilter(VirtualHostFilter.java:191)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.servlet.filters.threadlocalcache.ThreadLocalCacheFilter.processFilter(ThreadLocalCacheFilter.java:55)
 at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
 at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:94)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
 at java.lang.Thread.run(Thread.java:619)

Basically its not allowing to set content type to application\json as per the below class

public class AjaxView extends AbstractView {
 private static final Log log = LogFactory.getLog(AjaxView.class);
   private static final String REQUEST_CONTEXT_ATTRIBUTE = RequestContext.class.toString();

 public AjaxView() {
//  super();  
  setRequestContextAttribute(REQUEST_CONTEXT_ATTRIBUTE);
     setContentType("application/json;charset=UTF-8");     
 }
    /* (non-Javadoc)
     * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response) 
    throws Exception {
        log.info("Resolving ajax request view - "+map);        
        JSONObject jsonObj = new JSONObject(map);
        log.info("content Type = " + getContentType());     
//        response.reset();
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(jsonObj.toString());
        response.getWriter().flush();
    }
}

Please help ASAP. What i need is, Ajax call returning JSON string in spring 2.5.x MVC Portlet...

thanks in advance.

A: 

It seems to me you should use following

  • use dataType:'json'
  • use contentType: "application/json; charset=utf-8"
  • use JSON.stringify to convert your data (every property separately) which you gives per data parameters.

you can see under http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086 an example how to make JSON-encoding. Another link http://stackoverflow.com/questions/2670147/can-i-return-json-from-an-asmx-web-service-if-the-contenttype-is-not-json/2671583#2671583 can be probably also helpful.

Oleg
hey, i have already notified that the content i m getting is not coming as JSON. the params are getting sent sucessfully and my calculations are generating final result. The only problem is i am getting the whole html in callback..so.. pls help regarding that...
Paarth
hi, Now i changed my view class, and made new one whcih directly implementing View interface. Now i am giving new AjaxView() everytime for my ajax method. Sot the error is gone but still my content is not coming in JSON :(
Paarth
You should set `dataType` to `'json'` or to `'html'`. It is better to set both `dataType` and `contentType` in the `jQuery.ajax`. At least in two places in your code you set ContentType to `application/json` (see `AjaxView` which you posted) and you write that you need have no JSON???
Oleg
i need JSON, but server not sending JSON... Its sending HTML.. thats what my problem is... will try your note also... thx
Paarth
the main problem is, even i created my own `AjaxView` class, returning the new view every time i call ajax method, setting content type to json, making new `JSONObject` with my return data into it and setting json string into `response.getWriter().print()` from the `AjaxView` class... Still i am getting HTML :(. Is this a problem of/regarding Portlet's VIEW mode?
Paarth
I don't know Spring, but in general it is enough to set contentType in the response and write the data as a stream. Like in the example from the link (see http://www.jtraining.com/blogs/ajax-with-spring-mvc-and-jquery.html) which you posted: `response.getOutputStream().write( jsonString.getBytes() );`. Moreover in the example from the link one use jQuery 1.3.2. In jQuery 1.4.x you should be more careful with setting of dataType and contentType. If you don't do this jQuery try to guess and you can receive not what you want.
Oleg
i have used jquery 1.3.2...and trying to look into it why its fetching my default page HTML in that JSON request :(
Paarth
+1  A: 

Hi,

I have not found the solution with spring 2.5. So upgraded to 3.0.3 and tried to use ContentNEgotiatingViewResolver with JSON. But it was failing.

check the link for detailed explanation on spring 3.0.3 of my problem. http://stackoverflow.com/questions/3183805/error-contentnegotiatingviewresolverof-spring-3-0-3-mvc-portletjson

finally, i found that spring 3 is not supporting portlet JSON. So i moved to servlet and not using spring MVC for json. Only for JSPs...i m using it.

Paarth