0

Spring portlet JSP, Making ajax request and in controller trying to get jsp page so that i can pass and generate pdf output.

But problem is didn't get any string data but html contents are returned on jsp page please check code as follwoing

@Controller("exportSummaryController")
@RequestMapping(value = "VIEW")
public class ExportSummaryController implements PortletConfigAware  {

    @ResourceMapping("exportAccRequest")
    public void accountRollupAction(@RequestParam("accNum") String accNum, 
        @RequestParam("sourceId") String sourceId, @RequestParam("serviceId") String serviceId, 
        @RequestParam("summaryExport") String strExport, ResourceRequest request, ResourceResponse response) throws Exception {

        //processing data

        ResourceResponseWrapper responseWrapper = new ResourceResponseWrapper(response) {
            private final StringWriter sw = new StringWriter();

            @Override
            public PrintWriter getWriter() throws IOException {
                return new PrintWriter(sw);
            }

            @Override
    public OutputStream getPortletOutputStream() throws IOException {
                return(new StringOutputStream(sw));
            }
            @Override
            public String toString() {
                return sw.toString();
            }

        };

        portletConfig.getPortletContext().getRequestDispatcher("/WEB-INF/jsp/account_summary.jsp").include(request, responseWrapper);
        String content = responseWrapper.toString();
        System.out.println("Output : " + content); // here i found empty output on command line but output is returned to jsp page.
    }    
}

public class StringOutputStream extends OutputStream {
        private StringWriter stringWriter;

        public StringOutputStream(StringWriter stringWriter) {
            this.stringWriter = stringWriter;
        }

        public void write(int c) {
            stringWriter.write(c);
        }
    }
d-man
  • 57,473
  • 85
  • 212
  • 296
  • I updated the code but still not luck there is some thing missing cuz output still going in parent response and when i debug the code getwritter is not being called. – d-man Aug 22 '13 at 14:51

1 Answers1

0

In your code the output is cosumed by only one OutputStream.

Try this,

ResourceResponseWrapper responseWrapper = new ResourceResponseWrapper(response) {
        private final StringWriter sw = new StringWriter();

        @Override
        public PrintWriter getWriter() throws IOException {
            return new PrintWriter(sw){

                @Override
                public void write(String s, int off, int len)
                {
                    try
                    {   sw.write(s, off, len);
                        response.getWriter().write(s, off, len);
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            };
        }


        @Override
        public String toString() {
            return sw.toString();
        }

    };
Niranjan
  • 1,776
  • 1
  • 13
  • 21
  • Try overriding `getPortletOutputStream()` as well in the above code. I don't know why it isn't working for you, which worked perfectly for me on `Liferay/Tomcat` server. – Niranjan Aug 22 '13 at 15:24
  • i tried overriding getPortletOutputStream but i din't know why not working. – d-man Aug 22 '13 at 18:12
  • can you please copy paste your controller code, may be i am missing some thing and i can find from it. – d-man Aug 23 '13 at 13:21