【发布时间】:2016-10-21 20:59:27
【问题描述】:
我无法让我的控制器响应我在 Spring Web MVC 应用程序中的 HTTP 请求。该项目将部署,但只会加载默认的“index.jsp”文件。我相信我的问题可能在 context.xml 文件中,但如果是问题,我不确定如何正确配置文件。
项目结构:
-src/main/java/controllers/projectGitControllers
-src/main/webapp/WEB-INF/config/projectGit-servletConfig.xml
-src/main/webapp/WEB-INF/pages/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>
【问题讨论】:
-
如果你是spring初学者,我强烈推荐你使用现代xml免费配置。示例:techzoo.org/spring-framework/…
-
我过去曾这样做过,想扩展我的知识。
-
如果您浏览到:localhost:8080/getHomepage 会有什么结果
标签: java spring jsp spring-mvc