2016-01-19 144 views
1

我想在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頁面正確打開

index.jsp OutPut

,但是當我點擊鏈接頁面上即URL http://localhost:8080/SpringMVCBasic/hello.html,我得到錯誤

HTTP Status 404 - 

當我使用這樣的網址http://localhost:8080/SpringMVCBasic/hello,我得到這個

HTTP Status 404 - /SpringMVCBasic/hello 

我已經採取了代碼here

+1

您應該像在控制器中定義的那樣將鏈接從「hello.html」更改爲「hello」。 –

+0

嘗試將URL模式更改爲/*。html

+0

什麼是您的上下文路徑?你在使用Eclipse還是Intellij Idea?任何你在控制檯中看到的東西。 –

回答

2

您所提供的映射:

@RequestMapping("/hello") 

,但你必須做的:

@RequestMapping("/hello.html") 
+0

不,不工作 –

+0

你把'hellopage.jsp'放入WEB-INF/jsp /目錄嗎? –

+0

是的,上面我提到了我從哪裏下載整個項目的網站,並試圖按原樣運行。 –

-1

請將所有這些jar添加爲外部jar或將這些jar添加到您的lib文件夾

Spring Jar

+0

我已經通過eclipse的屬性添加了所有spring framework 4.1.1 JAR文件。 –

0

我下載了eclipse項目並在jboss上運行它。需要 繼罐子被列入:

  • 春天上下文
  • 彈簧核心
  • 彈簧網
  • 彈簧webmvc
  • 彈簧AOP
  • 彈簧豆
  • 春季表情

如果我之後用Java 8運行jboss,似乎都可以正常工作。

0

在spring-servlet中啓用Spring MVC @Controller編程模型。這樣的XML:

<mvc:annotation-driven /> 
0

在你的index.jsp,而不是寫

href="hello.html"> 
change to 
href="./hello.html"> 

HelloworldController.java,而不是寫

@RequestMapping("/hello") 

變化: -

@RequestMapping(value="/hello.html")