JSF 2.0 javax.faces.webapp.FacesServlet mappig - jsf-2

I started to use JSF 2.0 recently and I don't understand completely how I need to configure the javax.faces.webapp.FacesServlet to correctly handle resources.
For example, If I decided to create a web application with .xhtml files and .jsp files and I want both them to use jsf technology how am I supposed to configure the jsf servlet to handle both?
Sometimes I found example where the servlet url pattern is just /faces/*
thanks!

I suggest to use a suffix pattern as URL pattern like *.jsf. If a Facelets file (.xhtml) is present on the given view ID, then it will be served. Otherwise if a JSP file (.jsp) is present on the given view ID, then it will be served. This also gives you the room to gradually upgrade from JSP to Facelets without the need to change URLs, so that you can ultimately get rid of those legacy JSPs in an easy way.

Related

Sitecore gives a blank page with just the text "Default page" in my MVC solution

When I browse to my startpage, e.g. /sv I get a blank page that just says "Default Page". However when I try /sv/ it works. Subpages like /sv/example work without slash though. I'm using Sitecore 7.1 with only MVC views.
Remove the default.aspx file from the web root.
That will fix your problem.
When requesting URLs without a slash at the end, the "StripLanguage" processor of the preprocessRequest pipeline rewrites path to the value of the Settings.DefaultPageName setting ("default.aspx" by default). Since such page physically exists on your site, ASP.NET MVC routing system does not handle such request, and the file itself is served. This behavior is controlled over the RouteCollection.RouteExistingFiles property (false by default), please refer to the following article for the details:
http://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.routeexistingfiles.aspx.
In other case, when a slash is added after a language, this won't happen, since the "StripLanguage" processor does not rewrite the path (which is also not an expected behavior). As a result, request URL does not match the "default.aspx" static file in the site and request is getting processed by ASP.NET MVC.
I suggest you to add the following setting to the "Web.config" file (instead of creating a "default.aspx" page), which points to the "default" page without extension:
<settings>
<setting name="DefaultAspxPageName" value="default"/>
</settings>
After that, the /default URL, without ".aspx" extension, will be processed by MVC and the appropriate item will be rendered independently of a slash after the language URL section.
On my side it works.
I want to point out that the answer to this is not my own but given from the support over at Sitecore who I want to extend a big "Thank you!" to. I had googled this forever until they helped me and I thought that I want to have this document and easily found when others struggle with it. A bug is filed and they are working on fixing it.
DefaultAspxPageName is Hidden Setting.. We can find more such hidden settings..#
http://www.newguid.net/sitecore/2014/sitecore-hidden-string-configuration-settings/

Calling a Jsf View id from a non JSF page

We are integrating new application with existing JSP application and trying to re use some of existing functionality.
I have a navigation rule in faces-navigation.xml like
<from-view-id>/WEB-INF/jsp/admin/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/WEB-INF/jsp/admin/welcome.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>admin</from-outcome>
<to-view-id>/WEB-INF/jsp/admin/dashaboard.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
In my jsp I am trying to call this definition like
Admin login
When I click on "Admin Login", I get page not found exception.
Is there another way to call this view ?
Files in /WEB-INF are not publicly accessible (i.e. the enduser can't open any files in /WEB-INF directly by entering its bare URL in browser's address bar). They are only accessible by a servlet which does a RequestDispatcher#forward() on the file in /WEB-INF folder. The old webapp code setup was apparently using such a servlet, either homegrown or from a different MVC framework.
You should be moving those pages to outside the /WEB-INF folder. I would by the way also remove the misleading /jsp part from the path as those files are not JSP files at all. Given the .xhtml extension, you are instead actually using its successor Facelets.
By the way, navigation rules are obsolete since JSF 2.0 thanks to the new "implicit navigation" feature. Perhaps you was focusing too much at JSF 1.x targeted books/tutorials while learning JSF?

JSF 2 Access on Facelet Files

I am starting to explore JSF 2 facelet and I would like to test this in a simple project.
I just have some query regarding the file structure in JSF 2. When I was using Spring,
I use to put all my pages under WEB-INF so that they wont be accessible to the browser.
I notice in JSF 2, you should put your *.xhtml outside of WEB-INF and allow access to them thru
the Faces Servlet.
Question, does this mean that all enterprise application that utilizes JSF always put
a security constraint in their web.xml?
<security-constraint>
<web-resource-collection>
<web-resource-name>XHTML files</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint />
</security-constraint>
Or they are using some sort of a filter, that traps all incoming request and then reject request
that has *.xhtml?
Is my understanding correct and if so which one is more apt to be used?
Thanks
A third alternative in JSF 2.x is to map the FacesServlet just straight on *.xhtml instead of *.jsf or whatever. This way you don't need to cobble with security constraints or filters to prevent endusers from directly accessing *.xhtml files. It has the only disadvantage that you cannot serve "plain vanilla" XHTML files without invoking the FacesServlet, but that would in turn already not make much sense, because such files should technically have the *.html extension.
Please note that this doesn't work in old JSF 1.x. The FacesServlet would run in an infinite loop invoking itself again and again.

How can I ensure all requests go through an interceptor stack?

When working in Struts2, it is just too easy to create a template and refer a URL to it without creating an associated Action. Struts2 merrily renders the template -- which is fine in most cases, but not in our case: in order to ensure proper selection of a locale, we need all our requests go through a minimal interceptor stack.
We've been researching these two ways, both unsuccessfully:
Defining a "default action" which
would be executed for any template
which doesn't have an associated
action.
Disabling the ability to
render templates without an action
-- this would force programmers to define actions for any template,
which is a good solution too.
Thanks.
Maybe look into the wildcard mappings.
<action name="*" class="struts2you.examplelogin.BaseActionSupport">
<result name="success">{1}.jsp</result>
</action>
If you place something like this as the first action I think all your unmapped jsp will be run through the default interceptor stack which you can define in struts.xml
Then also place your jsp files under the WEB-INF directory to prevent direct access
When working in Struts2, it is just too easy to create a template and refer a URL to it without creating an associated Action.
Since Struts2 is an MVC framework, every request to it should invoke an action class and therefore go through an interceptor stack. I assume that you mean that you have JSPs that are not under WEB-INF and so they can be invoked directly via URL. That's generally a poor practice in the MVC world, as your JSPs should only represent the view layer. Place the JSPs somewhere under WEB-INF and all requests will be forced to go through an action, which will resolve your problem.
As for the choices, I would advocate creating explicit mappings for each of your templates.

Map a struts2 action result to a controller instead of a jsp

I'm working on on a site built using struts2. The vast majority of our targets generate xml, so mapping the result to a jsp page makes sense. A couple of our targets actually generated binaries. I'm wondering if there is a convenient way to say that the result should come from a servlet/controller instead of a jsp.
Obviously this could be done by modifying the web.xml so the struts filter doesn't apply to those targets and mapping those targets as servlets, but this is non-ideal. Currently, the struts filter applies to the entire site, and we would like to keep it that way.
Ideas?
Thanks!
I am not sure I understand you scenario, but it seems to me you want the Stream Result
Here you can find an excellent tutorial with code examples about Struts2 file upload and file download that covers the Stream Result:
http://www.jeetrainers.com/struts2-course/chapter12-13-1-1#slide

Resources