java.io.FileNotFoundException when file is in folder

时光总嘲笑我的痴心妄想 提交于 2019-12-12 04:14:17

问题


I have created project using JF 2.0.

When I run project, I can execute file xyz.xhtml by saying http://localhost:8080/myProject/faces/xyz.xhtml

However when I put the same in in folder xyz and try to execute as http://localhost:8080/myProject/faces/xyz/xyz.xhtml I get exception as java.io.FileNotFoundException.

First few lines of stacktrace are

java.io.FileNotFoundException
at org.apache.naming.resources.DirContextURLConnection.getInputStream(DirContextURLConnection.java:403)
at com.sun.faces.facelets.impl.DefaultFaceletCache._getLastModified(DefaultFaceletCache.java:172)
at com.sun.faces.facelets.impl.DefaultFaceletCache.access$000(DefaultFaceletCache.java:62)
at com.sun.faces.facelets.impl.DefaultFaceletCache$1.newInstance(DefaultFaceletCache.java:82)
at com.sun.faces.facelets.impl.DefaultFaceletCache$1.newInstance(DefaultFaceletCache.java:78)
at com.sun.faces.util.ExpiringConcurrentCache$1.call(ExpiringConcurrentCache.java:99)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)

Can someone point at what I am doing wrong?

Update 1

File structure I have is

+ Web Content
  + WEB-INF
    - web.xml
    - faces-config.xml
  + xyz
    - xyz.xhtml
  - xyz.xhtml

回答1:


Maybe you have a first folder structure like this

+ Web Content
  - xyz.xhtml
  + xyz
    - xyz.xhtml

And your servlet mapping configuration for the Faces Servlet looks like this:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

This means that the Faces Servlet (the servlet that send the request to the JSF framework) will apply for every page inside the ProjectName/faces/<whatever> (if and only if the pages are under a faces folder structure). If you want to apply the Faces Servlet for every html page in your site, change the configuration to this:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

EDIT

I've made a test based in your problem. It should work with no problems. Make sure the project have been built correctly and it has been correctly deployed, if you still have these problems try changing the servlet mapping configuration.




回答2:


The problem is in xyz.xhtml: it is referencing a resource (template? include?) using a relative path, instead of an absolute path - and when you move it down into a directory that reference is no longer valid.



来源:https://stackoverflow.com/questions/11477862/java-io-filenotfoundexception-when-file-is-in-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!