我想在eclipse中製作一個spring hello world程序。下面是代碼spring mvc HTTP狀態404 -
的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>
彈簧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 url-pattern> –
什麼是您的上下文路徑?你在使用Eclipse還是Intellij Idea?任何你在控制檯中看到的東西。 –