控制器无法正常运行Spring MVC

时间:2016-09-24 06:56:23

标签: java spring spring-mvc controller

我可以加载所需的jsp,但控制器不能与jsp一起使用。另外,出于某种原因,如果我将jsp放在对应的文件夹配置中,就像web.xml / servletConfig文件被映射一样,那么它就不会让我访问jsp了。但是,如果我将jsp文件放在webapp文件夹中,我就可以访问它。

的web.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 class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="WEB-INF/jsp/" p:suffix=".jsp" />

</beans>

projectHub-servletConfig.xml

package com.projectHub.controllers;

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

@Controller
public class ProjectHubController {

    //http://localhost:8080/projectHub/homepage.html
    @RequestMapping(value="/homepage")
    public String getHomepage(Model model) {

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

        return "homepage";
    }

}

控制器

<%@ 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>

homepage.jsp

 SELECT *
 FROM table1 t1
   LEFT  JOIN table2 t2
        ON t1.Field1=t2.Field2

文件夹结构图片:http://imgur.com/Gv75lwz

1 个答案:

答案 0 :(得分:0)

根据您的配置

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

您的jsp应位于 /WEB-INF/jsp 文件夹中。

因此,当您执行此操作return "homepage";时,它将转换为/WEB-INF/jsp/homepage.jsp,如果您的jsp不在此路径中,您将获得404