Springmvc-beannameurlhandlermapping
Spring MVC-Bean名URLハンドラーマッピングの例
次の例は、Spring Web MVCフレームワークを使用してBean名URLハンドラーマッピングを使用する方法を示しています。 BeanNameUrlHandlerMapping クラスはデフォルトのハンドラーマッピングクラスであり、URLリクエストを構成に記載されているBeanの名前にマップします。
<beans>
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/"/>
<property name = "suffix" value = ".jsp"/>
</bean>
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name = "/helloWorld"
class = "com.finddevguides.HelloController"/>
<bean name = "/hello*"
class = "com.finddevguides.HelloController"/>
<bean name = "/welcome"
class = "com.finddevguides.WelcomeController"/>
</beans>
たとえば、URIの場合、上記の構成を使用します
/helloWorldまたは/hello \ {any letter}がリクエストされると、DispatcherServletはリクエストを *HelloController に転送します。 /welcomeが要求されると、DispatcherServletはその要求を *WelcomeController に転送します。 */welcome1が要求された場合、DispatcherServletはコントローラーを検出せず、サーバーは404ステータスエラーをスローします。
まず、動作するEclipse IDEを用意し、Spring Web Frameworkを使用して動的フォームベースのWebアプリケーションを開発するための次の手順を検討します。
Step | Description |
---|---|
1 | Create a project with a name TestWeb under a package com.finddevguides as explained in the Spring MVC - Hello World chapter. |
2 | Create Java classes HelloController, WelcomeController under the com.finddevguides package. |
3 | Create view files hello.jsp, welcome.jsp under the jsp sub-folder. |
4 | The final step is to create the content of all source and configuration files and export the application as explained below. |
HelloController.java
package com.finddevguides;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HelloController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("hello");
model.addObject("message", "Hello World!");
return model;
}
}
WelcomeController.java
package com.finddevguides;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class WelcomeController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("welcome");
model.addObject("message", "Welcome!");
return model;
}
}
TestWeb-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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/"/>
<property name = "suffix" value = ".jsp"/>
</bean>
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name = "/helloWorld"
class = "com.finddevguides.HelloController"/>
<bean name = "/hello*"
class = "com.finddevguides.HelloController"/>
<bean name = "/welcome"
class = "com.finddevguides.WelcomeController"/>
</beans>
hello.jsp
<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
welcome.jsp
<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
ソースファイルと構成ファイルの作成が完了したら、アプリケーションをエクスポートします。 アプリケーションを右クリックし、[エクスポート]→[WARファイル]オプションを使用して、 TestWeb.war ファイルをTomcatのwebappsフォルダーに保存します。
次に、Tomcatサーバーを起動し、標準ブラウザーを使用してwebappsフォルダーから他のWebページにアクセスできることを確認します。 URLを試してください- http://localhost:8080/TestWeb/helloWorld 。SpringWebアプリケーションで問題がなければ、次の画面が表示されます。
URLを試してください- http://localhost:8080/TestWeb/hello 。SpringWebアプリケーションで問題がなければ、次の画面が表示されます。
*http://localhost:8080/TestWeb/welcome* というURLを試してみてください。SpringWebアプリケーションで問題がなければ、次の画面が表示されます。
*http://localhost:8080/TestWeb/welcome1* というURLを試してみてください。SpringWebアプリケーションで問題がなければ、次の画面が表示されます。