我知道也许其他人已经问过这个问题了,我在stackoverflow上检查了所有相关的问题,但我不能解决我的问题,我希望你们的人能帮助我。我现在正在学习Spring MVC,目前我正在处理简单的页面。由于某些原因,我的代码不能加载,即使我认为一切都是正确的(或者至少我认为我是这样做的)。当我尝试访问localhost:8080/PROJECTNAME/欢迎时,我得到了Description,源服务器没有找到目标资源的当前表示,或者不愿意透露存在。
来自Java的错误是:在名为'spring-dispatcher‘的DispatcherServlet中,找不到URI /ProjetName/欢迎的HTTP请求的映射。
我使用的是Apache Tomcat8.5,它集成在Eclipse IDE和JDK8中
这是我的代码。
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";
}
发布于 2018-08-25 02:42:06
更改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)
发布于 2018-08-25 02:43:11
<url-pattern>/</url-pattern>
您未指定任何应用程序上下文。因此,要么将欢迎添加到您的映射中,要么通过localhost:8080/ PROJECTNAME
访问它
https://stackoverflow.com/questions/52009940
复制相似问题