組み込みの Tomcat 8 を開始し、WebApplicationInitializerインターフェイスの実装を使用して Spring アプリケーション コンテキストをブートストラップします。この実装では、 DispatcherServletを Tomcat サーブレット コンテキスト、私のコードにも追加します。
@Override
public void onStartup(final ServletContext servletContext) throws ServletException
{
final XmlWebApplicationContext dispatcherContext = new XmlWebApplicationContext();
dispatcherContext.setConfigLocation("/path/to/context/defintion.xml");
// Register and map the dispatcher servlet
final ServletRegistration.Dynamic dispatcher =
servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.addMapping("/test/*");
}
どこかで私は:
Tomcat t = new Tomcat()
// ... some config ...
t.start()
上記のメソッドをトリガーします。正常に動作します。
コンテキスト定義は次のようになります。
<!-- annotation config -->
<context:annotation-config/>
<!-- scan for controllers -->
<context:component-scan base-package="com.samples.controller"/>
<!-- static resources -->
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
私のコントローラー:
@Controller
// @Order(value = 1)
public class ReadyController
{
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView ready(final HttpServletRequest request, final HttpServletResponse
response)
{
try
{
response.getWriter().print("Ready!");
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}
ブラウザで Ready メソッドを開こうとすると、404 Not Found と表示されます。mvc :resourcesマッピングを削除すると、突然機能します。Order アノテーションをコントローラーに追加し、order 属性を mvc:resources マッピングに追加しようとしましたが、うまくいきませんでした。
誰でもこれに対する解決策を見たことがありますか?
私が使用した:Java8、埋め込みTomcat8、Spring4
EDIT : ログファイル出力を追加
コンテキストに mvc:resources タグが追加されたログ出力は次のとおりです。
2014-04-29 15:41:31 DEBUG DispatcherServlet:838 - DispatcherServlet with name 'dispatcher' processing GET request for [/test]
2014-04-29 15:41:31 TRACE DispatcherServlet:1091 - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@3cf4de47] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:41:31 TRACE BeanNameUrlHandlerMapping:127 - No handler mapping found for [/test]
2014-04-29 15:41:31 TRACE DispatcherServlet:1091 - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@5f1fb0ba] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:41:31 TRACE SimpleUrlHandlerMapping:127 - No handler mapping found for [/test]
2014-04-29 15:41:31 TRACE DispatcherServlet:1091 - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@29f2754] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:41:31 TRACE SimpleUrlHandlerMapping:127 - No handler mapping found for [/test]
2014-04-29 15:41:31 WARN PageNotFound:1110 - No mapping found for HTTP request with URI [/test] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:41:31 TRACE DispatcherServlet:1053 - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@2bc277d6
2014-04-29 15:41:31 DEBUG DispatcherServlet:991 - Successfully completed request
2014-04-29 15:41:31 TRACE XmlWebApplicationContext:331 - Publishing event in WebApplicationContext for namespace 'dispatcher-servlet': ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[1ms]; status=[OK]
2014-04-29 15:41:31 TRACE XmlWebApplicationContext:331 - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[1ms]; status=[OK]
2014-04-29 15:41:31 TRACE ClassPathXmlApplicationContext:331 - Publishing event in org.springframework.context.support.ClassPathXmlApplicationContext@3c130745: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[1ms]; status=[OK]
2014-04-29 15:41:31 TRACE StaticApplicationContext:331 - Publishing event in org.springframework.context.support.StaticApplicationContext@63440df3: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[1ms]; status=[OK]
mvc:resources タグをコンテキスト ファイルから削除すると、reuqest が機能し、ログ出力は次のようになります。
2014-04-29 15:42:31 TRACE DispatcherServlet:1043 - Bound request context to thread: org.apache.catalina.connector.RequestFacade@1f28ea73
2014-04-29 15:42:31 DEBUG DispatcherServlet:838 - DispatcherServlet with name 'dispatcher' processing GET request for [/test]
2014-04-29 15:42:31 TRACE DispatcherServlet:1091 - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@1820ce89] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:42:31 TRACE BeanNameUrlHandlerMapping:127 - No handler mapping found for [/test]
2014-04-29 15:42:31 TRACE DispatcherServlet:1091 - Testing handler map [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@7753d076] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:42:31 DEBUG DefaultAnnotationHandlerMapping:124 - Mapping [/test] to HandlerExecutionChain with handler [com.samples.controller.ReadyController@5ce05166] and 1 interceptor
2014-04-29 15:42:31 TRACE DispatcherServlet:1131 - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@303e6671]
2014-04-29 15:42:31 TRACE DispatcherServlet:1131 - Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@75da797a]
2014-04-29 15:42:31 TRACE DispatcherServlet:1131 - Testing handler adapter [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter@384cb80]
2014-04-29 15:42:31 DEBUG DispatcherServlet:925 - Last-Modified value for [/test] is: -1
2014-04-29 15:42:31 DEBUG HandlerMethodInvoker:172 - Invoking request handler method: public void com.samples.controller.ReadyController.ready(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2014-04-29 15:42:31 DEBUG DispatcherServlet:1012 - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
2014-04-29 15:42:31 TRACE DispatcherServlet:1053 - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@1f28ea73
2014-04-29 15:42:31 DEBUG DispatcherServlet:991 - Successfully completed request
2014-04-29 15:42:31 TRACE XmlWebApplicationContext:331 - Publishing event in WebApplicationContext for namespace 'dispatcher-servlet': ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[2ms]; status=[OK]
2014-04-29 15:42:31 TRACE XmlWebApplicationContext:331 - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[2ms]; status=[OK]
2014-04-29 15:42:31 TRACE ClassPathXmlApplicationContext:331 - Publishing event in org.springframework.context.support.ClassPathXmlApplicationContext@353d0772: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[2ms]; status=[OK]
2014-04-29 15:42:31 TRACE StaticApplicationContext:331 - Publishing event in org.springframework.context.support.StaticApplicationContext@7113b13f: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[2ms]; status=[OK]
編集: mvc:resourcesタグを削除し、代わりにmvc:default-servlet-handlerタグを使用すると、同じ結果が得られ、リクエストが機能しません。