2016-06-14 30 views
1

私が問題に苦しんだが、ここで記述されたリソースリゾルバはビューを解決したら春ブーツMVC構成要求が再び

No mapping found for HTTP request with URI Spring MVC

問題は、それが再び「doesnのディスパッチャにリダイレクトされますされそれにマップされたメソッドを見つける。例えば

/testとレゾルバのためのユーザーの要求が/views/test.jspにそれをマップしている場合、ディスパッチャの代わりに、レスポンスのレンダリングは再びパス/views/test.jspを検索しようとします。

デフォルトのディスパッチャサーブレットパスを/(デフォルトでは/)に変更しようとしました。

Springブート構成では機能しません。

ディスパッチャのパスを/以外のものに設定する必要がない場合は、/pageのような解決策があるかどうかを知りたいと思います。

問題を解決するためのテストコードは次のとおりです。

package com.example; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.context.embedded.ServletRegistrationBean; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.DispatcherServlet; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

@SpringBootApplication 
public class TestMvcApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(TestMvcApplication.class, args); 
    } 
} 

@Configuration 
class MvcConfig extends WebMvcConfigurerAdapter { 
    public MvcConfig() { 
     System.out.println("%%%%%%%%%%%%% " + getClass() + " loaded %%%%%%%%%%%%%%%"); 
    } 

    @Bean 
    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) { 
     ServletRegistrationBean registration = new ServletRegistrationBean(
       dispatcherServlet); 
     System.out.println("%%%%%%%%%%%%%%%%% Adding dispatcher servletmapping"); 
     registration.addUrlMappings("/"); 
     return registration; 
    } 
} 

@Controller 
class TestController { 
    @RequestMapping("test") 
    public String test() { 
     return "test"; 
    } 
} 

application.properties

logging.level.org.springframework.web=DEBUG 

spring.mvc.view.prefix: /views/ 
spring.mvc.view.suffix: .jsp 

(ここでhttps://github.com/ConsciousObserver/stackoverflow/tree/master/TestMvcテストプロジェクトのためのgithubのリンクです)私が最終的に解決策を見つけた

Returning [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/views/test.jsp]] based on requested media type 'text/html' 
Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/views/test.jsp]] in DispatcherServlet with name 'dispatcherServlet' 
Forwarding to resource [/views/test.jsp] in InternalResourceView 'test' 
DispatcherServlet with name 'dispatcherServlet' processing GET request for [/views/test.jsp] 
Looking up handler method for path /views/test.jsp 
Did not find handler method for [/views/test.jsp] 
Matching patterns for request [/views/test.jsp] are [/**] 
URI Template variables for request [/views/test.jsp] are {} 
Mapping [/views/test.jsp] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[[email protected]362712]]] and 1 interceptor 
Last-Modified value for [/views/test.jsp] is: -1 
Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 
Successfully completed request 
Successfully completed request 
+0

適切な統合を解除する 'MvcConfig'を削除します。本当にJSPを使いたいのですか?それは 'war'パッケージでしか動作せず、jspファイルはあなたの場合に' webapp/views'に入る必要があります。 –

+0

設定がどのように壊れているか詳しく教えてもらえますか?はい、レガシープロジェクトをSpringブートに変換するときにJSPが必要ですが、多くのJSPがあります。これはまだ他のビューレイヤに移行する準備ができていません。 – 11thdimension

+0

おそらくまだ動作することが予想される自動構成を無効にする(その一部)ため、壊れています。 –

答えて

1

関連のログがあります。

次の依存関係を含むと、問題が解決します。ディスパッチャを視野に要求をリダイレクト

<dependency> 
    <groupId>org.apache.tomcat.embed</groupId> 
    <artifactId>tomcat-embed-jasper</artifactId> 
    <scope>provided</scope> 
</dependency> 
<dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>jstl</artifactId> 
</dependency> 

私はJSPの解析エラーが内部であったことを推測している(あるいは、JSTLViewが存在しません)。

これらの依存関係を含めた後、JSPビューは正常にレンダリングされます。