繁体   English   中英

Spring MVC中的RequestMapping失败

[英]RequestMapping failing in Spring MVC

我已经尝试了几乎所有尝试来弄清楚这里发生了什么。 我无法让控制器响应我输入的网址,并继续出现HTTP 404错误。

我正在使用的网址http://localhost:8080/projectHub/getHomepage.html

目录-http://imgur.com/Gv75lwz

projectHub-servletConfig.xml

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

    <mvc:annotation-driven/>

    <context:component-scan base-package="com.projectHub.controllers"></context:component-scan>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="WEB-INF/jsp/" p:suffix=".jsp" />

</beans>

homepage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>ProjectHub Homepage</title>
</head>
<body>

    <h1>${titleHomepage}</h1>
    <p>WTF</p>

</body>
</html>

ProjectHubController.java

@Controller
public class ProjectHubController {

    @RequestMapping(value="/getHomepage")
    public String getHomepage(Model model) {

        model.addAttribute("titleHomepage", "ProjectHub");

        return "homepage";
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app 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"
    version="3.0">

    <display-name>ProjectHub</display-name>

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

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

</web-app>

我认为您在web.xml中的网址格式错误。 它应该是“ / *”而不是“ /”。

Http URL区分大小写。 您网址中的projectHub是大写P还是小写? 尝试使用P大写。 也不要在末尾使用.html。 如果问题仍然存在,请共享服务器sysouts。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM