0
votes

I am working on a JSF (2.2) application. I am seeing some weird behavior working with h:form and h:commandbutton.

Issue - I have following code in say searchRecord.xhtml -

<h:form>
    <!-- Input fields -->
    <h:commandbutton type="submit" value="Search" title="Search" action="#{bean.search}"/> 
</h:form>

The issue I am facing is when I click on submit button, it shows 404-page not found with URL pointing to current page. It is not executing the specified bean action.

I tried to debug this. When the form is getting translated into HTML, the form is getting generated with method="post" action="/MyApplication/WEB-INF/searchRecord.xhtml" (which looks to be the correct behaviour). Still, on clicking the button, I am getting 404.

Can anyone please help me figuring out what is the issue? I wasted my weekend figuring this out but in vain.

EDIT - IDE - Eclipse JSF Version - Mojarra 2.2.8 Directory structure of my project is -

Project

- Java Resource
----src -> contains java files
- WebContent
---- META-INF
---- WEB-INF
------facelets -> contains *.xhtml files
------resources -> contains img, css and JS files in respective folders
------commonLayout.xhtml
- index.xhtml

I access my application using a launchHandler servlet which validates the request parameters and forward to searchRecord.xhtml.

I am able to see searchRecord.xhtml. but Now when I click , I am getting 404.

As a standard, we are required to use servlet and then forward accordingly.

1
what is written in your bean.search method? Is the navigation rule defined in faces-config.xml correct? - AswathyPrasad
type="submit" is superfluous... (it might even cause problems sometimes if I remember correctly) - Kukeltje
"which looks to be the correct behaviour" Nope, it isn't. Try entering that URL in browser's address bar. Try accessing any /WEB-INF file this way. Including web.xml. You'll see that this is impossible. Not without reason. - BalusC
@EAP - The bean.search method is returning a outcome (String) based on user input. The high level implementation is to return success if user parameters is valid and a row is found in the database and error if user parameter is invalid. Based on outcome, the navigation to next page will happen. - Akash
@BalusC - Yeah, it is correct. I am not able to access any resources inside /WEB-INF. Can you please point out what I am doing wrongly? P.S. - I can't post the entire code here because of work place restrictions. Apologies for it! - Akash

1 Answers

1
votes

I found solution for my problem. The issue here was the wrong directory structure (Somehow I missed the point that resources under /WEB-INF are not reachable by URL. Thanks to @BalusC for pointing this out!!!). Based on the answers on below post -

JSF files inside WEB-INF directory, how do I access them?

Which XHTML files do I need to put in /WEB-INF and which not?

I restructured my projects as follows -

My Application
|- Java Resource
|----src -> contains java files
|- WebContent
|---- META-INF
|---- Resources -> contains img, css and JS files in respective folders
|---- JSF
|      |--Contains client .xhtml files
|---- WEB-INF
|      |--template -> contains the master templates for my application
|      |--web.xml
|---- index.xhtml

Now the navigation is happening as expected and all the pages are displayed.

I am also planning to use JSF 2.2 configuration parameter and put resources under WEB-INF.