How to stay in same page when executing a Server request in a portlet - portlet

I am calling a webpage from a portlet. The webpage is a form for user to enter data and has a button which submits the user data into Database.
But the Button also redirects the portlet site to the webpage through the proxy Gateway.
How to stay in the same portlet page while having the Form data submitted to the database?

You could use AJAX

Just adding to the AJAX answer..
A standard JSR 286 portlet supports such asynchronous action through serveResource method in the portlet class, which you'll need to override.
In the java file,
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
//Write or invoke your database code here...
}
Also, in the html <form> tag you will need to set <portlet:resourceURL> in the action attribute.
Hope this helps, even though I am assuming by portlet you mean Java portlets and not something else

Related

how to redirect/map to externalregistration page from AuthenticateExternalAsync to angular page

I am using external providers to login to my web app. (for example Google). In my custom userservice I get to AuthenticateExternalAsync and from there I want (if need to) redirect to Angular page.
public override Task AuthenticateExternalAsync(ExternalAuthenticationContext context)
{
...
...
context.AuthenticateResult = new AuthenticateResult("~/externalregistration", user.Subject, name, identityProvider: user.Provider);
return Task.FromResult(0);
}
i have html page
at https://localhost:44300/Content/app/externalregistration.html
How do I map externalregistration to this page?
At the moment I get an error
https://localhost:44300/identity/externalregistration#
HTTP Error 404.0 - Not Found
thank you
Mark
The page for the partial login has to be with IdentityServer - see that it's looking for it at /identity/ and not /Content/app/.
If from your user service you issue a partial login, then that web page is entirely up to you to serve up from the server. If that partial login page needs to know the identity of the user, then it needs to be hosted in the same path as IdentityServer so the partial login cookie can be read on the server. If you then want that page to be a SPA, then you'd have to have some server side code issue something into the browser for your SPA to know the identity of the user. If you want that page to be a SPA and make Ajax calls back to the server, you need to include some XSRF protection.
All in all, custom partial pages are easiest implemented as standard server-rendered MVC pages.

How can I reload my page to redirect to an URI Fragment in VAADIN?

In my Vaadin webapp I have a tipical architecture with login. In some cases, the user can access directly to some resources using Vaadin URI Fragments (http://example.com/#fragment).
When a user tryes to access some resource, If the user has logged in, I take from the URL the #FRAGMENT and I bring him to it.
But if the user has no logged in, when he logs in I used to bring him to the main page using
getPage().open("/", "_self");
but since if I add an URI Fragment, the getPage().open(...) does not work.
Is there any way to redirect the user to a correct URL (URL with UriFragment in my case) from code?
Note that there is a fundamental difference in how navigation is handled in traditional web applications versus single-page applications as implemented with Vaadin. In traditional web applications you navigate through the app by making full HTTP GET-Requests on some path (such as www.example.com/myapp/home). On each such request, a full page reload is performed. You can't do that with Vaadin, as a full page reload means reloading the Vaadin widget set and rebuilding the page layout from the ground up. Therefore, single-page applications typically use the URI fragment for navigation purposes. Changes to this fragment are solely handled by the client-side JavaScript code. There will be no GET-Request induced by the browser when the URI fragment is changed.
That's why the approach you described doesn't work for you. Using Page.open(...) will open a web page through a HTTP GET-Request resulting in a complete reload of your Vaadin application.
The solution for your problem is to solely handle all navigation (including state-dependent redirects) through the Page object's URI fragment handling methods (or through the [Navigator][1] component). Redirecting in Vaadin can be achieved by programmatically setting the URI fragment with Page#setUriFragment() or Navigator#navigateTo() and having your URI handling code (or Navigator) take care of the rest. Only then it is assured that your users stay on the same page even when they are redirected to a login form or to some other place after logging in.
I would like to add to Roland's answer and share how I solved this.
My UI:
#Override
protected void init(VaadinRequest request) {
setSizeFull();
setContent(masterView);
getPage().addUriFragmentChangedListener(event -> present(event.getUriFragment()));
present(getPage().getUriFragment());
}
The masterView is just a CustomComponent that has a content section. When the menu is clicked, I simply setContent to the masterView's content section. Swapping out the middle, basically.
present method:
private void present(String fragment) {
masterView.setContent(getComponentFromFragment(fragment));
}
Finally:
private Component getComponentFromFragment(String fragment) {
if (fragment.equals(someOtherView.NAME))
return someOtherView;
return null; // null clears it out as in the welcome page
}
The important part is the present in the init. When the UI renders for the first time and fires the init, it goes ahead and grabs whatever the URI fragment is in the browser and presents that as well.
Works great.
Maybe this can work:
UI.getCurrent().getPage().executeJavaScript("window.location.href = 'http://google.com'");

How can I avoid that my URL changes when I upload a file using servlets in GWT?

I have the option to upload a file in my webapp, and I already have it done, but there is a problem: the browser redirects me to "base_url" + the url-pattern defined in the web.xml file after the upload is done. I know that should be normal because I'm defining it in the <url-pattern> tag. However, I want that my webapp stays in the current url, how can I achieve this?
<servlet-name>uploadServlet</servlet-name>
<servlet-class>com.premium.server.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>uploadServlet</servlet-name>
<url-pattern>/Project/upload</url-pattern>
</servlet-mapping>
Note: I've already tried this <url-pattern>/</url-pattern> and this <url-pattern>/*</url-pattern> but didn't work.
private static final String UPLOAD_ACTION_URL = GWT.getModuleBaseURL() + "upload";
public void onModuleLoad() {
final DynamicForm form = new DynamicForm();
form.setAction(UPLOAD_ACTION_URL);
form.setEncoding(Encoding.MULTIPART);
form.setMethod(FormMethod.POST);
SubmitItem submit = new SubmitItem("a", "Submit");
submit.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
form.submitForm();
}
});
UploadItem upload = new UploadItem();
upload.setName("uploadFormElement");
form.setFields(upload, submit);
VLayout panel = new VLayout();
panel.addMember(form);
RootPanel.get().add(form);
According to SmartGWT's documentation, this is the expected behavior:
Upload without the Smart GWT Server
If it is acceptable that the application will do a full-page reload after the upload completes, you can simply:
set encoding to "multipart"
include an UploadItem to get a basic HTML upload control
set action to a URL where you have deployed server-side code to handle the upload
call DynamicForm.submitForm to cause the form to be submitted
This cause the DynamicForm component to submit to the form.action URL like an ordinary HTML element. Many online tutorials are available which explain how to handle HTML form file upload in various server-side technologies.
Note that when you submitForm(), the only values that will be sent to your actionURL are values for which actual FormItems exist. This differs from saveData(), in which the entire set of form values are always sent. To handle submitting extra values, use HiddenItems.
For further details, see the UploadItem docs.
— Source: http://www.smartclient.com/smartgwtee-latest/javadoc/com/smartgwt/client/docs/Upload.html
What you're probably looking for is what they call background upload:
Background upload without the Smart GWT Server
Achieving background file upload without using the Smart GWT server is also possible although considerably more advanced. In addition to the steps above, create a hidden <iframe> element in the page, and use target to target the form submission at this IFRAME. In order receive a callback notification when the upload completes, after processing the file upload, your server should output HTML content for the IFRAME that includes a <SCRIPT> block which will navigate out of the IFRAME (generally via the JavaScript global "top") and call a global method you have declared as a callback.
— Source: http://www.smartclient.com/smartgwtee-latest/javadoc/com/smartgwt/client/docs/Upload.html
Note: the FormPanel widget from GWT proper defaults to background upload.

Redirecting to second page in GWT doesn't load GWT components in second page

i am using GWT app engine to deploy my application in local host.
i want to redirect to second page when user completed his registration & clicked "submit" button, the browser has to redirect to automatically to his Profile page with his registration details.
i used fallowing code to redirect to second page from first page;
String url = GWT.getHostPageBaseURL()+"/UserViewProfile.html";
Window.Location.replace(url);
in my case the first page is URL is like:
http://127.0.0.1:8888/UserRegistration.html?gwt.codesvr=127.0.0.1:9997
when i submitted on "Submit" button it is edirecting to URL like:
http://127.0.0.1:8888/UserViewProfile.html
In second page(UserViewProfile.html) i developed simple HTML content & simple Textbox widget to check it's functionality. But i am seeing HTML content only but not "Textbox".
To see text box i has to type URL like:
http://127.0.0.1:8888/UserViewProfile.html?gwt.codesvr=127.0.0.1:9997
how i can access last part "?gwt.codesvr=127.0.0.1:9997" at end of my URL pattern automatically? if i add it manually, at the time of hosting it may leads to problem. please if any body give solution, that would be great.
I do not understand the use case. Anyway I guess you need to conditionally check if you are in DevMode or ProdMode, and add the gwt.codesvr=127.0.0.1:9997 query string accordingly. Something like:
String url = GWT.getHostPageBaseURL()+ "/UserViewProfile.html";
if (GWT.isProdMode()) {
Window.Location.replace(url);
} else {
Window.Location.replace(url + "?gwt.codesvr=127.0.0.1:9997");
}
The gwt.codesvr=127.0.0.1:9997 query string parameter is used by GWT to (simplifying) bootstrap your app in the so called Development Mode, instead of the Production Mode (the actual compiled version of your application). Without this check, if you are in DevMode, you end up requesting the UserViewProfile.html that looks for the compiled version of your app (that does not show anything, if you've never compiled it, or if you have simply recently clean the project).
Do also note that URL rewriting (by not simply changing the # fragment identifier), means application reloading.

Create servlet in Wicket to pass input as a parameter on submit to the web page

I'm new to Wicket and have made an application using some tutorials.
How do I create a servlet and pass on the input from that to a web page without database transactions?
I'm assuming you are trying redirect to a Wicket page (parameterized) from outside the Wicket application.
You can do this by using one of the URL encoding strategies (BookmarkablePageRequestTargetUrlCodingStrategy, QueryStringUrlCodingStrategy, ...) which will give that page a clean URL which can be referenced easily. Or you can put a BookmarkablePageLink in your web application somewhere, copy the link that it generates and use it to redirect to.
You can reference that link anywhere you wish passing parameters to it using the normal ?par1=val&par2=val system.
To read these parameters in your page you will need to define your page constructor like so:
...
public MyPage(final PageParameters parameters) {
final String par1 = parameters.getString("par1");
final String par2 = parameters.getString("par2");
}
...