2015-11-12 42 views
0

這是我在春天的第一次嘗試,這個問題似乎很基礎。我花了很多時間試圖讓它工作,但不能。根據我對Spring的瞭解,有一個調度程序servlet將所有傳入流量路由到適當的控制器。春天4世界的Hello World程序,netbeans IDE

根據上述理解,我已經使用NetBeans IDE創建了一個項目。代碼看起來像這樣。

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>redirect.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

redirect.jsp中

<%-- 
Views should be stored under the WEB-INF folder so that 
they are not accessible except through controller process. 

This JSP is here to provide a redirect to the dispatcher 
servlet but should be the only JSP outside of WEB-INF. 
--%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<% response.sendRedirect("index.htm"); %> 

調度-servlet.xml中

<?xml version='1.0' encoding='UTF-8' ?> 
<!-- was: <?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:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 

    <!-- 
    Most controllers will use the ControllerClassNameHandlerMapping above, but 
    for the index controller we are using ParameterizableViewController, so we must 
    define an explicit mapping for it. 
    --> 
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="mappings"> 
      <props> 
       <prop key="index.htm">helloController</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/jsp/" 
      p:suffix=".jsp" /> 

    <!-- 
    The index controller. 
    --> 
    <bean name="indexController" 
      class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
      p:viewName="index" /> 

    <bean name="helloController" 
      class="controller.HelloController" 
      p:viewName="helloWorld" /> 

</beans> 

HelloController.java

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 

/** 
* 
* @author rv814771 
*/ 
@Controller 
public class HelloController { 

    @RequestMapping("/helloWorld") 
    public String sayHello() { 
     return "index"; 
    } 
} 

的helloWorld.jsp

<%-- 
    Document : helloWorld 
    Created on : Nov 12, 2015, 12:40:16 PM 
    Author  : rv814771 
--%> 

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <h1>Hello World!</h1> 
    </body> 
</html> 

爲什麼它不會被重定向到的hello world頁面。

+0

如果這是你在春天的第一次嘗試,溝全部爲手工配置,已經過時並且不必要的複雜。使用Spring Boot,它可以自動完成所有這些。 http://start.spring.io – chrylis

回答

0

爲什麼你已經重定向到「的index.htm」

response.sendRedirect("index.htm") 

但是控制器被映射到:

@RequestMapping("/helloWorld")