简体   繁体   English

Openshift Spring MVC Tomcat应用程序的部署路径返回404

[英]Openshift Spring MVC Tomcat application's deployed path returns 404

I am running a Tomcat7 application using Spring MVC on OpenShift under the domain: financial-datasite.rhcloud.com . 我正在使用OpenShift上的Spring MVC在以下域中运行Tomcat7应用程序: Financial-datasite.rhcloud.com I run and test the application locally using a Tomcat server and later push it to the remote repository. 我使用Tomcat服务器在本地运行和测试应用程序,然后将其推送到远程存储库。 Currently, there's only a HomePage and a button underneath redirecting to a different page. 当前,只有一个主页和一个重定向到其他页面的按钮。 When testing locally, both pages display contents as expected. 在本地测试时,两个页面均按预期显示内容。 However, when deployed to the remote server, only the HomePage is displayed, and on clicking the button, I get an HTTP 404 error. 但是,当部署到远程服务器时,仅显示主页,并且在单击按钮时,出现HTTP 404错误。 I have come across various similar questions here, but none of them have helped so far. 我在这里遇到了各种类似的问题,但到目前为止,它们都没有帮助。 I have played around configuring the web.xml, pom.xml, servlet-context.xml, and the controller files. 我玩过配置web.xml,pom.xml,servlet-context.xml和控制器文件的过程。 However, none of those have helped. 但是,这些都没有帮助。 I have also been checking the tail files and logs to monitor what's happening, which suggests that the remote server is accessing some 'printWelcome' method (which doesn't even exist in my project) in the controller class for the second page: 我还一直在检查尾文件和日志以监视正在发生的情况,这表明远程服务器正在第二页的控制器类中访问某些“ printWelcome”方法(我的项目中甚至不存在):

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/Sectors],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.spring.mvc.SectorController.printWelcome(org.springframework.ui.ModelMap)

Here is my project structure . 这是我的项目结构 In my localhost, the default page is run as localhost:8181/mvc/ and the second page is run as http://localhost:8181/mvc/Sectors . 在我的localhost中,默认页面运行为localhost:8181/mvc/ ,第二个页面运行为http://localhost:8181/mvc/Sectors Similarly, post deployment, the home page is run as http://financial-datasite.rhcloud.com and the second page executed as http://financial-datasite.rhcloud.com/Sectors , which throws a 404 error for /WEB-INF/views/hello.jsp, which, again, doesn't even exist in my project directory. 同样,在部署后,主页以http://financial-datasite.rhcloud.com运行,第二页以http://financial-datasite.rhcloud.com/Sectors执行,这为/ WEB引发404错误-INF / views / hello.jsp,同样,它甚至不存在于我的项目目录中。 Another thing I observed in the log files are that the code is likely not hitting the SectorController class as I have coded for print commands to be logged, which aren't really getting logged into the console when the page is requested from the deployed site. 我在日志文件中观察到的另一件事是,由于我已经编写了要记录的打印命令的代码,因此代码很可能未命中SectorController类,当从已部署的站点请求页面时,这些命令实际上并没有登录到控制台中。 I am quite unsure about which files are being run on the remote server and if there are any configuration issues that I am unaware of. 我不太确定远程服务器上正在运行哪些文件,以及是否有我不知道的配置问题。 Following are my web.xml, pom.xml, servlet-context.xml, Sectors.jsp, Google-Maps.js from where I've called the new page to load), and SectorController.java (which is the controller file for the second page). 以下是我的web.xml,pom.xml,servlet-context.xml,Sectors.jsp,Google-Maps.js(我从中调用了要加载的新页面)和SectorController.java(这是用于控制的文件)第二页)。 Apologies for a lengthy question, please let me know if you require any more information. 对于一个冗长的问题,我们深表歉意。如果您需要更多信息,请告诉我。 Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。

web.xml web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

  <display-name>Financial Data Site</display-name>

  <servlet>
    <servlet-name>financial</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>financial</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <welcome-file-list>
    <welcome-file>index</welcome-file>
  </welcome-file-list>

</web-app>

pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.spring</groupId>
    <artifactId>mvc</artifactId>
    <name>SpringMVC</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>

    <properties>
        <java-version>1.6</java-version>
        <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>

        <!-- Newly Added from here -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>

    <repositories>
        <repository>
            <id>eap</id>
            <url>http://maven.repository.redhat.com/techpreview/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>eap</id>
            <url>http://maven.repository.redhat.com/techpreview/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
    <!-- Till here -->

    <dependencies>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- MySQL database driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.10.Final</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>   

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>

        <!-- Tag Library -->
        <dependency>
          <groupId>taglibs</groupId>
          <artifactId>standard</artifactId>
          <version>1.1.2</version>
          <scope>compile</scope>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- When built in OpenShift the 'openshift' profile will be 
                used when invoking mvn. -->
            <!-- Use this profile for any OpenShift specific customization 
                your app will need. -->
            <!-- By default that is to put the resulting archive into the 
                'deployments' folder. -->
            <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
            <id>openshift</id>
            <build>
            <finalName>financial</finalName>
                <plugins>
                    <plugin>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.4</version>
                        <configuration>
                            <outputDirectory>webapp</outputDirectory>
                            <warName>ROOT</warName>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

servlet-context.xml servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <annotation-driven />

    <context:component-scan base-package="com.spring.mvc" />

    <resources mapping="/resources/**" location="/resources/" />

    <beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

</beans:beans>

Sectors.jsp Sectors.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ page session="false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sectors</title>
</head>

<body>
    <h1>Message</h1>
    <c:if test="${not empty Sectors}">

        <ul>
            <c:forEach var="_SectorNames" items="${Sectors}">
                <li>${_SectorNames}</li>
            </c:forEach>
        </ul>

    </c:if>
</body>

</html>

Google-Maps.js : following snippet only contains the function used to create a div section on the map and to call a new page on a new window Google-Maps.js :以下代码段仅包含用于在地图上创建div部分和在新窗口中调用新页面的函数

function HomeControl(controlDiv, map)
{
      // Set CSS for the control border.
      var _ControlUI = document.createElement('div');
      _ControlUI.style.backgroundColor = '#fff';
      _ControlUI.style.border = '2px solid #fff';
      _ControlUI.style.borderRadius = '3px';
      _ControlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
      _ControlUI.style.cursor = 'pointer';
      _ControlUI.style.marginBottom = '22px';
      _ControlUI.style.textAlign = 'center';
      _ControlUI.title = 'Click to filter by Sectors';
      controlDiv.appendChild(_ControlUI);

      // Set CSS for the control interior.
      var _ControlText = document.createElement('div');
      _ControlText.style.color = 'rgb(25,25,25)';
      _ControlText.style.fontFamily = 'Roboto,Arial,sans-serif';
      _ControlText.style.fontSize = '12px';
      _ControlText.style.lineHeight = '38px';
      _ControlText.style.paddingLeft = '5px';
      _ControlText.style.paddingRight = '5px';
      _ControlText.innerHTML = '<strong>View by Sectors</strong>';
      _ControlUI.appendChild(_ControlText);

      // Setup the click event listeners, also calls Sectors page on a new window
      google.maps.event.addDomListener(_ControlUI, 'click', function() {
          //add code here to redirect to Sectors page
          var _Window = window.open('/mvc/Sectors', '__blank');
          _Window.focus();
      });
}

SectorController.java SectorController.java

    package com.spring.mvc;

    import java.text.DateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Locale;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;

    import com.spring.dao.impl.SectorDAOImpl;
    import com.spring.model.Sector;

    @Controller
    public class SectorController {

        private static final Logger logger = LoggerFactory.getLogger(SectorController.class);

        @RequestMapping(value = "/Sectors", method = {RequestMethod.HEAD, RequestMethod.GET})
        public ModelAndView DisplaySectors(Locale locale, Model model) {

            logger.info("Welcome home! You are in: {}.", locale);

            Date date = new Date();
            DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

            String formattedDate = dateFormat.format(date);

            model.addAttribute("serverTime", formattedDate );

            SectorDAOImpl _SectorDAOImpl = new SectorDAOImpl();
            List<Sector> _Sectors = _SectorDAOImpl.GetByID();       
            List<String> _SectorNames = new ArrayList<String>();

            for( Sector sector : _Sectors) {
                _SectorNames.add(sector.getSectorName());
            }

            ModelAndView _ModelAndView = new ModelAndView("Sectors");
            _ModelAndView.addObject("Sectors", _SectorNames);

            return _ModelAndView;
        }
}
  1. As Jessai noted in a comment, 正如Jessai在评论中指出的那样,

     var _Window = window.open('/mvc/Sectors', '__blank'); 

    Do not use your project name explicitly! 不要显式使用项目名称! There are a ways to get your context name, such as request.getContextPath() method of HttpServletRequest. 有一种获取上下文名称的方法,例如HttpServletRequest的request.getContextPath()方法。

    In this case with a hard-coded URL string I think that you can use relative URL, just 'Sectors' or './Sectors'. 在这种情况下,使用硬编码的URL字符串,我认为您可以使用相对URL,仅使用'Sectors'或'./Sectors'。

    References: 参考文献:

  2. '__blank' : you meant '_blank' ? '__blank':您的意思是'_blank'?

  3. By the way: 顺便说说:

    You are deploying on Tomcat 7, so you can declare adherence to Servlet 3.0 specification instead of 2.5 in your web.xml file. 您正在Tomcat 7上进行部署,因此可以在web.xml文件中声明遵守Servlet 3.0规范,而不是2.5。

    See the following to disable components scanning at startup: 请参阅以下内容以在启动时禁用组件扫描:
    https://wiki.apache.org/tomcat/HowTo/FasterStartUp#Configure_your_web_application https://wiki.apache.org/tomcat/HowTo/FasterStartUp#Configure_your_web_application

  4. On

     INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/Sectors],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.spring.mvc.SectorController.printWelcome(org.springframework.ui.ModelMap) 

    If your class file does not match your source code, it means that your code has not been compiled. 如果您的类文件与源代码不匹配,则表明您的代码尚未编译。 Delete your compiled classes (eg use mvn clean ) and try again. 删除已编译的类(例如,使用mvn clean ),然后重试。

    If you are curious, you can unpack your war file with any ZIP archiver application and look what is actually there. 如果您感到好奇,则可以使用任何ZIP存档器应用程序解压缩war文件,并查看其中的实际内容。

  5. Do you or our company own the domain name of http://spring.com web site? 您或我们公司是否拥有http://spring.com网站的域名? If not, DO NOT use package name com.spring and do not use <groupId>com.spring</groupId> . 如果不是,请不要使用包名称com.spring ,也不要使用<groupId>com.spring</groupId> Those names do not belong to you. 这些名称不属于您。 They are someone else's property. 他们是别人的财产。

  6. On

     <org.springframework-version>3.1.1.RELEASE</org.springframework-version> 

    If you are using 3.x, why not the current 3.2.12.RELEASE in 3.x series, or better the last 4.1.6.RELEASE? 如果您使用的是3.x,为什么不选择3.x系列中的当前3.2.12.RELEASE,或者更好的是最后的4.1.6.RELEASE? Spring Framework 3.1.x has reached end of life and is no more supported. Spring Framework 3.1.x已到使用寿命,并且不再受支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Openshift上部署的Tomcat应用返回404 - Deployed Tomcat app on Openshift returns 404 Spring Boot MVC应用程序在部署到外部Tomcat / tc Server实例时返回HTTP 404 - Spring Boot MVC Application returns HTTP 404 when deployed to an external Tomcat/tc Server instance 部署在Tomcat上的Jersey应用程序返回404 - Jersey application deployed on Tomcat returns 404 spring-mvc应用程序中的tomcat 404错误 - tomcat 404 error with spring-mvc application 在OpenShift Tomcat上部署后无法运行Spring MVC Web应用程序? - Not able to run Spring MVC Web application after deploying on OpenShift Tomcat? 部署在Tomcat上的Spring MVC应用程序无法在IntelliJ上运行 - Spring MVC application deployed on Tomcat does not run on IntelliJ 将@Valid与部署到Tomcat的Spring MVC应用程序一起使用时出现问题 - Problem using @Valid with Spring MVC application deployed to Tomcat 部署到 Tomcat 时无法访问基本 REST Spring MVC 应用程序 - Basic REST Spring MVC application cannot be reached when deployed to Tomcat Tomcat使用Spring MVC返回404页面; 控制器未达到 - Tomcat returns a 404 page using Spring MVC; controller not reached Spring 引导应用程序在本地主机上运行,但在外部 Tomcat 上返回 404 - Spring Boot application runs on localhost but returns 404 on external Tomcat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM