Páginas

18 de febrero de 2013

llamadas a un servlet desde gwt



hacer la clase del servlet 


@WebServlet(name = "nombre_servlet", urlPatterns = {"/nombre_proyecto/nombre_servlet"})
public class no_clase_servlet  extends HttpServlet  {

// variable serializable 


//aquie va la chicha
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
      doPost(req, resp);
    }


@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
String id = request.getParameter("id");
String salida = "";
Typo_variable variable = new Typo_variable();
  nota = (Typo_variable)request.getAttribute("id_varible");



}

}//fin de la clase



llamada a un servlet desde gwt


 String url = GWT.getModuleBaseURL() + "nombre_servlet?id="+id+"& id_varible ="+ variable;
RequestBuilder rq = new RequestBuilder(RequestBuilder.POST,url);
rq.setHeader("Content-Type","multipart/form-data");
rq.setHeader("Access-Control-Allow-Origin", "*");
try {
rq.sendRequest(null, new RequestCallback() {

@Override
public void onError(Request arg0, Throwable arg1) {
// TODO Auto-generated method stub
}

@Override
public void onResponseReceived(Request arg0, Response arg1) {
//Info.display("","exito "+arg0.toString());
//Info.display("","exito "+arg1.getText());
String sCoSiniestro = "";
//System.out.println(arg1.getStatusCode());
try {
sCoSiniestro = new String(arg1.getText().getBytes("ISO-8859-1"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println("text "+arg1.getText());
//System.out.println("text "+arg1.getStatusText());
//System.out.println("text "+arg1.getHeadersAsString());
//System.out.println("string "+arg1.toString());
str_imprimir = sCoSiniestro;
//display.setData(sCoSiniestro);

}
});
} catch (RequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

llamada para una descarga de documentos


com.google.gwt.user.client.Window.open(GWT.getModuleBaseURL() + "nombre_servlet?tipoInforme=19", null, null );

otra menera

String cadena = GWT.getModuleBaseURL() + "nombre_servlet?id="+id+"& id_varible ="+ variable;
com.google.gwt.user.client.Window.open(cadena, 
                        "_blank", "");

imprimir en GWT


en el evento del boton para imprimir


Element nodoiFrame = display.getDetalle().getElement().getChild(1).getChild(0).getChild(0).getChild(0).getParentElement();
//nodoiFrame.getFirstChildElement().setId("imprimir");
it(nodoiFrame.getFirstChildElement());


buscando el compone iframe donde esta el conetado de lo que queremos imprimir

// display.getDetalle().getElement().getChild(1).getChild(0).getChild(0).getChild(0).getChild(0).getParentElement().setId("imprimir");
Element nodoiFrame = display.getDetalle().getElement().getChild(1).getChild(0).getChild(0).getChild(0).getParentElement();
nodoiFrame.getFirstChildElement().setId("imprimir");


el codigo que hace imprimir

public static native void it() /*-{ 
        $wnd.print();
    }-*/;

    public static native void it(String html) /*-{
        var frame = $doc.getElementById('imprimir');
        if (!frame) {
            $wnd.alert("Error: Can't find printing frame."); 
            return;
        }
        frame = frame.contentWindow;
        var doc = frame.document;
        doc.open();
        doc.write(html);
        doc.close();
        frame.focus();
        frame.print();
    }-*/;

    public static void it(UIObject obj) {
        it("", obj.getElement().toString());
    }

    public static void it(Element element) {
      it("", element.toString());
    }
   

    public static void it(String style, String it) {
        it("
"+style+"
"+it+""); 
    }

    public static void it(String style, UIObject obj) {
        it(style, obj.getElement().toString());
    }

    public static void it(String style, Element element) {
        it(style, element.toString ());
    }

29 de enero de 2013

Subida archivos con FileUploadField de GXT

- Configuración FormPanel:

        FormPanel fp = new FormPanel();
        fp.setEncoding(Encoding.MULTIPART);
        fp.setMethod(Method.POST);
        fp.setAction("uploadServlet"); // Path del servlet de subida

- Configuración FileUploadField:

        FileUploadField flupldFld = new FileUploadField();
        flupldFld.setName("file");

- Añadir el FileUploadField al FormPanel:

       fp.add(flupldFld, new FormData("100%"));

- Configuración Button:

      myButton.addSelectionListener(new SelectionListener() {

            @Override
            public void componentSelected(ButtonEvent ce) {
               
                if (display.getFlupldFld().getValue() != null) {
                    fp.submit();
                }
            }
        });

- Añadir listener de respuesta de la subida al FormPanel

      fp.addListener(Events.Submit, new Listener () {

            @Override
            public void handleEvent(final FormEvent be) {

                Log.info("[be.getResultHtml()] = " + be.getResultHtml());
                }
            }
        });

- Creas un servlet y con este código tendrás el archivo (necesitarás la librería de apache commons-fileupload)

      FileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      List items = (List) upload.parseRequest(request);

- También en el servlet puedes escribir algo en su salida para que lo puedas recibir en el 'be.getResultHtml()' del listener que le pusiste al FormPanel.

      response.getOutputStream().print("Fichero subido correctamente");

creacion de properties en un proyecto gwt




se crea una clase la nombre -> ConfigsGetter

//es la localizacion donde esta nuestro archivo de propertis

private static final String BUNDLE_NAME = "/res/config/file.properties";

public ConfigsGetter() {
}
public static String getString(String key) {
try {
InputStream in = ConfigsGetter.class.getResourceAsStream(BUNDLE_NAME);
if (in == null ) {
System.out.println("Properties file: " + BUNDLE_NAME + " not found!");
return null;
}
else {
Properties pro = new Properties();
pro.loadFromXML(in);
return pro.getProperty(key);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

en el archivo  file.properties

key="plantilla">/res/config/fichero.xls
key="plantilla1">/res/config/fichero1.xls
key="plantilla2">/res/config/fichero2.xls
key="plantilla3">/res/config/fichero3.xls

para llamarlo 

ConfigsGetter.getString("plantilla")