Tribune

Home » Liferay Portal » English » 3. Development

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
toggle
Karen Rivero
ResourceURL Problem
20 giugno 2011 19.49
Risposta

Karen Rivero

Punteggio: New Member

Messaggi: 9

Data di Iscrizione: 14 giugno 2011

Messaggi recenti

Hi all,

Im having some problems with the ResourceURL. Thank you all in advance.
I'm using
Liferay 5.2.3
Glassfish 2.x
Netbeans 6.7.1

Well I am trying to use the ResourceURL to open a PDF. The problem I hace is that it doesnt generate the PDF, so I thought that my .jasper was wrong, so I created a new project to try the jasperreport and it generates perfectly, so this was discarted, Next, I debug my application and I realized that my code never invoke the serveResource procedure, Maybe you can help me with this, I cant see my mistake.

My JSP.
 1
 2<script type="text/javascript">
 3    jQuery(document).ready(function(){
 4    jQuery('#btnOMObtenerImprimir_Click').dialog({
 5            autoOpen: false,
 6            width: 320,
 7            height: 180,
 8            modal: true,
 9            show: "blind",
10            hide: "explote",
11            resizable: false,
12            buttons: {
13            "  Si  ":function(){
14               document.getElementById("hdnOMObtenerEvento").value = 2;
15               document.frmOMObtener.submit();
16            },
17            "  No  ":function(){
18                jQuery(this).dialog("close");
19            }
20            }
21        });
22    })
23<html>
24    <head>
25        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
26        <title>Episodio Medicos Generales</title>
27    </head>
28    <body>
29        <form method="post" action="<%=renderResponse.createActionURL().toString()%>" name="frmOMObtener" id="frmOMObtener">
30                <table width="100%" cellpadding="0" cellspacing="0">
31                    <tr>
32                        <td style="text-align: right">
33                            <input type="button" value="Imprimir" name="btnOMObtenerImprimir" id="btnOMObtenerImprimir" style="font-family:Helvetica; font-size:11px" onclick="jQuery('#btnOMObtenerImprimir_Click').dialog('open')">
34                        </td>
35                    </tr>
36                </table>
37                <input id="hdnOMObtenerEvento" name="hdnOMObtenerEvento" type="hidden">
38                <input id="hdnOMObtenerKRecetaMedica" value="<%=K_RecetaMedica%>" name="hdnOMObtenerKRecetaMedica" type="hidden">
39                <div id="btnOMObtenerImprimir_Click" title="Confirmar" style="font-family: Helvetica" class="confirmar">
40                    <table width="300px">
41                        <tr>
42                            <td style="text-align:center; font-size:12px">
43                                <b>En orden poder imprimir se generará la vista previa.  <br/>
44                                    ¿Desea continuar? <br/>
45                                    Por favor cierre la página al terminar.</b>
46                            </td>
47                        </tr>
48                    </table>
49                </div>
50        </form>
51    </body>
52</html>
53</script>


My portlet code
 1
 2package edu.upc.saludable.sisregame.Portlets;
 3
 4import edu.upc.saludable.sisregame.ServiceClient.WSCOrdenMedicamento;
 5import javax.portlet.GenericPortlet;
 6import javax.portlet.ActionRequest;
 7import javax.portlet.RenderRequest;
 8import javax.portlet.ActionResponse;
 9import javax.portlet.RenderResponse;
10import javax.portlet.PortletException;
11import java.io.IOException;
12import javax.portlet.PortletRequestDispatcher;
13import javax.servlet.http.HttpServletRequest;
14
15public class OrdenMedicamentoObtener extends GenericPortlet {
16
17    WSCOrdenMedicamento  wscOrdenMedicamento = null;
18    public static int eventoEM = 0;
19    String recetamedica = "";
20
21    public void processAction(ActionRequest request, ActionResponse response) throws PortletException,IOException {
22        if (wscOrdenMedicamento==null){
23            wscOrdenMedicamento = new WSCOrdenMedicamento();
24        }
25        eventoEM = Integer.parseInt(request.getParameter("hdnOMObtenerEvento"));
26        switch (eventoEM){
27            case 2:
28                recetamedica = "/WEB-INF/jsp/ReporteRecetaMedica.jsp?recetamedica=" +
29                         request.getParameter("hdnOMObtenerKRecetaMedica").toString();
30                break;
31        }
32    }
33   
34    public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {       
35      
36        PortletRequestDispatcher dispatcher = null;
37        switch (eventoEM)
38        {
39               case 2:
40                    PortletRequestDispatcher dispatcher = null;
41                    response.setContentType("text/html");
42                    ResourceURL resourceURL = response.createResourceURL();
43                    resourceURL.setParameter("resourceType", "pdf");
44        break;
45                case 0:
46                    response.setContentType("text/html");
47                    dispatcher = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/OrdenMedicamentoObtener_view.jsp");
48                            dispatcher.include(request, response);
49                            break;
50        }
51    }
52
53public void serveResource(ResourceRequest reqRequest, ResourceResponse resResponse) throws PortletException, IOException
54    {
55        String rType = ParamUtil.getString(reqRequest, "pdf");
56        HttpServletRequest request = PortalUtil.getHttpServletRequest(reqRequest);
57        HttpSession session = request.getSession();
58        ThemeDisplay themeDisplay = (ThemeDisplay)reqRequest.getAttribute(WebKeys.THEME_DISPLAY);
59        Company company = themeDisplay.getCompany();
60
61        if(rType != null && rType.equals("pdf"))
62        {
63            try{
64                Connection conn;
65                Class.forName("com.mysql.jdbc.Driver").newInstance();
66                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/saludable_final","root","root");
67
68                File reportFile = new File(getPortletContext().getRealPath("Reportes") + "\\RecetaMedica_Paciente.jasper");
69                Map parameters = new HashMap();
70                parameters.put("_c_recetamedica", c_recetamedica);
71                ByteArrayOutputStream baos = new ByteArrayOutputStream();
72                byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath (), parameters,conn);
73
74                resResponse.setContentType("application/pdf");
75                resResponse.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=360, must-revalidate");
76                resResponse.setBufferSize(baos.size());
77
78                OutputStream ouputStream = resResponse.getPortletOutputStream();
79                baos.write(bytes,0,bytes.length);
80                baos.writeTo(ouputStream);
81                ouputStream.flush();
82                ouputStream.close();      
83            }
84            catch(Exception e){
85                System.out.print("error: " + e.toString());
86            }
87        }
88    }
Thiago Leão Moreira
RE: ResourceURL Problem
21 giugno 2011 14.14
Risposta

Thiago Leão Moreira

LIFERAY STAFF

Punteggio: Liferay Legend

Messaggi: 1433

Data di Iscrizione: 10 ottobre 2007

Messaggi recenti

hey Karen,

You should use in your jsp the ResourURL object instead of ActionURL something like this.

1action="<%=renderResponse.createResourceURL().toString()%>"


And I think you should research more about portlets technology. I can see some confusing in your code. ;)
Karen Rivero
RE: ResourceURL Problem
21 giugno 2011 19.53
Risposta

Karen Rivero

Punteggio: New Member

Messaggi: 9

Data di Iscrizione: 14 giugno 2011

Messaggi recenti

Thiago Leão Moreira:
hey Karen,

You should use in your jsp the ResourURL object instead of ActionURL something like this.

1action="<%=renderResponse.createResourceURL().toString()%>"


And I think you should research more about portlets technology. I can see some confusing in your code. ;)


There is no way to do this in the processAction or the doView?
This is because Im trying to do this functionalitty. I have my portlet A with a button so when I click it, it add the portlet B, this portlet B is the one that contains the PDF.
If I use the code from the jsp I need an action, and I dont think I have it.

This is the page that I take as example.
http://blogs.oracle.com/satya/entry/new_feature_resource_serving_in

Finally, I have to admit that Im new in Liferay, but Im learning a lot I have display a few portlets but Im stuck in this functionalitty create a PDf with jasperreport and show it in the Liferay Portlet. I hope with the time I can learn more and optimize my code and in the future help people with my "know how" I hope you can understand me and can help me. Thanks a lot!
Karen Rivero
RE: ResourceURL Problem
21 giugno 2011 20.49
Risposta

Karen Rivero

Punteggio: New Member

Messaggi: 9

Data di Iscrizione: 14 giugno 2011

Messaggi recenti

I have an idea to gat the functionalitty that I said before.

Maybe I can add a portlet from the serveResource and in this portlet put the PDF is this possible?
Jignesh Thakkar
RE: ResourceURL Problem
21 giugno 2011 23.13
Risposta

Jignesh Thakkar

Punteggio: New Member

Messaggi: 24

Data di Iscrizione: 9 maggio 2010

Messaggi recenti

Hi Karen

Actually you have to call serveResouce method directly whenever you are trying to submit the form.

The below code will help you out to call serveResource Directly from jsp page.

<form action=<portlet:resourceURL/> method="post">
....content...
</form>

While in ServeResource Method , you can use below code to export a file as a PDF

JasperReport report = JasperCompileManager.compileReport(properties.getProperty("my.path") + "/" + reportfilename);
JasperPrint print = JasperFillManager.fillReport(report, paramMap, connection);
OutputStream os = response.getPortletOutputStream();
response.setContentType("application/pdf");
response.setProperty("Content-Disposition", "attachment; filename= "sample.pdf");

let me know this will help you out or not....