PDA

View Full Version : Events.Submit don´t work for FromPanel Encoding.MULTIPART and Method.POST



andre_guitar7
28 May 2009, 10:06 AM
Olá!

Quando um FormPanel possuí Encoding.MULTIPART e Method.POST, ele nunca entra no listener Events.Submit.


formDeEdicao.setEncoding(Encoding.MULTIPART);
formDeEdicao.setMethod(Method.POST);
formDeEdicao.addListener(Events.Submit, new Listener<FormEvent>()
{
public void handleEvent(FormEvent formEvent)
{
//NÃO PASSA POR AQUI
}
});Isso é um BUG?

calhau
10 Aug 2009, 1:13 PM
I have same problem...

sven
10 Aug 2009, 2:31 PM
Many people use this without any problems. Dont forget about the same origin policy within the browsers. Moving this to the help forum.

lucasdeoliveira
20 Aug 2009, 9:43 AM
Same problem here: I have a class that extends FormPanel (intented to upload files to a servlet) and I have the following client code:



//...
this.setMethod(Method.POST);
this.setEncoding(Encoding.MULTIPART);
this.setAction(fileUploadAction);

this.addListener(Events.Submit, new Listener<FormEvent>() {
public void handleEvent(FormEvent arg0) {
MessageBox.alert("Result", "Check Forums on how to get an actual result: "
+ arg0.getResultHtml(), null);
atualizarPesquisa();
}
});
//...
here goes the server side code:


FileItem uploadItem = getFileItem(request); //ommited, it works anyway
byte[] fileContents = uploadItem.get();


// persist file on db... (ommited)

response.setStatus(HttpServletResponse.SC_CREATED);
response.setContentType("text/plain");
response.getWriter().print("id do arquivo = " + Arquivo.numeroArquivos);
response.flushBuffer();
I've tried both hosted andserver mode but unfortunately when I attempt to this.submit() nothing happens. As a test I tried the Events.BeforeSubmit and it worked fine. Not sure what I'm missing...

any help will be apreciated.

thanks in advance!

coebs
1 Sep 2009, 2:15 AM
I have the same problem as described above.

My goal is to submit a form to a server-side cgi and get the response.

But the Events.Submit is never fired in my Listener.

A summary of my code:

FormPanel panel = new FormPanel();
panel.setMethod(FormPanel.Method.POST);
panel.setEncoding(FormPanel.Encoding.URLENCODED);
panel.setAction(url);
panel.addListener(Events.Submit, new Listener<BaseEvent>() {
public void handleEvent(BaseEvent be) {
System.out.println("Hello World");
}
});

Any response from EXT JS? Thanks in advance ...

lucasdeoliveira
1 Sep 2009, 5:40 AM
I believe it worked on versions prior than 2.x =/, but the projects I'm working on demand the use of some nice stuff presented on 2.x. So I've got no way out. Do you guys have any other idea to bypass this problem?

cheers!

Henrique Miranda
1 Sep 2009, 6:30 PM
I was using GWT 1.6 with GXT 2.0 my app was working, but when i changed for GWT 1.7 and GXT 2.0.1 form event don't work more.

coebs
1 Sep 2009, 8:31 PM
Okay, here is an update of my case.

IN MY CASE, Events.Submit doesn't be fired under the GWT hosted mode (in eclipse), but after it to be compiled, I can get the expected result(i.e. Events.Submit is fired after formPanel.submit() being called) by using FireFox browser, though still not by using IE 8.0.6. :-?:-?:-?

lucasdeoliveira
2 Sep 2009, 3:01 AM
Howdy!

I was using GWT 1.6 with GXT 2.0 my app was working, but when i changed for GWT 1.7 and GXT 2.0.1 form event don't work more.

can you share the piece of your code that does the event.submit treatment with us?

thanks a lot guys! let's make this thing work =D

cheers!

Henrique Miranda
2 Sep 2009, 3:08 AM
formPanel = new FormPanel();
formPanel.setAction(GWT.getModuleBaseURL()+"/Pc.pcUpload");
formPanel.setEncoding(FormPanel.Encoding.MULTIPART);
formPanel.setMethod(FormPanel.Method.POST);
formPanel.setBorders(false);
formPanel.setBodyBorder(false);
formPanel.setLabelWidth(80);
formPanel.setPadding(5);
formPanel.setHeaderVisible(false);
formPanel.setLayout(new ColumnLayout());
formPanel.addListener(Events.Submit, new Listener<FormEvent>(){
public void handleEvent(FormEvent be) {
eventoFormSubmit(be);
}
});

lucasdeoliveira
2 Sep 2009, 3:16 AM
@Henrique Miranda

yep, same code for me as well, still you're luckier since it worked for GWT 1.6 and GxT 2.0, which is also my case although mine doesn't work (strangely Event.BeforeSubmit is ok)
=/

I'll try generating the war file to specific browsers like COEBS did. Later on I'll post a status report as well.

cheers!

coebs
3 Sep 2009, 10:28 PM
Hi guys, any updates?! Let's share our solutions and discoveries ... remember that the information can help other users of the community too ...

Henrique Miranda
4 Sep 2009, 5:14 AM
i am testing my application in Hosted Mode, FF 3.5 and IE8. Is working in FF but isn't working in IE and Hosted Mode.

maticpetek
6 Sep 2009, 9:04 AM
Hello,
I tough i have the some problem, but then I call "setName" method on FileUploadField and values are now successfully send to server. Test on Hosted mode, FF 3.5, Opera 10, Safari 4. Environment : OS X, Java 5, GWT 1.6.4, GXT 2.0.1

Regards,
Matic

Client code:

this.fPanel = new FormPanel();
this.fPanel.setHeaderVisible(false);
this.fPanel.setBodyBorder(false);
this.fPanel.setAction("./bridge");
this.fPanel.setEncoding(Encoding.MULTIPART);
this.fPanel.setMethod(Method.POST);
this.fPanel.setButtonAlign(HorizontalAlignment.LEFT);
this.fPanel.setWidth("100%");
this.fPanel.setHeight(80);
this.fPanel.setLayout(new VBoxLayout());

this.fPanel.addListener(Events.Submit, new Listener<FormEvent>() {
public void handleEvent(FormEvent arg0) {
status.setVisible(false);
importResultTA.setValue("Respond: " + arg0.getResultHtml());
}
});

hfType = new HiddenField<String>();
hfType.setName("TYPE");
hfType.setValue("UVOZ_PODATKOV");
this.fPanel.add(hfType);

fileUpload = new FileUploadField();
fileUpload.setAllowBlank(false);
fileUpload.setFieldLabel("Datoteka");
fileUpload.setWidth("100%");
fileUpload.setName("fupload");
this.fPanel.add(fileUpload);

Server code:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (!ServletFileUpload.isMultipartContent(request))
return;

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);

List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
return;
}

HashMap paramMap = new HashMap();
byte sendObjectB[] = null;
for (Iterator i = items.iterator(); i.hasNext();) {
FileItem item = (FileItem) i.next();

if (item.isFormField()) {
paramMap.put(item.getFieldName(), item.getString());
} else {
sendObjectB = item.get();
}
}

maksud
7 Sep 2009, 8:18 AM
Yes it works fine on my Hosted mode. When I selected content type of servlet to "application/octet-stream" it won't show. But I changed the content type to "text/html" and now it works. Don't know which magic did the trick.

lucasdeoliveira
8 Sep 2009, 11:52 AM
Hi,



(...)
fileUpload.setWidth("100%");
fileUpload.setName("fupload");
this.fPanel.add(fileUpload);


I tried the setName thing, but the damn event is not called. I also tried to compile it for ie and still no results.
I've tried setting the content type to "text/html", "application/octet-stream" and "text/plain" (one each time of course), but nothing happens so far. My server side code looks like this:



public class ServletFileHandler extends HttpServlet implements Servlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FileItem uploadItem = getFileItem(request);
byte[] fileContents = uploadItem.get();
String fileName = "testingFile";

// get application bean to save file:
RemoteCadAnexo aplCadAnexo = (RemoteCadAnexo)ProjetaApplicationContext.getInstance().getBean("aplCadAnexo");
aplCadAnexo.saveFile(fileName, fileContents);

response.setStatus(HttpServletResponse.SC_CREATED);
response.setContentType("text/html");
response.getWriter().print("hey ho, let's go!");
response.flushBuffer();
}

public FileItem getFileItem(HttpServletRequest request) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);

try {
List items = upload.parseRequest(request);
Iterator it = items.iterator();

while (it.hasNext()) {
FileItem item = (FileItem) it.next();

if (!item.isFormField()
&& "uploadFormElement".equals(item.getFieldName())) {
return item;
}
}
} catch (FileUploadException e) {
return null;
}
return null;
}
}
Do you guys see anything weird/wrong/stupid?

let's keep on going lads!
cheers, thanks a lot!

maku
16 Sep 2009, 7:07 AM
Hi,

I have the same problem. Is there really no solution to get the event??????

TIA

silvershadow
15 Oct 2009, 6:24 AM
I have the same problem :(

mtarantini
16 Oct 2009, 4:29 AM
Like others I have the same issues...

In fact it's working on IE8 / FF3.5...but not on WebKit Browser (Safari & Chrome).

The code of my servlet isn't called when using WebKit browsers.


Here My Form :



public class MibFileUploadWindow extends Window
{

private MibrowerMainView aMibrowerMainView;
private MibBrowserMessages messages = I18n.messages;
private FileUploadField aFileUploadField;
final FormPanel aFormPanel = new FormPanel();

public MibFileUploadWindow(MibrowerMainView aMibrowerMainView)
{
this.aMibrowerMainView = aMibrowerMainView;
this.setLayout(new FitLayout());
this.setWidth(350);
initGUI();
}

/***
* Initialize
*/
private void initGUI()
{
// Window's title
this.setHeading(messages.label_uploadFile());

LayoutContainer aLayoutContainer = new LayoutContainer(new FitLayout());
aLayoutContainer.setStyleAttribute("margin", "10px");

aFormPanel.setHeaderVisible(false);
aFormPanel.setFrame(true);
aFormPanel.setAction(GWT.getHostPageBaseURL() + "/FileUpload");
aFormPanel.setEncoding(Encoding.MULTIPART);
aFormPanel.setMethod(Method.POST);
aFormPanel.setButtonAlign(HorizontalAlignment.CENTER);
aFormPanel.setWidth(350);

aFileUploadField = new FileUploadField();

aFileUploadField.setName(messages.label_browse());
aFileUploadField.setAllowBlank(false);

aFileUploadField.setFieldLabel(messages.label_file());
aFormPanel.add(aFileUploadField);

Button btn = new Button(messages.label_send());
btn.addSelectionListener(new SelectionListener<ButtonEvent>()
{

@Override
public void componentSelected(ButtonEvent ce)
{
if (!aFormPanel.isValid())
{
return;
}
aFormPanel.submit();
hide();
MessageBox.info(messages.label_informations(), messages.label_file_upload_confirmation(), null);
aMibrowerMainView.getController().handleEvent(new AppEvent(MibBrowserEventTypes.PopulateTreeRequest));
}
});

aFormPanel.addButton(btn);
aLayoutContainer.add(aFormPanel);
this.add(aFormPanel);
}

@Override
public void show()
{
aFileUploadField.clear();
super.show();
}

}


My Servlet :


public class FileUploadServlet extends HttpServlet
{
private String MIB_FOLDER = "mibs";
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

try
{
processRequest(req, resp);
}
catch (FileUploadException e)
{
CLogger.debug(e.getMessage());
}
catch (ServletException e){
CLogger.debug(e.getMessage());
}
catch(IOException e){
CLogger.debug(e.getMessage());
}
catch (Exception e)
{
CLogger.debug(e.getMessage());
}


}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, FileUploadException, Exception {
// Check that we have a file upload request
if (ServletFileUpload.isMultipartContent(request)){
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory(4096,new File(MIB_FOLDER));

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
List<FileItem> items = upload.parseRequest(request);

// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
String fileName = item.getName();
// clean File Name
if(fileName.contains("/")){
int lastIndexOfSlash = fileName.lastIndexOf("/");
fileName = fileName.substring(lastIndexOfSlash, fileName.length());
}
if(fileName.contains("\\")){
int lastIndexOfAntiSlash = fileName.lastIndexOf("\\");
fileName = fileName.substring(lastIndexOfAntiSlash, fileName.length());
}
if(!new File(MIB_FOLDER).exists()){
new File(MIB_FOLDER).mkdirs();
}
if(! new File(MIB_FOLDER +"/"+ fileName).exists()){
File uploadedFile = new File(MIB_FOLDER + "/" + fileName);
item.write(uploadedFile);
}
}
}
}
}

The web.xml




<servlet>
<servlet-name>FileUpload</servlet-name>
<servlet-class>
web.app.mibbrowser.server.FileUploadServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>FileUpload</servlet-name>
<url-pattern>/myApp/FileUpload
</url-pattern>
</servlet-mapping>



I've noticed that the HTML code of the Button is <button ...>, normally should'nt be something like <input type="submit" ... />


Thx you for your help.

navi75
10 Dec 2009, 5:18 PM
I have tested with the new GXT 2.1.0 release and still the problem is same.

The submit listener is never called in IE7.0

Can GXT team provide any information on the same as when can we get the fix for the FileUpload Submit listener problem.

Thanks

navi75
22 Jan 2010, 6:23 PM
GXT development team do we have any solution for the file upload submit listener not being called.

Thanks.

lucasdeoliveira
23 Jan 2010, 5:12 AM
Hi,
I spent a huge amount of time trying to make it work and unfortunately I couldn't so I switched my file upload engine to gwtupload (http://code.google.com/p/gwtupload/).

cheers!

wildlettuce
12 Jul 2010, 9:29 PM
A workaround isnt that hard.

All you need to do is find the iframe that holds the upload response, and get its content.

Assuming that the response is XML, you can do the following:


FormPanel panel = new FormPanel();
.
.
.
panel.addListener(Events.Submit, new Listener<FormEvent>() {
public void handleEvent(FormEvent be) {
com.google.gwt.dom.client.Element e = getUploadResponse(panel.getTarget());
}
}
.
.
// JSNI native method to get the content
private native com.google.gwt.dom.client.Element getUploadResponse(String target)/*-{
return $wnd.document.getElementsByName(target)[0].contentWindow.document.activeElement;
}-*/;


Good luck

judoOlle
27 Jul 2010, 5:41 AM
Has there been any official solution to this problem yet? I'm having big problems firing the Events.Submit-event... :(

sven
27 Jul 2010, 5:43 AM
I dont have any problems with it.
Can you please post some fully working testcase that imlpements EntryPoint and also shows the serverside code?

Best would be an ready eclipse project i just have to import.

judoOlle
27 Jul 2010, 5:58 AM
Thanks for the quick reply.

Unfortunately I do now have a sample testcase, at the moment.

Just so I understand this event correctly: is it supposed to always fire as long as the request has reached the servlet? Or are you required to configure the response of the servlet in any way?

This is some of my code, if it should be of interest!

Client Side:

//This should be fired when/if the form is successfully submitted
panel.addListener(Events.Submit, new Listener<FormEvent>() {
public void handleEvent(FormEvent fe) {
Window.alert("The response " + fe.toString());
}
});

com.extjs.gxt.ui.client.widget.button.Button submitButton = new com.extjs.gxt.ui.client.widget.button.Button(
"Submit");
submitButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
if (!panel.isValid()) {
return;
}
// handle the post
if (fileField.getValue().endsWith("xml")) {
panel.submit();
fileUploadWindow.hide();
} else {
Window.alert("Not a valid file");
}
}
});

Right now this is all my Server side code:



protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html");
response.getWriter().print("OK!");
//doGet(request, response);


Many thanks!

sven
27 Jul 2010, 6:22 AM
fileUploadWindow.hide();

remove that and hide it first in the submit event.

judoOlle
27 Jul 2010, 6:30 AM
YES! It worked!
I am forever grateful! :)

kosikt
13 Jan 2011, 8:26 AM
Thank you! I was doing the same thing.