create public url to view in portlet - jsf-2

I have simple primefaces portlet, which is connected to the database. I have view (index.xhtml) with the table, which presents all my entries from db (I'm using p:dataTable). I have also a simple view (someView.xhtml), which shows specific information about one selected entry. When I click in the table one entry, browser loads someView.xhtml and page url looks like this:
somepage?p_auth=wRR1tGze&p_p_id=SomePortlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_SomePortlet__facesViewIdRender=%2Fviews%2Findex.xhtml
I need to create url which directly shows me page someView.xhtml and get custom parameter from url.
I'm using primefaces 3.5 and liferay 6.2 with bridge 3.2.4.
Thanks in advance.
Marcin

Liferay Faces Bridge has a Facelet implementation of the portlet: JSP tags.
Not sure if that's what you are asking for, but you can do a full page HTTP GET by putting something like this in your facelet view:
<ui:composition xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:portlet="http://java.sun.com/portlet_2_0">
<portlet:renderURL var="renderURL">
<portlet:param name="_jsfBridgeViewId" value="someView.xhtml" />
</portlet:renderURL>
<h:outputLink value="#{renderURL}" />
</ui:composition>

Related

CommandLink action redirect to external url not working

I am using jsf 2. I have a link in a facelet file that points to a external url. My current view is /app1/home.xhtml with a h:commandLink that has an action like so
<h:commandLink value="#{link}" action="#{action.redirect}" target="_blank"/>
the url that action.redirect redirects to is /app2/info.do. However it appears that JSF tries to change the extension of the url to .xhtml. and then fails with the error message.
com.sun.faces.context.FacesFileNotFoundException: /app2/info.xhtml Not Found in ExternalContext as a Resource
how do i get this to correctly redirect ?
Don't use the <h:commandLink> here, it's the wrong tag for the purpose. It is designed to submit a JSF POST form and navigate to a JSF view, both which you obviously don't want to let take place in this particular use case.
Just use the <h:outputLink> or even plain HTML <a>. So,
<h:outputLink value="#{action.redirect}" target="_blank">#{link}</h:outputLink>
or
#{link}
Note that you don't need the <h:form> in both approaches.

Simple jsf2.0 application : javax.faces.application.ViewExpiredException View could not be restored

In a simple jsf2.0 application,I am getting this exception :javax.faces.application.ViewExpiredException View could not be restored.
In console I am getting following error:
org.portletfaces.bridge.BridgeException: Must first call setPortletContext(PortletContext)
When I executed my application without useing Primefaces jar it worked properly. But after addind Primefaces jar I started getting this exception.
I am using Tomcat 7.2.
EDIT: There are only 3 pages in the application and no backing bean.. A link on first page is invoking second page. But when I click on the link I am getting this error and the second page is not displayed.
Cant understand the cause of the problem. Please help.
There are only 3 pages in the application and no backing bean.. A link on first page is invoking second page. But when I click on the link I am getting this error and the second page is not displayed.
That can happen if you're navigating by UICommand links/buttons. You should not navigate by POST links/buttons at all, but just by GET links/buttons.
Replace all those UICommand links/buttons which are incorrectly been used for page-to-page navigation by normal UIOutcomeTarget links/buttons. In other words, replace <h:commandButton> by <h:button>, <h:commandLink> and <p:commandLink> by <h:link> and <p:commandButton> by <p:button>.
I.e. do not use
<h:form>
<p:commandButton value="Go to next page" action="nextpage" />
</h:form>
but instead use
<p:button value="Go to next page" outcome="nextpage" />
See also:
When should I use h:outputLink instead of h:commandLink?

Adding a ApplicationBarIconButton from Application.Resources in XAML

I have a ApplicationBarIconButton which I use repeatedly on multiple pages.
I want to be able to define the Click action in my Application C# file.
I have
<Application.Resources>
<shell:ApplicationBarIconButton
x:Key="AddLocationAppBarIconBut"
IconUri="/icons/appbar.add.rest.png"
Text="location"
Click="Add_Location_Event" />
in my App.xaml file and I want to load it into another page I have under
<phone:PhoneApplicationPage.ApplicationBar>
How do I go about this exactly?
Can I bind a click event to an event in my Application cs file?
Unfortunately, you can't!
In full WPF framework, you'd be able to do something like this:
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar>
<shell:ApplicationBar.Buttons>
<StaticResource ResourceKey="AddLocationAppBarIconBut" />
</shell:ApplicationBar.Buttons>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
The trick here is in the usage of StaticResource, but Silverlight doesn't have a backing class representation like that; you can only use the Markup Extension as {StaticResource AddLocationAppBarIconBut}, but this won't help you here!
What you can is define the full ApplicationBar object as a resource and then just call it on the page like this:
<phone:PhoneApplicationPage ApplicationBar="{StaticResource ApplicationBarResource}">
<!-- remaining code -->
<phone:PhoneApplicationPage />

Get JSF 2 url to change after first, not second click

JSF URLs don't seem to change after the first click, only after the 2nd. E.g. when navigating from home.jsf to auction.jsf, the page being displayed is already auction.jsf but the URL in the browser address bar stays at home.jsf, until I click on the Auction link the second time. Why is this happening? Is there a way to disable it, and get the url to display correctly?
You seem to be navigating by POST instead of by GET. You should not perform page-to-page navigation by POST in first place. Replace <h:commandLink> by <h:link>.
So, do not navigate by
<h:form>
<h:commandLink value="Auction" action="auction" />
</h:form>
but by
<h:link value="Auction" outcome="auction" />
A JSF POST form basically submits to the current URL and any navigation is by default performed by a server-side forward using RequestDispatcher#forward() (if you are well familiar with the basic Servlet API, you know what this means). You could work around with performing a redirect instead
<h:form>
<h:commandLink value="Auction" action="auction?faces-redirect=true" />
</h:form>
but, as said, this is a work around not a solution.
See also:
When should I use h:outputLink instead of h:commandLink?
You need to add faces-redirect=true to your URL. Check out this short tutorial from Mkyong.com.

synchronisation of pages in address bar in a web application using jsf2

I'm working in a jsf2 application and I have a problem with the page name displayed in the address bar. When I navigate to an other page I have in address bar the name of the previous page.
Thanks.
That's because you're using POST instead of GET for page-to-page navigation and JSF defaults to submitting to the very same view with you're using <h:form>. Using POST for page-to-page navigation is in any case a very poor approach. It's not only not user friendly (it's not bookmarkable and confusing), but it's also not SEO friendly (searchbots don't follow <form> actions).
To solve this problem, you must stop using <h:commandLink> for page-to-page navigation. Use <h:link> instead.
<h:link value="Go to next page" outcome="nextpage" />
This will just render a
Go to next page
Which is perfectly bookmarkable, user friendly and SEO friendly.
When you're navigating to a different page as result of a real POST form submit, I'd also replace that by returning null or void and just render the result/messages conditionally in the very same view. Or if the result is supposed to be bookmarkable (e.g. search results), you should use normal links or plain <form> instead of <h:form> and use <f:viewParam> in target view.
See also:
When should I use h:outputLink instead of h:commandLink?
This is happerning because, in JSF, when you are navigating to a new page the server is performing a forward to the new page. The browser is unaware of the forwarding and is displaying the old url. To make the server perform a redirect instead of forwarding, you have to either:
If you are using navigation rules, add a <redirect/> to each rule:
<navigation-rule>
<from-view-id>/pages/from.xhtml</from-view-id>
<navigation-case>
<from-outcome>to</from-outcome>
<to-view-id>/pages/to.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
If you are returning an outcome from a bean method, add a ?faces-redirect=true to the outcome:
public String navigate() {
return "/pages/to?faces-redirect=true";
}

Resources