I´m trying to catch the next two highlighted fields in a servlet where I can get the uploaded file.
The source code is just the same as which is shown in a GWT FormSubmit class Javadoc
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
// Create a panel to hold all of the form widgets.
VerticalPanel panel = new VerticalPanel();
form.setWidget(panel);
// Create a TextBox, giving it a name so that it will be submitted.
final TextBox tb = new TextBox();
tb.setName("WorkTitle");
tb.setValue("WorkTitle");
panel.add(tb);
// Create a ListBox, giving it a name and some values to be associated
// with
// its options.
ListBox lb = new ListBox();
lb.setName("listBoxFormElement");
lb.addItem("foo", "fooValue");
lb.addItem("bar", "barValue");
lb.addItem("baz", "bazValue");
panel.add(lb);
// Create a FileUpload widget.
FileUpload upload = new FileUpload();
upload.setName("uploadFormElement");
panel.add(upload);
// Add a 'submit' button.
panel.add(new Button("Submit", new ClickListener() {
public void onClick(Widget sender) {
form.setAction(GWT.getModuleBaseURL()+"uploadWork");
form.submit();
}
}));
I´m getting these parameters with this code lines in my servlet:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("ENTRAA BIENNNN");
System.out.println(" ___ELEMENTO1" + req.getAttribute("WorkTitle"));
System.out.println(" ___ELEMENTO3" + req.getParameterValues("WorkTitle"));
But both returns me NULL.
How could I do?
TIA!
Most servlet containers do not decode multipart/form-data automatically, so req.getParameter (or getParameterValues or similar getters) won't return anything.
You'll have to use a library such as Apache Commons FileUpload, or Jetty's MultiPartFilter to decode multipart/form-data payload.
As a side note, req.getAttribute has nothing to do with getting data out of the request; it's used to pass data, related to a request, between server components (between the servlet container and servlets, or between a filter and a servlet, for instance)
Related
I have a smartGwt DynamicForm with a FormItem
FormItem item = createTextItem();
form.setFields(item);
After creating the and setting fields, I need to dynamically set an editor type for the item. I have to do it dynamically based on some conditions.
I'm calling item.setEditorType(new PasswordItem());
just after I call form.editRecord(record); so that the new editor type should appear. But it is not working.
Tried calling item.redraw() and is not working.
My goal is to set the editor type dynamically based on the record that is edited.Please help.
Try with Custom Data Binding (see page 23 for more details). What you tried won't work, AFAIK, because the ListGridField has already been created with the initial custom editor, and it can't be changed dynamically with setEditorCustomizer.
Take a look at this sample (based on this showcase demo), which does what you want to do to the password field when it is being edited in the DynamicForm, and after the changes have been saved (please pay attention to the comments, as without some of these settings it won't work as expected):
public void onModuleLoad() {
final DataSource dataSource = ItemSupplyLocalDS.getInstance();
final DynamicForm form = new DynamicForm();
form.setIsGroup(true);
form.setNumCols(4);
form.setDataSource(dataSource);
// very important for not having to set all fields all over again
// when the target field is customized
form.setUseAllDataSourceFields(true);
final ListGrid listGrid = new ListGrid();
listGrid.setWidth100();
listGrid.setHeight(200);
listGrid.setDataSource(dataSource);
listGrid.setAutoFetchData(true);
IButton editButton = new IButton("Edit");
editButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
form.editRecord(listGrid.getSelectedRecord());
// when the button is clicked, the password field is rendered with
// a plain text item editor, for easy verification of values entered
FormItem passwordField = new FormItem("passwordFieldName");
passwordField.setEditorProperties(new TextItem());
form.setFields(passwordField);
form.markForRedraw();
}
});
IButton saveButton = new IButton("Save");
saveButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
form.saveData();
// when the button is clicked, the password field is rendered with
// a password editor, for added privacy/security
FormItem passwordField = new FormItem("passwordFieldName");
passwordField.setEditorProperties(new PasswordItem());
form.setFields(passwordField);
form.markForRedraw();
}
});
VLayout layout = new VLayout(15);
layout.setWidth100();
layout.setHeight100();
layout.addMember(listGrid);
layout.addMember(editButton);
layout.addMember(form);
layout.addMember(saveButton);
layout.draw();
}
I am sending request on server, and server returns StreamingOutput (wriring bytes of pdf file)
In onResponseRecieved i need to handle this file and start download. How to start to download this file?
I supose you dont need to process that data in javascript, do you?
If you just want to download the file or display it in the browser, create a button in your page so as when the user clicks it will show in a new window or the user will asked to save the file:
final String url = "http://gwtquery.googlecode.com/git/README.txt";
String name = "README.txt";
Anchor link1 = new Anchor(name);
RootPanel.get().add(link1);
link1.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Window.open(url, "_blank", "");
}
});
I have a FormPanel which sends a POST Request to a server with:
form.setAction("http://servlet_address")
The problem is, after firing submit(), the formpanel redirects the page to "http://servlet_address" (where the request was sent), but this action is not desired, as the servlet just receives data. How can I avoid this redirection behaviour?
It is a standard HTML form behavior. When you submit the form you actually send form data to server using HTTP request. "Method" attribute allows you to define where to encode form data (URL or HTTP request body). And result of form submitting (HTTP request) will be displayed in your browser.
As I understand you want to override this behavior. There are several ways to do it. All of them requires you to process form data manually and generate correct request. It means that you can not submit your form. There are 2 common patterns for GWT to communicate with server:
GWT-RPC ( https://developers.google.com/web-toolkit/doc/latest/tutorial/RPC )
Request Factory ( https://developers.google.com/web-toolkit/doc/latest/DevGuideRequestFactory )
Both of them are easy to use if you have Java Server back-end. If you need to submit your data to non-java we-server than Request Factory only can be used and it is complicated task.
See the following code it works fine without redirecting (this code work properly in an onModuleLoad() method):
final FormPanel formPanel = new FormPanel();
formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
formPanel.setMethod(FormPanel.METHOD_POST);
VerticalPanel verticalPanel = new VerticalPanel();
verticalPanel.add(new Label("Username"));
TextBox userid = new TextBox();
userid.setName("username");
verticalPanel.add(userid);
verticalPanel.add(new Label("Password"));
PasswordTextBox passwd = new PasswordTextBox();
passwd.setName("pass");
verticalPanel.add(passwd);
verticalPanel.add(new Button("Submit", new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
formPanel.submit();
}
}));
formPanel.add(verticalPanel);
formPanel.setAction("submit"); // e.g. servlet URL
// Add an event handler to the form.
formPanel.addSubmitHandler(new FormPanel.SubmitHandler() {
public void onSubmit(SubmitEvent event) {
// This event is fired just before the form is submitted. We can
// take
}
});
formPanel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
public void onSubmitComplete(SubmitCompleteEvent event) {
// When the form submission is successfully completed,
// this event is fired.
Window.alert(event.getResults());
}
});
RootPanel.get().add(formPanel);
Have a nice time.
I have a servlet that delivers png and svg images. With png i have no problem:
Image image = new Image(GWT.getHostPageBaseURL() + token);
But how to get svg to work? I already added "lib-gwt-svg" to my dependencies. There is a SVGImage class:
SVGImage svg = new SVGImage(OMSVGParser.parse(???));
The parse takes a string. Is there a way to load a raw String from an URL?
Or how to get it to work (with or without "lib-gwt-svg")?
Update:
thx to Andrei Volgin: he pointed out that it should work with "Image" and it does (i just had to correct the mime type to "image/svg+xml"). But the scripts within svg-image don't work this way (it looks like the images is rendered as a normal bitmap image).
I need the image rendered as svg (with scripts).
If you use a URL to load an image, you don't need any libraries at all. And you don't need a servlet to deliver them. Just add images to your /war/images folder. Then, in your GWT code:
Image image = new Image();
image.setUrl("images/myImage.svg");
myPanel.add(image);
You may want to add some logic for browsers that do not support svg files.
I found a solution that works without any external library (like Andrei's Solution) but keeps also the embedded scripts working. I used the info from here - i used a HTMLPanel and loaded the image via "RequestBuilder":
String url = GWT.getHostPageBaseURL() + link.getToken();
RequestBuilder rB = new RequestBuilder(RequestBuilder.GET, url);
rB.setCallback(new RequestCallback() {
#Override
public void onResponseReceived(Request request, Response response) {
//create Widget
chartImage = new HTMLPanel(response.getText());
//add to layout
layout.add(chartImage);
}
#Override
public void onError(Request request, Throwable exception) {
// TODO Auto-generated method stub
}
});
I want to upload file in my application and want to set path where the files should be saved after uploading in my local system.I am using the following code but on the submit button getting no response while clicking.Please tell me the code which works fine for the file upload in gwt.
[code]
public class FormPanelExample implements EntryPoint {
public void onModuleLoad() {
// Create a FormPanel and point it at a service.
final FormPanel form = new FormPanel();
form.setAction("/myFormHandler");
// Because we're going to add a FileUpload widget, we'll need to set the
// form to use the POST method, and multipart MIME encoding.
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
// Create a panel to hold all of the form widgets.
VerticalPanel panel = new VerticalPanel();
form.setWidget(panel);
// Create a TextBox, giving it a name so that it will be submitted.
final TextBox tb = new TextBox();
tb.setName("textBoxFormElement");
panel.add(tb);
// Create a ListBox, giving it a name and some values to be associated with
// its options.
ListBox lb = new ListBox();
lb.setName("listBoxFormElement");
lb.addItem("foo", "fooValue");
lb.addItem("bar", "barValue");
lb.addItem("baz", "bazValue");
panel.add(lb);
// Create a FileUpload widget.
FileUpload upload = new FileUpload();
upload.setName("uploadFormElement");
panel.add(upload);
// Add a 'submit' button.
panel.add(new Button("Submit", new ClickListener() {
public void onClick(Widget sender) {
form.submit();
}
}));
// Add an event handler to the form.
form.addFormHandler(new FormHandler() {
public void onSubmit(FormSubmitEvent event) {
// This event is fired just before the form is submitted. We can take
// this opportunity to perform validation.
if (tb.getText().length() == 0) {
Window.alert("The text box must not be empty");
event.setCancelled(true);
}
}
public void onSubmitComplete(FormSubmitCompleteEvent event) {
// When the form submission is successfully completed, this event is
// fired. Assuming the service returned a response of type text/html,
// we can get the result text here (see the FormPanel documentation for
// further explanation).
Window.alert(event.getResults());
}
});
RootPanel.get().add(form);
}
}
Thanks
Amandeep
Now, I remember.. There's a bug in the FormPanel code that causes form.submit() not to work, when the type of the form is changed from the default (don't know if it's fixed yet in any release of GWT). If you create a "native" submit button like this:
HTML nativeSubmitButton new HTML("<input class='gwt-Button' type='submit' value='" + buttonText + "' />")
It will submit the form.
The disadvantage is that you cannot use any Button methods on this object, since it's a simple HTML wrapper. So disabling the button on submit (to avoid accidental double submit, and to give feedback that the form is actually submitting) won't work .
I've created a utility class for this purpose myself called DisableableSubmitButton that is essentially a FlowPanel with one HTML button like the above, and one gwt Button that is disabled, and some logic to toggle each of them visible. Since it cannot modify the actual enabled status of the HTML button all submit handlers must ask this class if it's "enabled" or not and cancel the event if it is. If you're interested in this implementation, i could share it with you (I don't want to flood stackoverflow with code unless you are interested).