Skip to content

Instantly share code, notes, and snippets.

@shimarin
Created September 4, 2010 15:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save shimarin/565275 to your computer and use it in GitHub Desktop.
Spring-MVCとVelocityを使った Twitterライクに REST風な URLハンドリングをするサイトのテンプレート
src/
net.stbbs.twitterlike.web/
RequestHandler.java
war/
WEB-INF/
lib/
org.springframework.asm-x.y.z-RELEASE.jar
org.springframework.beans-x.y.z-RELEASE.jar
org.springframework.context-x.y.x-RELEASE.jar
org.springframework.context.support-x.y.x-RELEASE.jar
org.springframework.core-x.y.z-RELEASE.jar
org.springframework.expression-x.y.z-RELEASE.jar
org.springframework.web-x.y.z-RELEASE.jar
org.springframework.web.servlet-x.y.z-RELEASE.jar
velocity-x.y.z.jar
commons-logging-x.y.z.jar
commons-collections-x.y.z.jar
commons-lang-x.y.z.jar
velocity/
index.vm
person.vm
status.vm
mvc-servlet.xml
web.xml
build.xml
<?xml version="1.0"?>
<project default="war">
<target name="war">
<jar basedir="war" jarfile="ROOT.war"/>
</target>
</project>
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="net.stbbs.twitterlike.web"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".vm"/>
</bean>
</beans>
package net.stbbs.twitterlike.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class RequestHandler {
@RequestMapping(value="", method = RequestMethod.GET)
public String index()
{
return "index";
}
@RequestMapping(value="{screenName}", method = RequestMethod.GET)
public String person(@PathVariable String screenName, ModelMap model)
{
model.put("screenName", screenName);
return "person";
}
@RequestMapping(value="{screenName}/status/{statusId}", method = RequestMethod.GET)
public String status(@PathVariable String screenName, @PathVariable int statusId, ModelMap model)
{
model.put("screenName", screenName);
model.put("statusId", statusId);
return "status";
}
}
<html>
<body>
screenName: $screenName<br/>
statusId: $statusId<br/>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
@shimarin
Copy link
Author

shimarin commented Sep 4, 2010

mvc:default-servlet-handler/ を登録してあると、リクエストハンドラで処理されなかったコンテンツを 404にする代わりにアプリケーションサーバのデフォルトサーブレットハンドラで処理させようとしてくれる。但し、デフォルトサーブレットハンドラはアプリケーションサーバによって異なるため Spring-MVCがアプリケーションサーバの種別を自動で認識して適切ななものを呼び出すようになっている。Spring-MVCが自動認識できないアプリケーションサーバを使っている場合は、適切なパラメータを渡してやる必要がある。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment