【发布时间】:2017-08-22 18:39:38
【问题描述】:
我正在尝试在 Eclipse 中制作一个 spring hello world 程序。这是代码
index.jsp
<a href="hello.html">click</a>
HelloWorldController.java
package com.javatpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "HELLO SPRING MVC HOW R U";
return new ModelAndView("hellopage", "message", message);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.javatpoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
hellopage.jsp
Message is: ${message}
当我转到http://localhost:8080/SpringMVCBasic/ 时,我正确打开了 index.jsp 页面
但是当我点击页面上的链接时,即 url http://localhost:8080/SpringMVCBasic/hello.html,我得到了错误
HTTP Status 404 -
当我使用http://localhost:8080/SpringMVCBasic/hello 这样的网址时,我得到了这个
HTTP Status 404 - /SpringMVCBasic/hello
我已从here获取代码
【问题讨论】:
-
您应该将链接从“hello.html”更改为“hello”,就像在控制器中定义的那样。
-
尝试将 URL 模式更改为
/*.html -
你的上下文路径是什么?您使用的是 Eclipse 还是 Intellij Idea。您在控制台中看到的任何内容。
标签: java spring jsp spring-mvc