Where are the pictures of my JSF page? - jsf-2

I have a navigation-rule like this in my JSF 2 application:
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-action>#{myBean.goToMyHome1}</from-action>
<from-outcome>myHome1.xhtml</from-outcome>
<to-view-id>/myhome1/myHome1.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
When I put an h:commandLink in myHome2.xhtml within directory myhome2 that satisfy this rule, the page myHome1.xhtml will display but there are no images on the page. Where they go?
Behind the application is Springsecurity 3 with this config:
<sec:intercept-url pattern="/myhome1/**" access="hasAnyRole('ROLE1','ROLE2')" />
<sec:intercept-url pattern="/myhome2/**" access="hasRole('ROLE2')" />

This look like as if your picture URLs are relative to the current URL path. E.g.
<img src="images/picture.png" />
When you open the page by
http://example.com/contextname/myHome1.xhtml
then the image URL will effectively point to
http://example.com/contextname/images/picture.png
But when you open the page by
http://example.com/contextname/myhome/myHome1.xhtml
then the image URL will effectively point to
http://example.com/contextname/myhome/images/picture.png
You need to fix it accordingly. There are several ways. Use a domain-relative path
<img src="/contextname/images/picture.png" />
Or prepend the context name dynamically
<img src="#{request.contextPath}/images/picture.png" />
Or just use <h:graphicImage> which will do that automatically
<h:graphicImage value="/images/picture.png" />

Related

rich:menuItem action doesn't redirect without navigation-rule

In JSF I want to redirect a page from a dropdown menu. It redirects when I use faces-config using navigation-rule:
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>test</from-outcome>
<to-view-id>/pages/design/TestDetails.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
But when I try to pass the URL directly, I'm not able to redirect:
<rich:menuItem label="someLabel" action="/pages/design/TestDetails.xhtml" />
What is the fault in my code?
Just new to RichFaces, found out below solution with javascript, seems working:
<rich:dropDownMenu mode="client" label="Navigate">
<rich:menuItem label="Page - 1" onclick="document.location.href='samplePage.jsf'" />
<rich:menuItem label="Page - 2" onclick="document.location.href='samplePage2.jsf'" />
</rich:dropDownMenu>
Basically you should use navigation-rule in JSF's action fields (that's what they're for). But if you want to put URL directly then just add ?faces-redirect=true to it (the equivalent of <redirect/> in navigation-rule):
<rich:menuItem label="someLabel" action="/pages/design/TestDetails.xhtml?faces-redirect=true" />
(Notice that you need to put full path not just TestDetails?faces-redirect=true as #Omar suggested in comments.)
Also: <from-view-id>*</from-view-id> can be ommited in rules like this one.

navigation between jsf pages within one portlet on WebSphere Portal 8.0

I'm working on WebSphere Portal 8.0 and I am trying to make portlet with few faces.
Can anyone share with me how can I make a simple link to other JSF site within one portlet?
I mean, I was trying to use this, but it doesn't work:
<h:outputLink value="page2.xhtml">
<h:outputText value="take me to the page2" />
</h:outputLink>
I'm redirected to the misterious link but I would like to stay on the same site but change face in this single portlet.
I have got answer.
If you want use GET, use this:
<h:link value="take me to the page2" outcome="page2" />
Or by POST use:
<h:form>
<h:commandLink action="#{menager.navigateToPage2}" value="take me to the page2" />
</h:form>
and in faces-config.xml
<managed-bean>
<managed-bean-name>menadzer</managed-bean-name>
<managed-bean-class>test.Menadzer</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>symbolAktywnejSpolki</property-name>
<value>#{param.symbol}</value>
</managed-property>
</managed-bean>
<navigation-rule>
<display-name>index.xhtml</display-name>
<from-view-id>index.xhtml</from-view-id>
<navigation-case>
<from-action>#{menager.navigateToPage2}</from-action>
<from-outcome>page2</from-outcome>
<to-view-id>page2.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
and the class:
public class Menager {
...
public String navigateToPage2() {
return "page2";
}
}
and if you use WebSphere Portal add this, to the faces-config.xml:
<application>
<view-handler>com.ibm.faces20.portlet.FaceletPortletViewHandler</view-handler>
<resource-handler>com.ibm.faces20.portlet.httpbridge.PortletResourceHandler</resource-handler>
<el-resolver>com.ibm.faces20.portlet.PortletELResolver</el-resolver>
</application>

no effect of "resultpath" constant in struts.xml

I have all the jsp pages (ex: index.jsp) in the WebContent folder and my application is working smoothly.
Now, I created a folder "pages" inside WebContent, where I transferred all my jsp pages.
And I added the following line inside the <struts> tags:
<constant name="struts.convention.result.path" value="/pages" />
But still, its not searching for jsp inside the "pages" folder.
I am getting:
HTTP Status 404 - /MyApplicationName/index.jsp
Where am I doing a mistake?
EDIT: The current structure of my application is as follows:
The welcome page is an action instead of a jsp page. The result page of this action is index.jsp. This action populates the dropdown menu and some other fields in index.jsp. Now, I want to keep index.jsp along with other jsp pages in the folder "WebContent/pages". I have over 30 jsp pages in my application, and I don't want to mention the full path of every jsp in the result-tag. Thats why I want to make use of the struts2 constant "struts.convention.result.path"
My question is, why the code I have mentioned above is not working?
Accessing index.jsp directly bypasses all S2 request handling.
There's no S2 or S2 Convention plugin involved, you're just accessing a JSP page.
If index.jsp is first page of your application then you need to change its path to /pages/index.jsp in <welcome-file> tag of web.xml. And for rest I agree with #Dave Newton :)
Probably your mistake was in struts.xml.
When you create a new-Folder under web-content make with the same name (folder-name) of
xml file under src and include it in struts.xml file it will work fine.
For eg: suppose you created example folder under web-content, now create example.xml file
under src folder and in struts.xml file include example.xml
example.xml:
<struts>
<package name="example" namespace="/example" extends="default">
<action name="your action name" class="your class" method="your method">
</action>
</package>
</struts>
struts.xml:
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<action name="" class="" method="">
</action>
</package>
<include file="example.xml"/>

action isn't invoked at the first click

i have a page with authentification which forword to my template page where there is menus.
<navigation-rule>
<from-view-id>pages/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>userOK</from-outcome>
<to-view-id>pages/template.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>userNOK</from-outcome>
<to-view-id>pages/login.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
my page template.xhtml
<h:form>
<p:menu type="plain" style="width:200;box-shadow: 6px 6px 6px black;top:-18;left:-40" >
<p:submenu label="Dossier" id ="Dossier" >
<p:menuitem update=":contentform,:messages" value="Nouveau Dossier" action="#{choix.setPage('ajoutDossier')}"
....
....
</h:form>
<h:form id="contentform" >
<h:panelGroup rendered="#{choix.page == 'ajoutDossier'}">
<ui:include src="Dossier/ajoutDossier.xhtml" />
</h:panelGroup>
....
....
</h:form>
the problem is that action isn't invoked at the first click i have to click 2 time , and before i click many time and action isn't invoked, and sometimes juste p:submenu which are in the last position work
also when it was no forward just the page template.xhtml it's work !
You are not taking advantage of JSF templates. Instead of using a template and several template clients you have everything in one page; and render stuff conditionally. Look this tutorial or any other tutorial about jsf tempaltes.
Normally you would have a template, which has all the layout and content common to all pages; and uses the tag <ui:insert name="title" >Default content</ui:insert> as many times as you need (with different names). Then you create a template client, defining the template using
<ui:composition template="./../resources/templates/templateFile.xhtml">
And then use the tag <ui:define name="nav2"> to set the content to include in the template.
Besides that, about the problem with navigation in your code: when you do a navigation you don't update content, because after a navigation the page is completely loaded (no ajax). So first of all you have to remove your update attribute; and also you have to set the "ajax" attribute of p:menuitem to false; because by default it's true. otherwise navigation will not take place.
the redirect attribute in the navigation rule is not required if you did things ok. It can work perfectly fine with a forward. The only problem with forward is that the browser URL will not change, because browser is not notified of the navigation.
i found the solution i have to add <redirect /> after the <to-view-id> of page which have the problem, because i read that JSF uses internal FORWARD instead of REDIRECT to navigate to the success page and this why the URL will not get changed but i don't really undestand why ??
here is the code who work :
<navigation-rule>
<from-view-id>pages/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>userOK</from-outcome>
<to-view-id>pages/template.xhtml</to-view-id>
<redirect />
</navigation-case>
<navigation-case>
<from-outcome>userNOK</from-outcome>
<to-view-id>pages/login.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
also to not loose css of redirect page i have to add this
<link rel="stylesheet" href="#{request.contextPath}/css/style.css" />

Issue with Spring security's logout

I've got a problem logging out in Spring framework.
First when I want j_spring_security_logout to handle it for me i get 404 j_spring_security_logout not found:
sample-security.xml:
<http>
<intercept-url pattern="/messageList.htm*" access="ROLE_USER,ROLE_GUEST" />
<intercept-url pattern="/messagePost.htm*" access="ROLE_USER" />
<intercept-url pattern="/messageDelete.htm*" access="ROLE_ADMIN" />
<form-login login-page="/login.jsp" default-target-url="/messageList.htm"
authentication-failure-url="/login.jsp?error=true" />
<logout/>
</http>
Sample url link to logout in JSP page:
Logout
When i try to use a custom JSP page i.e. I use login form for this purpose then I get better result at least it gets to login page, but another problem is that you dont't get logged off as you can diretcly type url that should be guarded buy you get past it anyway.
Slightly modified from previous listings:
<http>
<intercept-url pattern="/messageList.htm*" access="ROLE_USER,ROLE_GUEST" />
<intercept-url pattern="/messagePost.htm*" access="ROLE_USER" />
<intercept-url pattern="/messageDelete.htm*" access="ROLE_ADMIN" />
<form-login login-page="/login.jsp" default-target-url="/messageList.htm"
authentication-failure-url="/login.jsp?error=true" />
<logout logout-success-url="/login.jsp" />
</http>
Logout
Thank you for help
I've just had this problem.
You need to make sure in web.xml your security filter matches on the url /j_spring_security_logout
e.g.
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/j_spring_security_logout</url-pattern>
</filter-mapping>
You should do POST request. Something like that:
<form action="${logoutUrl}" method="post" id="logoutForm">
<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}" />
</form>
<script>
function formSubmit() {
document.getElementById("logoutForm").submit();
}
</script>
<c:if test="${pageContext.request.userPrincipal.name != null}">
<h2>
Welcome : ${pageContext.request.userPrincipal.name} |
Logout
</h2>
</c:if>
I ran into the same problem and after loosing hope, finally I found out the answer by accident.
Of course we learn a lot by reading and using someone else's codes and, by doing this we inherit settings we don't know much about.
And this is what happened to me when programming using Spring Security.
In the Spring Security XML, within the http tag, there is this line:
<logout invalidate-session="true" logout-success-url="/login" logout-url="/logout" />
I got this line during my research from some tutorial or example. And after 2 days struggling with the j_spring_security_logout keyword and getting nothing but error 404, I figured out this.
In the logout tag I am using, there's this logout-url parameter set to "/logout". Then I realized that according to my settings, my spring is expecting to receive /logout instead of /j_spring_security_logout.
Once I updated my code accordingly, it worked like a charm.
Is logout link aware of the context path?
For example, if your context path is "myapp", where does the above mentioned link point?
"http://localhost:8080/myapp/j_spring_security_logout" or "http://localhost:8080/j_spring_security_logout" ?
In fact, the j_spring_security_logout is only valid within the context of the webapp so only the first link would lead to the correct url
I had the same issue.
Seems to be a bug on 3.0.6!
I just downgrade to 3.0.5 and everything works nicely.
Try this link in your page whow content a logout link:
<h:outputLink value="#{request.contextPath}/logout.jsp">Logout</h:outputLink>
and creeate a logout.jsp file in your "webcontent" folder with the following code:
<% response.sendRedirect("/#{request.contextPath}/j_spring_security_logout"); %>
if an eror occured try to change "#{request.contextPath}" to the name of your project
ex: my project name is "security" so i am using in my logout.jsp file:
<% response.sendRedirect("/security/j_spring_security_logout"); %>

Resources