0

I get error 404 when I deploy the generated .war file to stand alone Tomcat server, but if I deploy the war inside my IDE(IntelliJ) with tomcat server it works fine.


This is the Initializer

        public void onStartup(ServletContext servletContext)
                       throws ServletException {
                      AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
                      mvcContext.register(AppConfig.class);

                servletContext.addListener(new ContextLoaderListener(mvcContext));

                      ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
                        "dispatcher", new DispatcherServlet(mvcContext));
                      dispatcher.setLoadOnStartup(1);
                      dispatcher.addMapping("/");
            }

This is the Application Configuration

    **Tiles Configuration**

        @Bean
        public TilesConfigurer getTilesConfigurer() {
            TilesConfigurer tilesConfigurer = new TilesConfigurer();
            tilesConfigurer.setDefinitions(
                    "/WEB-INF/defs/tiles.xml");
            return tilesConfigurer;
        }

        @Bean
        public UrlBasedViewResolver getUrlBasedResolver() {
            UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
            urlBasedViewResolver.setViewClass(new TilesView().getClass());
            return urlBasedViewResolver;
        }

        /** End of Tiles Configuration */

    **View Resolver:**

    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/pages/**");
            resolver.setSuffix(".jsp");
            return resolver;


    **Tiles.xml**

    <definition name="hello-tiles" extends="commons">
            <put-attribute name="header" value="/WEB-INF/template/header2.jsp" />
            <put-attribute name="title" value="Hello Page" />
            <put-attribute name="body" value="/WEB-INF/pages/hello.jsp" />
        </definition>
Andoy Abarquez
  • 1,119
  • 4
  • 17
  • 30

1 Answers1

-1

The application needs to know the Tomcat installation path on your production machine to resolve the paths. Either you can hard code in your xml or you can specify a properties file that can get the installation path of tomcat i.e CATALINA_HOME or CATALINA_BASE. The reason it works in your IDE and not on your production is because the system understands the /WEB-INF in your IDE but it doesn't happen so in when you deploy it in the stand alone system.

You can append your installation path of your standalone server to each of the paths. e.g. C:/Tomcat/bin. Hence you will need to refer your files like

<put-attribute name="body" value="C:/Tomcat/bin/WEB-INF/pages/hello.jsp" />

If you are not a fan of absolute path, you can refer the variables mentioned above and how to use them from a properties file.

Hope it helps.

user2339071
  • 4,254
  • 1
  • 27
  • 46