How do i properly access the .xhtml files when i run my project in Intellij IDEA

84 Views Asked by At

I'm trying to learn Java Web Development. I created a new Jakarta EE 8.0 web project with JSF and Servlet as dependecies on Intellij IDEA and i've been trying to change the default home page to a home.xhtml file created by me but when i start the Tomcat server it always return's a 404 error. The home.xhtml file is inside the webapp folder in the project and the default url that the IDE generates is "localhost:8080/JSFStarter_war_exploded/". How do i need to structure the project and where do the .xhtml files go? Also how do i configure the web.xml file? The "welcome-file" property does not seem to be working. Sorry for any typos, english is not my first language.

My web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/home.xhtml</welcome-file>
    </welcome-file-list>

</web-app>
1

There are 1 best solutions below

0
On

Make sure your home.xhtml file is located in the webapp folder of your project. Remove the '/' in the welcome file definition in the web.xml:

<welcome-file-list>
  <welcome-file>home.xhtml</welcome-file>
</welcome-file-list>

Then restart the server and you should now see home.xhtml.