Skip to content

Instantly share code, notes, and snippets.

@siguremon
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siguremon/9350050 to your computer and use it in GitHub Desktop.
Save siguremon/9350050 to your computer and use it in GitHub Desktop.
Spring3のSpring MVCでなるべく最小単位のHello world ref: http://qiita.com/siguremon/items/84c831391a6204079fd2
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- (1) -->
<context:component-scan base-package="controller" />
<!-- (2) -->
<mvc:annotation-driven />
<!-- (3) -->
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
<!-- (4) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
SpringTodo
|-pom.xml
|-src
|-main
| |-java
| | |-controller
| | |-HelloWorldController.java -- Controllerクラス
| |-resources
| | |-META-INF -- Spring周りの設定ファイルおよびリソースファイルを配置するフォルダ
| | |-spring
| | |-beans-webmvc.xml -- Spring MVCのBean定義ファイル
| |-webapp -- Web Application周りのファイルを配置するフォルダ
| |-WEB-INF
| |-web.xml
| |-resources -- 静的リソースを配置するフォルダ
| |-views -- jspなどのviewを配置するフォルダ
| |-helloworld.jsp
|-test
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<h1>hello world</h1>
package controller;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
// (1)
@Controller
public class HelloWorldController {
// (2)
@RequestMapping(value = "/", method = GET)
public String home() {
// (3)
return "helloworld";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd
">
<display-name>spring-todo</display-name>
<!-- (1) -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/META-INF/spring/beans-webmvc.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- (2) -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment