Spring Web MVC HttpRequest无法正常运行

时间:2016-10-21 20:59:28

标签: java spring jsp spring-mvc

我无法让我的控制器在我的Spring Web MVC应用程序中响应我的HTTP请求。该项目将部署,但只会加载默认的" index.jsp'文件。我相信我的问题可能在context.xml文件中,但我不确定如果问题是如何正确配置文件。

项目结构:

-src /主/爪哇/控制器/ projectGitControllers

-src /主/ web应用/ WEB-INF /配置/ projectGit-servletConfig.xml

-src /主/ web应用/ WEB-INF /页/ homepage.jsp

我正在尝试使用的网址

- http://localhost:8080/projectGit/getHomepage

的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>projectGit</display-name>

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

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

</web-app>

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>projectGit Homepage</title>
</head>
<body>
    <h1>${titleHomepage}</h1>
    <p>Working</p>
</body>
</html>

projectGitControllers.java

package controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class projectGitControllers {

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

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

        return "homepage";
    }

}

projectGit-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:aop="http://www.springframework.org/schema/aop"
    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
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <mvc:annotation-driven/>

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

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


</beans>

2 个答案:

答案 0 :(得分:1)

您的@Controller班级有package controllers;

你有

base-package="com.application.projectGit.controllers"

因此,您的@Controller永远不会创建一个bean。

因此从package controllers;更改为package com.application.projectGit.controllers;

注意:是一种不好的做法,使用包名称的大写字符

添加01:

自:

<bean id="viewResolver"  
      class="org.s....InternalResourceViewResolver" 
      p:prefix="WEB-INF/pages/" 
      p:suffix=".jsp" />

p:prefix="WEB-INF/pages/"更新为p:prefix="/WEB-INF/pages/"。因此现在是/WEB-INF,它以/

开头

如果您收到相同或新的错误消息,请考虑更新您的帖子,以包含完整的错误堆栈跟踪。

答案 1 :(得分:0)

您的viewResolver指向WEB-INF/jsp/

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

而您的jsp位于WEB-INF/pages

src/main/webapp/WEB-INF/pages/homepage.jsp