how to make error page (http 500) work in IceFaces? - java

Using Icefaces 2, if an error occurs during execution of an action method on a standard (=not icefaces) h:commandButton, it just seems the button has no action. No error page is displayed, although it is configured to do so in web.xml.
I can make it work by surrounding the tag with
<f:ajax disabled="true">...</f:ajax>
But I'd want to either disable this automatic ajax from Icefaces (see question How to disable unsollicited ajax on standard components (h:commandButton) while using Icefaces? ), or make the error page work anyway.
JSF implementation is Mojarra 2.1 which comes with Glassfish 3.1.

The basic problem is that Icefaces captures the submit button and puts ajax in it. I think this is simply bad behavior: I understand that something like that could happen in a ice:commandButton or even under ice:form, but it happens to h:commandButton to h:form as well. This can be disabled, as per http://wiki.icefaces.org/display/ICE/Configuration , by setting autorender context parameter to false in web.xml . But then, you need to explicitely enable this behavior on every icefaces form (you get an error otherwise).
Anyway, as stated here: http://wiki.icefaces.org/display/ICE/Handling+Exceptions, putting this script in the page basically solves the problem:
//Assign your error handling function to a variable
var iceErrorCallback = function iceHandleError(statusCode, responseTxt, responseDOM) {
//Handle all errors by simply redirecting to an error page
window.location.href = "./generalError.xhtml";
}
//Safely check if ICEfaces is available
if (ice) {
//Turn off the popups as we plan to handle this ourselves
ice.configuration.disableDefaultIndicators = true;
//Register your error handler as a callback
ice.onServerError(iceErrorCallback);
}
Update: I had to patch some Icefaces javascript for it to work, see http://jira.icefaces.org/browse/ICE-6546 . I know realize normal Icefaces behavior is displaying a popup with the error, which didn't happen.

Related

Spring controller method called twice

I'm making a Spring MVC web app.
The problem is that on single method is called twice and I don't know why.
#RequestMapping(value="/profile/{id}", method = RequestMethod.GET)
public String displayUserProfile( #PathVariable String id) {
System.out.println("asdasddsasd");
return "account/userProfile";
}
I commented many line from this method, but is still not working. Also tried to return other view..no good luck.
In console(ulr requests are written):
/demo/account/profile/f91b3a38-6921-41e0-98b7-58dff5cb1152
asdasddsasd
/demo/account/profile/0
asdasddsasd
After the second call of tihs method, it's going to my view
Any other method work fine.
Does anyone know what's the problem here?
*I also read similar question from here..nothing helped
LE: what I also said in the comments.
What is funny is that, if I set o model to the view, on the second call of the method, my view get's the model from the first call. (on the second call, with id 0, the model is null)
I have also observed one GET request causing the controller method to execute twice. The problem occurred when requesting the service using a Chrome browser (the problem did not occur when using Postman). In my case the culprit was the JSONView Chrome extension.
I determined the cause by using the Network tab of the Chrome developer tools. It showed my GET service being requested two times. The second request was initiated by content.js, which is a JavaScript file bundled with JSONView.
After I disabled the JSONView extension, a GET request through Chrome would cause the controller method to execute only once.
I experienced this called-twice phenomenon because BrowserSync replayed HTTP requests in each open BrowserSync browser window.
I finally got some time to find a solution here.
Tried many things, but it didn't worked.
I replaced #PathVariable with #RequestParam and the URL is not accessed twice :)
Had the same problem.
Eventually I found that I have a null in a background-image url like so:
style="background-image: url(null);"
that caused to send another GET with a path variable null.
I have the same problem and find a lot of solution and finally I found the simple reason. It's in my html template css: background:url() .
This cold will run the same url again. So I just remove it out or put url in the bracket and it works.
Sounds like an issue on client side.
Open up your browser, enter <host/port/whatever_you_need_to access_the_app>/demo/account/profile/f91b3a38-6921-41e0-98b7-58dff5cb1152
and check the logs. The chances are that you'll see only one entry
Now run your client code and check network requests to the service. If you're call the controller from the browser like Chrome F12->Network tab should help.
I know it's a kind of obvious, but I think there is nothing really "unusual" in this controller, so it should be more at the level of general flow. In this case maybe it's the best to trace the HTTP traffic and see how many/when/how does it generate requests to your controller.
This might also occur due to one more reason. Because i found samething and observed following.
1st time its when your request processed and you see println statement in Console.
And if you refresh browser at method request of controller method ( example http://localhost:8080/DemoMVC/add?***) each refresh your tomcat processes request again and you get same println statement in console.
Perhaps this is too late. However, I still face these issues and forget the solution every time.
In case you are using any JS library like Angular or React then in your service call observe the response as well.
Here is a code snippet
return this.http.get<User>(`${this.resourceUrl}/activate`, { params: options, observe: 'response' })
.pipe(
filter((response: HttpResponse<User>) => response.ok),
map((response: HttpResponse<User>) => response.body),
catchError(error => {
return of(error)
})
);
The key area to focus is { params: options, observe: 'response' }
I had a controller which was listening to localhost/ and a get method which matched on a path variable something like this:
#GetMapping("/{foo}")
String get(#PathVariable(required = false) String foo)
{
return "hello world";
}
And the problem was, that after calling localhost/ I got the first call, and after that, I got the second call for the favicon.
The same would be true if you would define a context root.

Portlet ActionRequest generation with Web Experience Factory 7.0.1

I am having strange issues with ActionRequest generation for portlet. I use WebSphere portal link builder in my source portlet
It has an option of specifying whether the URL it generates will be a action/render url.
action
Portlet action processing
render
Portlet render processing
Even though I specify "action" as url generation type it always generates a Render url.
In my target portlet i write below code
if (webAppAccess.getHttpServletRequest().
getAttribute(Constants.PORTLET_REQUEST) instanceof ActionRequest) {
System.out.println("Its an action request ");
} else if (webAppAccess.getHttpServletRequest().
getAttribute(Constants.PORTLET_REQUEST) instanceof RenderRequest) {
System.out.println("Its a Render request ");
}
Its always going to the else if block. I am not sure why.
Can any one suggest any ideas?
Thanks,
Arajit
Arajit
Where are you placing the above code? (eg, in the page, in main, in an explicitly called action?). If it's just in a method called on page render, then you would expect a render request. It will only be an ActionRequest if it's an explicitly called action, not using AJAX for partial page rendering.
What is the use case? There may be an alternative to the Portal Link builder (eg, such as JSR286 events via the Cooperative Portlet builders).
You may find more experts on Web Experience Factory and questions/answers on the #WEF forum at:
https://www.ibm.com/developerworks/community/forums/html/forum?id=11111111-0000-0000-0000-000000000889
I hope that info helps,
..Mike Burati
http://www-10.lotus.com/ldd/pfwiki.nsf/
The postings on this site are my own and do not necessarily represent the positions, strategies, or opinions of IBM.

Force page expiration in wicket 1.5.8

I'm trying to expire all previous versions of a given page with Wicket 1.5.8. In wicket 1.4, it was done by getPage().getPageMap().clear(). Now in wicket 1.5, page map is gone, and I can't figure out how to dot that.
My use case is that I have a wizard (http://www.wicket-library.com/wicket-examples/wizard/) to create/edit an entity. When the wizard is submitted, the user is redirected to the entities list. At this point I don't want the user to be able to use the browser back button to go back to the wizard in the state it was, and thus want to expire previous versions of the page with the wizard (I am using getPageSettings().setRecreateMountedPagesAfterExpiry(true); so the mounted page will be recreated in a blank state if the page expires when the user goes back, which is what I want).
Looking around I found the possibility to use Session.get().clear(); which removes all pages from the Session (I don't know if the 1.4 version was removing all pages or just all versions of the page used to access the PageMap - which would be better for multi-tab support). However, using that works only partly, as the last page is not expired.
Supposing the wizard is mounted at /wizard, redirecting to /list at the end, the flow would be something like: /wizard?1, /wizard?2, /wizard?3, /list. Now when I use the back button, /wizard?3 is not expired, although /wizard?1 and /wizard?2 are as expected. The session clearing and sending to the list page are done in the onFinish method of the wizard, which reads like this:
#Override
public void onFinish() {
Session.get().clear();
Session.get().getFeedbackMessages().add(new FeedbackMessage(..));
setResponsePage(ListPage.class);
}
So, come to the question itself: would anyone know how to get the expected behaviour, i.e. expiring /wizard?3 as well?
Thanks
Note: ListPage is a bookmarkable page and I tried as well with setResponsePage(new ListPage());
Update with what I finally did base on Andrea's suggestion
It is to be noted that my application uses a custom session object extending wicket's Session; let's call it AppSession.
in AppSession, I added a boolean clearRequested attribute (defaults to false)
in AppSession, I added a static void method requestClear() which is just a shortcut to set clearRequested to true in the session
in the onFinish() of my wizard, just before calling setResponsePage, I call AppSession.requestClear()
lastly I just add a RequestCycleListener to my application.
getRequestCycleListeners.add(new AbstractRequestCycleListener() {
#Override
public void onBeginRequest(RequestCycle cycle) {
super.onBeginRequest(cycle);
AppSession session = AppSesssion.get();
if (session.isClearRequested()) {
session.clear();
session.setClearRequested(false);
}
}
});
Now anytime I need to clear session after an action in order to expire pages, I juste call AppSession.requestClear() and in the next request the session is cleared.
you could clear your session in ListPage. Since this page is bookmarkable you could pass it a page parameter indicating that ListPage must get rid of the pages in session.

Redirect to Mobile pages in Struts

I have an existing web app which uses Struts for the forward-action...
I am trying to create a mobile version (mainly the UI design will be different) of this existing web app.
Now this app is using Struts 2 for the forward-action thing..
My question is can I extend this Struts XML to redirect based on desktop/mobile..
e.g. Let's say I have desktop.jsp and mobile.jsp...Now I detect where the user is coming from at a server level and have that info in the session..Can I update my stuts-config XML such that the only thing I need to change would be the "forward" JSP URL based on where the user is coming from?
I am looking at no change to the Action...only the forward URL for the JSP.
Please note that this question is not about "how to detect", but more about what approach to take once we have detected the browser and have that info from the server..
Thank you.
Better option is always to add a Filter here . and rest how to detect you know it.
Track all the request check for the header to find out if its coming from mobile ? and serve request according to it.
you can forward the request to another action which will handle the mobile specific data to load and maybe to have different results. One result pointing to desktop.jsp other pointing to the mobile.jsp.
sample:
public String execute(){
if(isMobile())
return "mobile";
else return "desktop";
}

Does NavigationHandler.handleNavigation() clear the flash?

I am using JSF2.0 Mojarra 2.0.2.
I have a method that logs out a user and puts a logout message in the flash, forwards to the login page (which has a div that prints out the flash). However, when I use navigationHandlers handleNavigation method for some reason the flash is not being displayed. I have a similar method that forwards a user to the login page if he/she isn't logged in.
If I handle the navigation through an h:link and just call the logout method directly, the flash is displayed as normal, but if I use the handleNavigation() method, the flash is cleared for some reason.
The code in question is:
public void performLogout()
{
getFacesContext().getExternalContext().invalidateSession();
setCurrentUser(null);
getFlash().put("notice", "Successfully logged out.");
super.getFacesContext().getApplication().getNavigationHandler()
.handleNavigation(getFacesContext(), null, "login");
}
Is there some way I can keep the flash when navigating like this?
thanks.
Edit: I believe this issue is related to another issue involving the flash not being preserved during redirects when xhtml pages are in different directories: http://java.net/jira/browse/JAVASERVERFACES-1635
You're right.
The JSF 2 flash scope is currently extremly buggy, and based on the specification, will probably remain almost unusable: http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/index.html
If you'd like a flash scope that works as follows, consider using CDI, and the flash scope from Seam Faces - http://docs.jboss.org/seam/3/faces/reference/snapshot/en-US/html_single/#flashscoped
"The flash scope should be active from
the moment an object is placed in the
scope, until the moment the response
has completed rendering."
--Lincoln

Categories