せっかくなのでSpringMVC2.5で書いてみる。


せっかく Spring Framework MVCのプロジェクトがあるのだから、2.5らしい書き方をしてみる。

まずWEB-INFの下にある「dispatcher-servlet」を修正する。

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />
    
        <context:component-scan base-package="shinsan"/>
</beans>


次に/WEB-INF/jsp/index.jspを修正する。

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<html>
    <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <body>
        <form action="result.htm" method="post">
            <input type="text" name="name">
            <input type="submit" value="送信">
        </form>
    </body>
</html>


DreamWeaverなどですぐに修正できるのがjspやhtmlをテンプレートで扱う理由だと思うので、ループや値表示以外にカスタムタグを使うのは好きじゃないので通常のhtmlで。そうでないならVisualWebJSFのようにツールですべてやってくれたほうがいい。

同じところに結果表示用のresult.jspを追加する。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello</title>
    </head>
    <body>
        <h2>Hello ${name}</h2>
    </body>
</html>


そしてコントローラクラスを作成。1つのクラスで複数のURLを作成するというマルチアクションコントローラというやつ。

package shinsan;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloAction {
    
    
    @RequestMapping(value="/index.htm" )
    public String index(ModelMap model){
        return "index";
    }
    
    
    @RequestMapping("/result.htm")
    public String result(@RequestParam(value="name" ) String name , ModelMap model){

        model.addAttribute("name", name);
        
        return "result";
    }
    

}

Spring MVCはやりたいことに対してたくさんの書き方があるのがいいと感じるか、面倒だと感じるかは意見が分かれるかも。

これでindex.htmで入力した値がresult.htmに表示されるというよくあるサンプルは出来上がり…

かと思ったら日本語を入力したら文字化け。UTF8使ってるというのに。いったいいつの時代のフレームワークだよと。


これML版のNetBeansだとこのまま出してしまうのはまずそう。おそらく日本語以外も問題がでまくりだと思うし。


というわけで、なつかしのエンコーディングフィルタを入れる。

org.springframework.web.filter.CharacterEncodingFilter

というのがそれだ。

web.xmlを開いてフィルタのタブを選択する。GUIで入力しよう。

これで文字化けはなくなった。エスケープ等してないので、その辺はちゃんと対応するべし。


ラッピングが薄いのでフレームワーク入門用としては結構優れているかも。