Calling a Jsf View id from a non JSF page - jsf-2

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?

Related

Struts 2: Navigation of pages through Link

I am using struts2 . I want that after clicking on link on 1st page it should go to 2nd jsp page.How to do that.
Thanks in Advance
First remember one thing since you are working with MCV2 based platform so its never advised to go from a jsp page to another jsp page directly which is being treated as bad practice.(you are not going through proper request cycle)
Request should go through Actions.Struts2 provide a convenient way to help you for such use-cases.
If in your struts config file you do not provide any Action class name framework will create an action class for you on the fly with return type as SUCCESS. This is all you need to do:
<action name=gotoJSP2>
<result>/page2.jsp</result>
</action>
This is all you need and you are good to go.
Though you can go from one JSP to Another but make sure you have no Struts2 tags and any such framework dependence since going form one JSP to another means not calling Struts2 Dispatcher filter and not letting framework to do required work to serve you.

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.

JSF 2.0 javax.faces.webapp.FacesServlet mappig

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.

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.

In GWT I'd like to load the main page on www.domain.com and a dynamic page on www.domain.com/xyz - I just can't make it work

I have a question for which I'm sure there must a simple solution. I'm writing a small GWT application where I want to achieve this:
www.domain.com : should serve the welcome page
www.domain.com/xyz : should serve page xyz, where xyz is just
a key for an item in a database. If there is an
item associated with key xyz, I'll load that and show a page, otherwise I'll show a
404 error page.
I was trying to modify the web.xml file accordingly but I just couldn't
make it work. I could make it work with an url-pattern if the key in question is after another /, for example: www.domain.com/search/xyz. However,
I'd like to have the key xyz directly following the root / (http://www.domain.com/xyz).
Something like
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
doesn't seem to work as I don't know how to address the main page
index.html (as it's not an actual servlet) which will then load my
main GWT module.
I could make it work with a bad work around (see below): Redirecting a
404 exception to index.html and then doing the look up in the main
entry point, but I'm sure that's not the best practice, also for SEO
purposes.
Can anyone give me a hint on how to configure the web.xml with GWT for
my purpose?
Thanks a lot.
Mike
Work-around via 404:
Web.xml:
<web-app>
<error-page>
<exception-type>404</exception-type>
<location>/index.html</location>
</error-page>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
index.html --> Main Entry point --> onModuleLoad():
String path = Window.Location.getPath();
if (path == null || path.length() == 0 || path.equalsIgnoreCase("/")
|| path.equalsIgnoreCase("/index.html")) {
... // load main page
} else {
lookup(path.substring(1)); // that's key xyz
You don't!
GWT is meant fro creating single-page webapplications (RIA) that don't do hyperlinking to all sorts of loose pages. You essentially bootstrap the GWT app when loading it in the initial page and dynamically generate content in the app by using widgets, handling events and requesting server data through either GWT-RPC or simple HTTP requests (for instance when accessing a REST service on some other domain).
I introduced a colleague to GWT two weeks ago and let him dig around for a while when he got stuck on how to "link to a new page?". I explained GWT is not geared towards page-based navigational websites, but for building applications that use the browser as their runtime.
Hope this can shed a little light on your problem :)
Regards,
-- Jeroen
Jeroen is right. You don't.
However, if you want to force it, you can. Essentially every new url will have to have an HTML page that contains the GWT widget. Thus, causing the GWT widget to then have to reload itself on every new url. Then it would configure its UI accordingly based upon the URL. You can use a REST framework such as Jersey or Restlet to make things easier.
But, you only want to do this if you intend to distribute these new links or urls outside of your GWT app. However, GWT supports URL changes through anchors (not sure if this is the proper terminology), see the documentation on History for more information. Do not 'link' within your app to other URLs on your domain that contain the same GWT widget. Doing anything that will cause a page refresh and your GWT widget to reload would be very inefficient. Use ClickHandlers.
To conclude, don't do this. =)
How about instead of
www.domain.com/?key=A1B2C3
you use GWT's way of doing it:
www.domain.com/#key=A1B2C3
you can do this via the History mechanism.
Try UrlRewriteFilter: http://tuckey.org/urlrewrite/ it is a plain ol' Java EE filter, so it should work.

Resources