java - Java Spring MVC 映射问题
问题描述
我知道也许其他人已经问过这个问题,我在stackoverflow上检查了所有相关问题,但我无法解决我的问题,我希望你能帮助我。我现在正在学习 Spring MVC,我现在处理简单的页面。由于某种原因,即使我一切都正确(或者至少我认为我这样做),我的代码也无法加载。当我尝试访问 localhost:8080/PROJECTNAME/welcome 时,我得到描述原始服务器没有找到目标资源的当前表示或不愿意透露存在的表示。
Java 的错误是:在名称为“spring-dispatcher”的 DispatcherServlet 中找不到具有 URI [/ProjetName/welcome] 的 HTTP 请求的映射。
我使用集成在 Eclipse IDE 中的 Apache Tomcat 8.5 以及 JDK 8
这是我的代码。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id = "WebApp_ID" version = "2.4"
xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>MySpringMVCWebApp</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-dispatcher-servlet.xml
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation = "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.xsd">
<context:component-scan base-package="org.mypackagename.com.*">
</context:component-scan>
<bean id="viewResolver"
class =
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value ="/WEB-INF/jsp/"/>
<property name = "suffix" value =".jsp"/>
</bean>
HelloController.java
package org.mypackagename.com;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView("HelloPage");
mav.addObject("msg", "Helloooooooooooo");
return mav;
}
}
HelloPage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC</title>
</head>
<body>
<h2>${msg}</h2>
</body>
</html>
为了工作,HelloController.java 进行了更改,下面是更新后的代码:
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)public String printHello(ModelMap
model) {
model.addAttribute("message", "Hello the fcking Spring MVC Framework!");
return "hello";
}
解决方案
如下更改您的 servlet 映射
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
将 url-pattern 指定为/ 只匹配直到 host/servlet
或'localhost:8080/PROJECTNAME/'
更新:
在你的 spring控制器中配置默认或主页,如下所示。
代替
@RequestMapping("/welcome")
做
@RequestMapping(value = "/", method = GET)
推荐阅读
- java - 我怎样才能带来统一格式的电话。数字
- c++ - 声明用于 qml 和 cpp 文件的全局字符串
- android - 如何访问此 api 在 Kotlin 中包含的每一个天气,我想将这些数据放入 RecyclerView 并显示为列表
- javascript - 如何从 LightningChart JS 隐藏特定的点形状?
- c++ - C++ 模板错误:“模板参数的显式指定参数无效”
- python - tkinter 代码对一些 gif 进行动画处理,但不是全部
- netlogo - BehaviorSpace Netlogo- Measure 使用这些报告器运行
- neo4j - Cypher 函数调用根据结果数更改返回值
- codenameone - CN1模拟器openGalery限制
- google-analytics - 现有媒体资源中的 Analytics 新视图未显示实时数据