0

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.

Akash
  • 608
  • 5
  • 17
  • what is written in your bean.search method? Is the navigation rule defined in faces-config.xml correct? – AswathyPrasad Sep 04 '17 at 09:41
  • `type="submit"` is superfluous... (it might even cause problems sometimes if I remember correctly) – Kukeltje Sep 04 '17 at 09:45
  • *"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 Sep 04 '17 at 10:35
  • @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 Sep 04 '17 at 11:46
  • @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 Sep 04 '17 at 11:49
  • @Kukeltje - Can you please let me know what else to use or any material I can refer? – Akash Sep 04 '17 at 11:50
  • Just leave it out... – Kukeltje Sep 04 '17 at 11:53
  • @EAP - regarding navigation rules, I am making use of JSF implicit navigation. So there are no navigation rules in faces-config.xml. – Akash Sep 04 '17 at 11:56
  • @Kukeltje - I tried leaving the type=submit but I am still getting the same error. – Akash Sep 04 '17 at 12:02
  • As @BalusC mentioned, the problem seems to be with html generated which is accessing the resource inside /WEB-INF. But I am not sure how to resolve it as the form (include action) is generated by JSF. – Akash Sep 04 '17 at 12:02
  • Sorry, I never intended it to be the solution, I said it was superfluous.... – Kukeltje Sep 04 '17 at 12:04
  • @Akash This is not the default behavior. You don't need to post your whole project. Just post a [mcve]. See also https://stackoverflow.com/tags/jsf/info Based on the symptoms I can only guess that there's a custom `ViewHandler` somewhere in the application which is not correctly handling the `getActionURL()` call. That's where the `` gets its action from. – BalusC Sep 04 '17 at 12:24
  • Thank you very much @BalusC for your time. I'll try to add the relevant code. Regarding the viewhandler, we have not implemented any custom view-handler and making use of default view handler. I'll try to debug further and check if anyhow the getActionURL() is overriden. I'll post my findings here. – Akash Sep 04 '17 at 12:48
  • I have edited my question to provide few more details. Please check if this is helpful in any way. – Akash Sep 04 '17 at 13:06

1 Answers1

1

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.

Akash
  • 608
  • 5
  • 17