2015-10-29 59 views
0

我是SpringMVC arch的新手,我通過創建示例應用程序來玩耍。SpringMVC - 無法使用名稱'dispatcherServlet'解析servlet名稱爲'index'的視圖

這裏是我的代碼

MVC-調度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     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"> 

    <context:component-scan base-package="com.app.controller" /> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost/app" /> 
     <property name="username" value="root" /> 
     <property name="password" value="root" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="com.app.domain" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.current_session_context_class">thread</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
      </props> 
     </property> 
    </bean> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/pages/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

的web.xml

<web-app 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" 
    version="2.5"> 

    <display-name>myApp</display-name> 

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class> 
        org.springframework.web.context.ContextLoaderListener 
       </listener-class> 
    </listener> 
</web-app> 

** TestController.java *

package com.app.controller; 
import java.io.IOException; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
@RequestMapping(value = "/myApp/*") 
public class TestController { 
@RequestMapping(value = "/index") 
public ModelAndView index(HttpServletResponse response) throws IOException 
{ 
return new ModelAndView("index"); 
} 
} 

的index.jsp

<html> 
    <body> 
    <h2>Hello World!</h2> 
    <p>This is a sample Spring Project</p> 
    </body> 
</html> 

經過很多教程和其他資源去後,我發現,隨處可見它與編寫的代碼相同。但我仍然無法弄清楚爲什麼我無法加載我的index.jsp頁面點擊網址http://localhost:8080/myApp/index

我得到以下異常:

javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'dispatcherServlet' 
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1227) 
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 

這裏是我的項目結構:

└─WEB-INF 
    └─pages 
     index.jsp 
    │ mvc-dispatcher-servlet.xml 
    │ web.xml 

請幫我解決這個問題。

回答

0

您的servlet映射URL模式不會選擇JSP。見this answer

爲了解決這個問題,你可以改變

<servlet-name>mvc-dispatcher</servlet-name> 
<url-pattern>/</url-pattern> 

要:

<servlet-name>mvc-dispatcher</servlet-name> 
<url-pattern>*.jsp</url-pattern> 
+0

我還沒有看到'在項目結構index.jsp'。只有'index.html' – jny

+0

對不起@dev_feed,jny這是我的錯誤,它實際上是index.jsp,它在頁面文件夾中。我編輯了我的問題 – DivyaMenon

+0

我試過了。它仍然給出同樣的錯誤 – DivyaMenon

相關問題