2015-09-08 37 views
1

請問我需要您的幫助來運行Spring應用程序,我是Spring的新手,我一直有問題需要運行它。我使用NetBeans創建了項目,並創建了所有默認文件,但代碼未運行。請幫忙,因爲我已經尋找解決方案,但都沒有幫助。由於在DispatcherServlet中使用URI [/ TestSpring /]發現名爲'dispatcher'的HTTP請求未找到映射

的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> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</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> 

調度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    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"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 
</beans:beans> 

的applicationContext.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"> 

    </beans> 
控制器
/* 
* 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 com.School; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.portlet.ModelAndView; 

/** 
* 
* @author olugbokikisemiu 
*/ 

@Controller 
public class Index { 

    @RequestMapping(value = "/index", method = RequestMethod.GET) 
    public ModelAndView IndexView() 
    { 
     ModelAndView mv = new ModelAndView("index"); 
     return mv; 
    } 
} 
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.jsp"); %> 
+0

我在redirect.jsp中除了一些問題。提供redirect.jsp和相關的類文件代碼。 – Ajit

+0

你是如何運行應用程序?當你得到錯誤時,你想要哪個網址? – jny

+0

我已經上傳了redirect.jsp ...謝謝 –

回答

0

你是如何運行應用程序?

你的問題的標題,你所提供的對應來看,您的網址應該是/index沒有/TestSpring

+0

請我不明白你在說什麼..我的代碼中的映射是/ index不是/ TestSpring ....我詳細的解釋將幫助plsss –

+0

什麼是您在瀏覽器中打開的網址?你如何運行應用程序?它應該是'response.sendRedirect(「index」)' – jny

+0

我直接從netbeans運行它... netbeans啓動瀏覽器時看到的網址是「http:// localhost:8080/TestSpring /」我注意到不是看到「索引」。另外我將重定向類中的「Index.jsp」更改爲「Index」,但仍然無效 –

0

我也有類似的問題。檢查你的war文件是否被創建(如果你正在使用maven,則在目標文件夾中)。然後將war文件部署到tomcat並嘗試訪問它。在您的IDE中,您也可以嘗試停止服務器,然後單擊* .war文件並嘗試運行它。

雖然我使用的註釋與xml配置對比。

相關問題