[英]Java Spring MVC mapping issue
I know that maybe other ppl already asked this question and I checked all of the relevant questions here at stackoverflow but I can't fix my problem and I was hoping someone from you can help me. 我知道也许其他人已经问过这个问题,所以我在stackoverflow上检查了所有相关问题,但我无法解决问题,我希望有人能帮助我。 I'm learning Spring MVC right now and I deal with simple pages for now.
我现在正在学习Spring MVC,现在我只处理简单的页面。 For some reason my code can't load even though I to everything correct(Or at least I think that I do).
由于某种原因,即使我一切正确,我的代码也无法加载(或者至少我认为可以)。 When I try to access the localhost:8080/PROJECTNAME/welcome I get Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
当我尝试访问localhost:8080 / PROJECTNAME / welcome时,我得到了描述源服务器未找到目标资源的当前表示形式,或不愿意透露该资源的存在。
The Error from Java is: No mapping found for HTTP request with URI [/ProjetName/welcome] in DispatcherServlet with name 'spring-dispatcher'. 来自Java的错误是:在DispatcherServlet中,名称为“ spring-dispatcher”的URI [/ ProjetName / welcome]找不到HTTP请求的映射。
I Use Apache Tomcat 8.5, which is integrated in the Eclipse IDE And also JDK 8 我使用了集成在Eclipse IDE和JDK 8中的Apache Tomcat 8.5
Here is my Code. 这是我的代码。
web.xml 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 弹簧调度-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 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 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 was changed in order to work below is the updated code: 为了工作,下面更改了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";
}
Change your servlet-mapping as below 如下更改您的servlet映射
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
specifying url-pattern as / only matches until host/servlet
or 'localhost:8080/PROJECTNAME/'
将url-pattern指定为/只会匹配到
host/servlet
或'localhost:8080/PROJECTNAME/'
Update: 更新:
Configure default or home page in your spring controller as below. 如下所示在spring 控制器中配置默认 页面或主页 。
Instead of 代替
@RequestMapping("/welcome")
do 做
@RequestMapping(value = "/", method = GET)
<url-pattern>/</url-pattern>
You didn't specify any application context. <url-pattern>/</url-pattern>
您未指定任何应用程序上下文。 So either add PROJECTNAME
to your mapping or access it through localhost:8080/welcome 因此,要么将
PROJECTNAME
添加到您的映射中,要么通过localhost:8080 / welcome访问它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.