Top > MyPage
 

Spring MVC(1)

最近AOPやらDIという言葉をよく耳にしますし、最近Spring in Actionという本を買って 読んだのですが(ほんのつい最近「開発者ノートシリーズ Spring」も買った)、いつものようにすぐ忘れちゃうので、ここにメモを取っておこうと思います。

まずはmavenプロジェクト作成

  1. maven genapp

    まずはお決まりの「maven genapp」で、genappのテンプレート一覧で、どれか迷ったけれど、 今回はstrutsを使う予定がないので、よくわかりませんが、「web」を選んでみた。

  2. project.propertiesなどが出来ており、徐々に変えていかなければいけないでしょう。しかし、project.xmlは以前使ったのを少々変更して、 使って、不具合があったらそのたびに修正することにします。。
    <?xml version="1.0" encoding="UTF-8"?>
    <project>
        <!-- the version of maven's project object model -->
        <pomVersion>3</pomVersion>
        <!-- a unique name for this project -->
        <id>kakeibo</id>
        <groupId>chikkun</groupId>
        <!-- a short but descriptive name for the project -->
        <name>cKakeibo</name>
        <!-- The version of the project under development, e.g.
           1.1, 1.2, 2.0-SNAPSHOT -->
        <currentVersion>1.0</currentVersion>
        <!-- the year the project started -->
        <inceptionYear>2005</inceptionYear>
        <package>com.chikkun.ckakeibo.*</package>
        <logo>/kakeibo.gif</logo>
        <description>webでkakeibo</description>
        <!-- a short description of what the project does -->
        <shortDescription>Money Management</shortDescription>
        <!-- the project home page -->
        <url>http://maven.apache.org/reference/plugins/examples/</url>
        <issueTrackingUrl>http://nagoya.apache.org/scarab/servlet/scarab/</issueTrackingUrl>
        <siteAddress>www.chikkun.com</siteAddress>
        <siteDirectory>/home/httpd/html/und/kakeibo</siteDirectory>
        <distributionDirectory>/home/httpd/html/und/kakeibo/builds/</distributionDirectory>
        <repository>
            <connection>scm:cvs:pserver:chikkun@www.chikkun.com:/home/und:ckakeibo</connection>
            <password>kazukun</password>
        </repository>
        <!-- any mailing lists for the project -->
        <mailingLists/>
        <!-- who the developers are for the project -->
        <developers/>
        <!-- jar files the project is dependent on -->
        <licenses>
            <license>
                <name>Chikkun Digitals </name>
                <url>http://www.chikkun.com </url>
                <distribution/>
                <comments/>
            </license>
        </licenses>
        <!-- details about the organization that 'owns' the project -->
        <organization>
            <name>Chikkun Digitals</name>
            <url>http://www.chikkun.com/</url>
            <logo>/penguin.gif</logo>
        </organization>
        <dependencies>
            <dependency>
                <id>junit</id>
                <version>3.8.1</version>
            </dependency>
            <dependency>
                <id>servletapi</id>
                <version>2.3</version>
            </dependency>
            <dependency>
                <groupId>cactus</groupId>
                <artifactId>cactus</artifactId>
                <version>13-1.7</version>
            </dependency>
            <dependency>
                <groupId>cactus</groupId>
                <artifactId>cactus-maven</artifactId>
                <version>1.7</version>
                <type>plugin</type>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.0.4</version>
                <properties>
                    <war.bundle>true</war.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>velocity</groupId>
                <artifactId>velocity</artifactId>
                <version>1.5-dev</version>
                <properties>
                    <war.bundle>true</war.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>hsqldb</groupId>
                <artifactId>hsqldb</artifactId>
                <version>1.7.3.3</version>
                <properties>
                    <war.bundle>true</war.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>hibernate</groupId>
                <artifactId>hibernate</artifactId>
                <version>2.1.8</version>
                <properties>
                    <war.bundle>true</war.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>hibernate</groupId>
                <artifactId>hibernate-tools</artifactId>
                <version>2.1.3</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>oro</groupId>
                <artifactId>oro</artifactId>
                <version>2.0.8</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>ehcache</groupId>
                <artifactId>ehcache</artifactId>
                <version>1.1</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.1</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>asm</groupId>
                <artifactId>asm</artifactId>
                <version>1.5.3</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>xdoclet</groupId>
                <artifactId>xdoclet-hibernate-module</artifactId>
                <version>1.2b4</version>
            </dependency>
            <dependency>
                <groupId>velocity-tools</groupId>
                <artifactId>velocity-tools</artifactId>
                <version>1.1</version>
                <properties>
                    <war.bundle>true</war.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>commons-validator</groupId>
                <artifactId>commons-validator</artifactId>
                <version>1.1.4</version>
                <properties>
                    <cactus.bundle>true</cactus.bundle>
                    <war.bundle>true</war.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils</artifactId>
                <version>1.6</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>commons-digester</groupId>
                <artifactId>commons-digester</artifactId>
                <version>1.6</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>3.1</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.0</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>1.2</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>commons-pool</groupId>
                <artifactId>commons-pool</artifactId>
                <version>1.2</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <!-- XDoclet dependencies -->
            <dependency>
                <groupId>xdoclet</groupId>
                <id>xdoclet</id>
                <version>1.2b4</version>
            </dependency>
            <dependency>
                <groupId>xdoclet</groupId>
                <artifactId>xdoclet-web-module</artifactId>
                <version>1.2b4</version>
            </dependency>
            <dependency>
                <groupId>xdoclet</groupId>
                <artifactId>xdoclet-ejb-module</artifactId>
                <version>1.2b4</version>
            </dependency>
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.8</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>xdoclet</groupId>
                <artifactId>xjavadoc</artifactId>
                <version>1.0.2</version>
            </dependency>
            <dependency>
                <groupId>xdoclet</groupId>
                <artifactId>maven-xdoclet-plugin</artifactId>
                <version>1.2b4</version>
                <type>plugin</type>
            </dependency>
            <!-- HttpUnit dependencies -->
            <dependency>
                <groupId>httpunit</groupId>
                <artifactId>httpunit</artifactId>
                <version>1.5.4</version>
                <properties>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>jtidy</groupId>
                <artifactId>jtidy</artifactId>
                <version>4aug2000r7-dev</version>
                <properties>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>jaxen</groupId>
                <artifactId>jaxen</artifactId>
                <version>1.1-beta-6</version>
                <properties>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>exml</groupId>
                <artifactId>exml</artifactId>
                <version>7.0</version>
                <properties>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>dbunit</groupId>
                <artifactId>dbunit</artifactId>
                <version>2.1</version>
                <properties>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>dtdparser</groupId>&gt; <artifactId>dtdparser</artifactId>&gt; <version>1.21</version>
                <properties>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>rhino</groupId>&gt; <artifactId>js</artifactId>&gt; <version>1.5R4.1</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>checkstyle</groupId>
                <artifactId>checkstyle-optional</artifactId>
                <version>3.4</version>
            </dependency>
            <dependency>
                <groupId>odmg</groupId>
                <artifactId>odmg</artifactId>
                <version>3.0</version>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
            <dependency>
                <groupId>springframework</groupId>
                <artifactId>spring</artifactId>
                <version>1.2.1</version>
                <type>jar</type>
                <properties>
                    <war.bundle>true</war.bundle>
                    <cactus.bundle>true</cactus.bundle>
                </properties>
            </dependency>
        </dependencies>
        <build>
            <nagEmailAddress>chikkun@chikkun.com</nagEmailAddress>
            <sourceDirectory>src/java</sourceDirectory>
            <unitTestSourceDirectory>src/test</unitTestSourceDirectory>
            <unitTest>
                <includes>
                    <include>**/*Test.java</include>
                    <include>**/Test*.java</include>
                </includes>
                <excludes>
                    <exclude>**/NaughtyTest.java</exclude>
                </excludes>
            </unitTest>
            <resources>
                <resource>
                    <directory>src/conf</directory>
                    <includes>
                        <include>*.properties</include>
                    </includes>
                </resource>
            </resources>
        </build>
        <reports>
            <report>maven-junit-report-plugin</report>
            <report>maven-cactus-plugin</report>
            <report>maven-javadoc-plugin</report>
            <report>maven-checkstyle-plugin</report>
            <report>maven-jdepend-plugin</report>
            <report>maven-linkcheck-plugin</report>
            <report>maven-pmd-plugin</report>
            <report>maven-license-plugin</report>
            <report>maven-tasklist-plugin</report>
        </reports>
    </project>
                        
  3. そして、maven eclipseでeclipseのプロジェクトを作成して、eclipseでインポートします。

Hello World

お決まりの「Hello World」でテストしてみよう

  1. web.xml
    <?xml version="1.0" encoding="Windows-31J"?>
    
    <!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>
        <display-name>cKakeibo</display-name>
        <description> Kakeibo System created by Chikkun</description>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <filter>
            <filter-name>Set Character Encoding</filter-name>
            <description>character encoding</description>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>Windows-31J</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>Set Character Encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <listener>
            <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <servlet>
            <servlet-name>kakeibo</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet>
            <servlet-name>velocity</servlet-name>
            <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
            <init-param>
                <param-name>org.apache.velocity.toolbox</param-name>
                <param-value>/WEB-INF/toolbox.xml</param-value>
            </init-param>
            <init-param>
                <param-name>org.apache.velocity.properties</param-name>
                <param-value>/WEB-INF/velocity.properties</param-value>
            </init-param>
            <load-on-startup>10</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>kakeibo</servlet-name>
            <url-pattern>*.html</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>velocity</servlet-name>
            <url-pattern>*.vm</url-pattern>
        </servlet-mapping>
        <welcome-file-list>
            <welcome-file>index.vm</welcome-file>
        </welcome-file-list>
    </web-app>
    
                        
  2. Controllerの作成(HelloWorldController)
    
    /*
     * 作成日: 2005/08/03
     *
     */
    package com.chikkun.kakeibo;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    public class HelloWorldController implements Controller{
    
      private String greeting;
      
      public void setGreeting(String greeting) {
        this.greeting = greeting;
      }
    
    
      public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("helloWorld", "message", greeting);
      }
    }
    
                        
  3. configure kakeibo-servlet.xml(for velocity, HelloWorldController)
    
    <?xml version="1.0" encoding="Windows-31J"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
       "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
        <bean name="/helloWorld.html" class="com.chikkun.kakeibo.HelloWorldController">
            <property name="greeting">
                <value>Hello World, My First Experience with Spring!!</value>
            </property>
        </bean>
        <bean id="velocityConfigurer"
            class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
            <property name="resourceLoaderPath">
                <value>WEB-INF/templates/</value>
            </property>
            <property name="velocityProperties">
                <props>
                    <prop key="directive.foreach.counter.name">loopCoounter</prop>
                    <prop key="directive.foreach.counter.initial.value">0</prop>
                </props>
            </property>
        </bean>
        <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
            <property name="suffix">
                <value>.vm</value>
            </property>
            <property name="exposeRequestAttributes">
                <value>true</value>
            </property>
            <property name="exposeSessionAttributes">
                <value>true</value>
            </property>
        </bean>
    </beans>