たとえば、Web アプリケーションが Web サーバーの webapps ディレクトリに存在する場合、デフォルトの Tomcat ポートwebapps/myapp/
を想定して、このアプリケーション コンテキストのルートにアクセスできます。http://localhost:8080/myapp/
これは、末尾のスラッシュの有無にかかわらず機能するはずです。デフォルトではそう思います-確かにJetty v8.1.5の場合です
ヒット/myapp
すると、Spring DispatcherServletが引き継ぎ、リクエストを に<servlet-name>
構成された にルーティングします。web.xml
この場合は/ui/*
です。
DispatcherServlet は、すべてのリクエストを からhttp://localhost/myapp/ui/
にルーティングします@Controller
。
Controller@RequestMapping(value = "/*")
自体では、mainPage()メソッドに使用できます。これにより、 mainPage()http://localhost/myapp/ui/
にhttp://localhost/myapp/ui
ルーティングされます。
注: SPR-7064 のため、Spring >= v3.0.3 も使用する必要があります。
完全を期すために、これをテストしたファイルを次に示します。
src/main/java/controllers/UIRootController.java
package controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UiRootController {
@RequestMapping(value = "/*")
public ModelAndView mainPage() {
return new ModelAndView("index");
}
@RequestMapping(value={"/other"})
public ModelAndView otherPage() {
return new ModelAndView("other");
}
}
WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd"
version="3.0" metadata-complete="false">
<servlet>
<servlet-name>ui</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<!-- spring automatically discovers /WEB-INF/<servlet-name>-servlet.xml -->
</servlet>
<servlet-mapping>
<servlet-name>ui</servlet-name>
<url-pattern>/ui/*</url-pattern>
</servlet-mapping>
</web-app>
WEB-INF/ui-servlet.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="controllers" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:order="2"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/views/"
p:suffix=".jsp"/>
</beans>
また、 と に 2 つの JSP ファイルがWEB-INF/views/index.jsp
ありWEB-INF/views/other.jsp
ます。
結果:
http://localhost/myapp/
-> ディレクトリ一覧
http://localhost/myapp/ui
そしてhttp://localhost/myapp/ui/
-> index.jsp
http://localhost/myapp/ui/other
そしてhttp://localhost/myapp/ui/other/
-> other.jsp
お役に立てれば!