java - 为什么在我的 JBoss Tomcat 7 + Spring MVC 项目中出现 HTTP Status 404?

标签 java spring maven spring-mvc tomcat

我正在使用 jboss 7 + spring mvc 进行一些测试,它在本地运行,但是当我放入云中时,它具有 HTTP 状态 404。

/pswebproj/src/main/java/pswebproj/control/HelloController.java

package pswebproj.control;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/hello")
// URL Controlador 
// - http://localhost:8080/pswebproj/spring/hello
public class HelloController
{
    @RequestMapping("/simple") // .../pswebproj/spring/hello/simple
    public ModelAndView helloRequest()
    {
        String model = "Message from helloRequest(), UAU!";

        ModelAndView mv = new ModelAndView("/hellospring.jsp");
        mv.addObject("msg", model);
        return mv;
    }

    @RequestMapping("/param") // .../pswebproj/spring/hello/param?prm=sbrubbles
    public ModelAndView helloRequestParam(@RequestParam String prm)
    {
        String model = "Message from helloRequestParam() "+prm;

        ModelAndView mv = new ModelAndView("/hellospring.jsp");
        mv.addObject("msg", model);
        return mv;
    }

    @RequestMapping(value="/pathvar/{var}") 
    //.../pswebproj/spring/hello/pathvar/sbrubbles
    public ModelAndView helloRequestPath(@PathVariable String var)
    {
        String model = "Message from helloRequestPath() "+var;

        ModelAndView mv = new ModelAndView("/hellospring.jsp");
        mv.addObject("msg",model);
        return mv;
    }   
}

/pswebproj/pom.xml

<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>pswebproj</groupId>
    <artifactId>pswebproj</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>pswebproj</name>
    <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>
    <properties>
        <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>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.2-1003-jdbc4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>     
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- SERVLET -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>               
        <!-- // SERVLET -->
        <!-- SPRING -->        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>       
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>      
        <!-- // SPRING -->  
    </dependencies>
    <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 'webapps' 
                folder. -->
            <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
            <id>openshift</id>
            <build>
                <finalName>pswebproj</finalName>
                <plugins>
                    <plugin>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.1.1</version>
                        <configuration>
                            <outputDirectory>webapps</outputDirectory>
                            <warName>ROOT</warName>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    <build>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.apache.maven.plugins
                                        </groupId>
                                        <artifactId>
                                            maven-compiler-plugin
                                        </artifactId>
                                        <versionRange>
                                            [3.1,)
                                        </versionRange>
                                        <goals>
                                            <goal>testCompile</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

/pswebproj/src/main/webapp/WEB-INF/web.xml

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         metadata-complete="false">
  <display-name>PsWeb Example Project</display-name>

  <servlet>
        <servlet-name>springweb</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>  
    <servlet-mapping>
        <servlet-name>springweb</servlet-name>
        <url-pattern>/spring/*</url-pattern>
    </servlet-mapping>  
</web-app>

/pswebproj/src/main/webapp/WEB-INF/springweb-servlet.xml

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

    <!-- Pacotes a serem escaneados -->
    <context:component-scan base-package="pswebproj.control" />

    <!--  Define quais anotações estão habilitadas -->
    <mvc:annotation-driven />

</beans>

我正在使用本地 jdk1.7.0_80 + tomcat 7。我可以使用 url http://localhost:8080/pswebproj/spring/hello/simple 运行站点

输出是:

Java Puro:来自 helloRequest() 的消息,UAU!
JSTL:来自 helloRequest() 的消息,UAU!
EL:来自 helloRequest() 的消息,UAU!

但如果我在云中运行 http://pswebproj-drinith.rhcloud.com/spring/hello/simple ,我得到了 HTTP 状态 404 的答案。

最佳答案

Spring 容器未检测到您的 Controller,因为您错过了应该指向 WEB-INF/的 contextConfigLocation 路径springweb-servlet.xml 用于 DispatcherServlet,您需要将其添加到您的 web.xml 文件中:

<servlet>
   <servlet-name>dispatcher</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>WEB-INF/springweb-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

关于java - 为什么在我的 JBoss Tomcat 7 + Spring MVC 项目中出现 HTTP Status 404?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43262670/

相关文章:

Java:我的 classOf 和 newArray 怎么样?

查询方法中的Spring Data可选参数

java - Maven Javadoc 插件 - 覆盖默认 CSS 的特定部分

java - 如何使用 deploy :deploy-file 使用 jar 部署源文件

java - ConcurrentModificationException 使我的应用程序崩溃

java - 系统代理设置检测失败

java - 使用 Java (HttpURLConnection) 向 Restheart 进行身份验证(对于 Mongodb)

Spring SimpleJdbcTestUtils 示例

java - Java中的隐式(继承对象),更适合的方式?

java - 如何找到Spring Data JPA和Spring版本的正确jar文件