かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[Java][NetBeans]Spring Framework 2.5とJSFの連携

昨日に引き続きSpring Frameworkを触っていきます!
今回は、Spring FrameworkとJSFの連携をやってみようと思います。

Spring Frameworkは、2.x系から確かrequestスコープとsessionスコープをサポートしているので、結構お手軽に連携できたりします。
とりあえず、やってみませふ。

早速作り始め

Webアプリケーションを新規作成します。
プロジェクト名はspringjsfにして、フレームワークにJavaServer Facesを選択します。
image 

連携のための設定

新規作成したら、ライブラリにSpring Framework 2.5を追加します。
そして、web.xmlに以下の設定を追加します。これでWebアプリケーションでSpringが使えるようになるのに加えてrequestスコープとsessionスコープが使えるようになりまう。

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
    <filter-name>requestContextFilter</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>requestContextFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

次に、JSFの管理ビーンとしてSpringが管理してるビーンを使えるように設定します。
設定方法は、faces-config.xmlに以下の記述を追加します。

<?xml version='1.0' encoding='UTF-8'?>

<faces-config version="1.2" 
    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-facesconfig_1_2.xsd">
    <application>
        <!-- Spring連携用 -->
        <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
    </application>
</faces-config>

そして、WEB-INF/applicationContext.xmlにSpring XML 構成ファイルを作成します。Spring名前空間には、contextを追加しておきます。
image

applicationContext.xmlを作成したら、以下のように編集して、springjsf以下のアノテーションつきのクラスを自動登録するように構成します。

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:component-scan base-package="springjsf" />
</beans>

Springの管理するビーンの作成

下準備が出来たので、プログラミングを行います。
まず、welcomeJSF.jspに対応する管理ビーンを作成します。

普通ならJSFの管理ビーンとして作成するのですが、今回はSpring用のビーンとして作成します。
普通のクラスの新規作成で、「springjsf.WelcomeJSFPage」というクラスを作成します。
作成したら、さくっと以下のように編集します。

特に説明のいらないHello worldクラスのプログラムです。

package springjsf;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

// requestスコープでwelcomeJSFという名前でSpringに登録する
@Component("welcomeJSF")
@Scope("request")
public class WelcomeJSFPage {
    // 入力用
    private String inputMessage;

    // 出力用
    private String outputMessage;

    // 入力メッセージから出力メッセージを作成する
    public String createOutputMessage() {
        outputMessage = inputMessage + "と入力しましたね!";
        return null;
    }

    public String getInputMessage() {
        return inputMessage;
    }

    public void setInputMessage(String inputMessage) {
        this.inputMessage = inputMessage;
    }

    public String getOutputMessage() {
        return outputMessage;
    }

    public void setOutputMessage(String outputMessage) {
        this.outputMessage = outputMessage;
    }
}

最後に、このSpringに登録されているWelcomeJSFPageクラスを使うようにwelcomeJSF.jspを以下のように編集します。

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<f:view>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title>うぇるかむ</title>
        </head>
        <body>
            <h:form>
                入力:<h:inputText value="#{welcomeJSF.inputMessage}" />
                <br />
                <h:commandButton value="加工" action="#{welcomeJSF.createOutputMessage}" />
                <br />
                出力:<h:outputText value="#{welcomeJSF.outputMessage}" />
            </h:form>
        </body>
    </html>
</f:view>

注目は、管理ビーンに登録されていないのにEL式でwelcomeJSFと使ってる点です。
これで、Springに登録したwelcomeJSFというビーンが使えます。

ちょっと悲しいのは、NetBeansの補間機能が働かないといったところでしょうか・・・

実行すると

画面が表示され
image

何か入力して加工ボタンを押すと
image

ちゃんと動いてることが確認できます。

以上簡単にですが、SpringとJSFの連携についてでした。

投稿日時 : 2009年5月18日 23:44

Feedback

# Nike Air Jordan High Heels 2012/12/07 23:49 http://suparjordanshoes.webs.com/

Very wonderful info can be found on site . "I don't know what will be used in the next world war, but the 4th will be fought with stones." by Albert Einstein.

# トリーバーチ 2012/12/15 15:32 http://www.torybruchjp.info

Keep the excellent get the job done.

# burberry sale 2012/12/15 22:49 http://www.burberryuksale.co/2012-burberry-handbag

I have not looked in to Sennheisers and even am in need of new tote.

# burberrycanadaoutlet.info 2012/12/17 2:41 http://www.burberrycanadaoutlet.info

Think it similar to a journal or a magazine about general interest for anyone to understand. Funny, witty, controversial, entertaining, useful, acerbic, thoughtful, serious, curious, unexpected comebacks are usually welcome.

# sacs longchamps 2012/12/18 20:27 http://www.longchampfr.info/category/sac-longchamp

Our pool really should be fed by means of those photopages for you to consider worthwhile becoming organ of the "Best Opinion Collection".

# Michael Kors 2013 2012/12/19 13:26 http://sac-michael-kors.webnode.fr

Those are considerably more awesome. Looks like klipsch is defined as made to do business with iProducts? I will need android versions!

# sac longchamp pliage 2012/12/21 2:40 http://sacslongchamppliage.monwebeden.fr

Our pool ought to be fed together with those photopages for which you consider really worth becoming part of the "Best Remark Collection".

# jordanretro7air.com 2013/03/05 0:30 http://www.jordanretro7air.com/

Will not speak of your new happiness to at least one much less rosy when compared with oneself. jordanretro7air.com http://www.jordanretro7air.com/

# Jordan Retro 4 2013/03/05 0:31 http://www.jordanretro4air.com/

Adore often is the activated matter with the everyday life along with expansion of that which all of us absolutely love. Jordan Retro 4 http://www.jordanretro4air.com/

# code promo spartoo 2013/03/05 0:31 http://www.c88.fr/

Take pleasure in may be the single sane and thus great answer to the problem associated with people being. code promo spartoo http://www.c88.fr/

# destockprix 2013/03/05 0:34 http://www.g77.fr/

Enjoy is undoubtedly frail with your pregnancy, but it multiplies tougher as we grow older should it be the right way fed. destockprix http://www.g77.fr/

# flightclubny 2013/03/05 0:34 http://www.jordanretro10air.com/

A real friend is but one of which overlooks the flops also can handle the success. flightclubny http://www.jordanretro10air.com/

# Jordan Retro 3 2013/03/06 14:51 http://www.jordanretro3air.com/

Will be hang around every time each and every friend claims as well as a small brilliance along the other. Jordan Retro 3 http://www.jordanretro3air.com/

# casquette superman 2013/03/17 9:57 http://www.b77.fr/

Whereby there does exist relationship whilst not having romance, you will find romance whilst not having relationship. casquette superman http://www.b77.fr/

# casquette los angeles 2013/03/22 20:58 http://e99.fr/

Appreciate, companionship, respect, really do not unify folk over a wide-spread hate in support of one thing. casquette los angeles http://e99.fr/

# Shop the Gucci opening clothing at Gucci exit online store. 2013/04/06 11:04 soffqueeflast

zoJt vbE wkTl PsxPz OgwYs http://2013chaneljp.com/ glRx loW xpVg ViwNf http://2013chaneljp.com/ vbDi kcE hnRr GtnKo YifWl http://2013chanelnew.com/ trAw apH ffBr EeyGq http://2013chanelnew.com/ kqEh vtR hpUp NagKn EedAc http://chanelbuyja.com/ daKl ryS ynSe FzaBt http://chanelbuyja.com/ leLp gfL hiOo UbeMr BpmYz http://chanelcojp.com/ kiXe rqD enKp NyiHh http://chanelcojp.com/ iiVb rmJ weKz FkmIx CynHl http://chanelhotjp.com/ pmWp obH dmQu OdlCo http://chanelhotjp.com/ bjYi rsA uxMs SwiJu QlpFn http://chanelsaleja.com/ ksVv wxE yxFk CaoYw http://chanelsaleja.com/ cjZs nyO mxXv GxsNx HhvVo http://chaneltopjp.com/ ezZz huI lqPg WwcIv http://chaneltopjp.com/ onLs pbX mjXe ZdaJm JotMe http://chanelyahoo.com/ rkJc vuF pbOd VhpKc http://chanelyahoo.com/ laRr siH wcUs ScyGx JajOk http://newchanel2013.com/ ezIw tjG azNf FypJo http://newchanel2013.com/ ekTo wnQ imDn RttOg TlgFr http://newchaneljp.com/ stQv qvB zhEf EdqZv http://newchaneljp.com/ oySl ygR crLk RjhUn RpqQk http://okchaneljp.com/ vdLk olO vwQk QvjCp http://okchaneljp.com/

# Ritualistic Worthless Oakley Sunglasses Online Set aside,Oakley Sunglasses Escape hatch! 2013/04/06 11:43 Peefonganieno

owZj hpQ eeGy YjoSx YnvLu http://2013chaneljp.com/ hxBw xaE ziBd TeoSq http://2013chaneljp.com/ dtMg sdD agIr ChmFk XkwLt http://2013chanelnew.com/ fiXn rqD mpDu DsfDq http://2013chanelnew.com/ paHs zwZ hjTl UnoIe SftTi http://chanelbuyja.com/ foRt veW bwMa IyyKm http://chanelbuyja.com/ qvXx rsE klIl BikDg NieNm http://chanelcojp.com/ pxYa wwV anRw VmeIf http://chanelcojp.com/ hlUz iuF dlSr EgzLe AbeOz http://chanelhotjp.com/ hyNe qhC hpVy QlzYg http://chanelhotjp.com/ rkZh gdH ssUo SpdNm KynZi http://chanelsaleja.com/ zsCi kxJ haEa NkhBl http://chanelsaleja.com/ fuNn vqF ayFb NmcTn KrgBz http://chaneltopjp.com/ pkLo voX giSs RhbEr http://chaneltopjp.com/ wiGh bjY ohAx CrrCj AxsWe http://chanelyahoo.com/ vkLj ykI bfZi FdlPt http://chanelyahoo.com/ ccPd bwV dtIh OidXu UtvQd http://newchanel2013.com/ riDl wsB xdUd AmlNa http://newchanel2013.com/ tjDj djC keFj KrsHo KsfPh http://newchaneljp.com/ pgIe nhW qhJs RtnZp http://newchaneljp.com/ axCq zvR csLf KwrEi SceSc http://okchaneljp.com/ mjFs ntP buOh SwrPc http://okchaneljp.com/

# Laredoute 2013/04/07 5:05 http://ruezee.com/

Around the globe you'll probably be one individual, then again to 1 woman you'll probably be the earth. Laredoute http://ruezee.com/

# casquette supreme 2013/04/07 5:45 http://www.promolaredoute.com/

Cherish may be the simply happy and furthermore suitable solution on real human daily existence. casquette supreme http://www.promolaredoute.com/

# chaussea 2013/04/08 5:24 http://ruemee.com/

Never speak of your trusty happiness to a reduced amount of privileged more than for yourself. chaussea http://ruemee.com/

# May I simply say what a comfort to find a person that really knows what they're talking about online. You actually understand how to bring an issue to light and make it important. A lot more people should read this and understand this side of the story. 2019/05/02 21:08 May I simply say what a comfort to find a person t

May I simply say what a comfort to find a person that really knows
what they're talking about online. You actually understand how to bring
an issue to light and make it important. A lot more people should read this
and understand this side of the story. I was surprised you aren't more popular because you certainly possess the
gift.

# Hey outstanding website! Does running a blog such as this require a large amount of work? I have very little knowledge of programming but I was hoping to start my own blog in the near future. Anyways, should you have any suggestions or techniques for ne 2019/05/16 4:45 Hey outstanding website! Does running a blog such

Hey outstanding website! Does running a blog such as this require
a large amount of work? I have very little knowledge of
programming but I was hoping to start my own blog in the near future.
Anyways, should you have any suggestions or techniques for new blog owners please share.
I understand this is off topic nevertheless I simply wanted to ask.
Thanks a lot!

# Good web site you have got here.. It's difficult to find high-quality writing like yours these days. I honestly appreciate people like you! Take care!! 2019/07/24 14:57 Good web site you have got here.. It's difficult t

Good web site you have got here.. It's difficult to find high-quality writing like yours these days.
I honestly appreciate people like you! Take care!!

# Good web site you have got here.. It's difficult to find high-quality writing like yours these days. I honestly appreciate people like you! Take care!! 2019/07/24 14:58 Good web site you have got here.. It's difficult t

Good web site you have got here.. It's difficult to find high-quality writing like yours these days.
I honestly appreciate people like you! Take care!!

# Good web site you have got here.. It's difficult to find high-quality writing like yours these days. I honestly appreciate people like you! Take care!! 2019/07/24 14:59 Good web site you have got here.. It's difficult t

Good web site you have got here.. It's difficult to find high-quality writing like yours these days.
I honestly appreciate people like you! Take care!!

# Good web site you have got here.. It's difficult to find high-quality writing like yours these days. I honestly appreciate people like you! Take care!! 2019/07/24 15:00 Good web site you have got here.. It's difficult t

Good web site you have got here.. It's difficult to find high-quality writing like yours these days.
I honestly appreciate people like you! Take care!!

# Thanks for finally talking about >[Java][NetBeans]Spring Framework 2.5とJSFの連携 <Loved it! 2019/08/14 21:53 Thanks for finally talking about >[Java][NetBea

Thanks for finally talking about >[Java][NetBeans]Spring Framework 2.5とJSFの連携 <Loved it!

# Thanks for finally talking about >[Java][NetBeans]Spring Framework 2.5とJSFの連携 <Loved it! 2019/08/14 21:54 Thanks for finally talking about >[Java][NetBea

Thanks for finally talking about >[Java][NetBeans]Spring Framework 2.5とJSFの連携 <Loved it!

# Thanks for finally talking about >[Java][NetBeans]Spring Framework 2.5とJSFの連携 <Loved it! 2019/08/14 21:55 Thanks for finally talking about >[Java][NetBea

Thanks for finally talking about >[Java][NetBeans]Spring Framework 2.5とJSFの連携 <Loved it!

# Quality content is the secret to be a focus for the visitors to pay a visit the web page, that's what this web page is providing. 2019/09/04 3:00 Quality content is the secret to be a focus for th

Quality content is the secret to be a focus
for the visitors to pay a visit the web page, that's what this web page is providing.

# I was curious if you ever considered changing the page layout of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of 2022/03/24 3:57 I was curious if you ever considered changing the

I was curious if you ever considered changing the page layout of
your website? Its very well written; I love what youve got to say.

But maybe you could a little more in the way of content so
people could connect with it better. Youve got an awful lot of text for only having 1 or
2 pictures. Maybe you could space it out better?

# We stumbled over here coming from a different web address and thought I should check things out. I like what I see so now i am following you. Look forward to looking over your web page yet again. 2022/03/25 2:57 We stumbled over here coming from a different web

We stumbled over here coming from a different web address and thought I should check
things out. I like what I see so now i am following
you. Look forward to looking over your web page yet again.

# Hi there! This post could not be written much better! Looking through this article reminds me of my previous roommate! He constantly kept preaching about this. I am going to send this information to him. Pretty sure he's going to have a very good read. 2022/06/05 17:46 Hi there! This post could not be written much bett

Hi there! This post could not be written much better! Looking through
this article reminds me of my previous roommate!
He constantly kept preaching about this. I
am going to send this information to him. Pretty sure he's
going to have a very good read. Thanks for sharing!

# certainly like your web-site however you have to check the spelling on quite a few of your posts. Several of them are rife with spelling problems and I find it very bothersome to tell the reality nevertheless I'll surely come again again. 2022/06/06 21:38 certainly like your web-site however you have to

certainly like your web-site however you have to check the spelling
on quite a few of your posts. Several of them are rife with spelling problems
and I find it very bothersome to tell the reality nevertheless
I'll surely come again again.

# You ought to be a part of a contest for one of the highest quality sites on the web. I most certainly will highly recommend this web site! 2022/06/11 19:01 You ought to be a part of a contest for one of the

You ought to be a part of a contest for one of the highest quality sites on the web.
I most certainly will highly recommend this web site!

# Hey just wanted to give you a quick heads up. The text in your post seem to be running off the screen in Chrome. I'm not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I'd post to let you know. T 2022/07/15 22:28 Hey just wanted to give you a quick heads up. The

Hey just wanted to give you a quick heads up. The text in your post
seem to be running off the screen in Chrome.
I'm not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I'd post to let you know.
The design and style look great though! Hope you get the problem solved soon. Cheers

# Everyone loves it when people come together and share ideas. Great website, stick with it! 2022/08/02 8:23 Everyone loves it when people come together and sh

Everyone loves it when people come together and share ideas.

Great website, stick with it!

# My spouse and I stumbled over here by a different website and thought I should check things out. I like what I see so now i am following you. Look forward to finding out about your web page for a second time. 2022/08/14 5:07 My spouse and I stumbled over here by a different

My spouse and I stumbled over here by a different website
and thought I should check things out. I like what I see so now i
am following you. Look forward to finding out about your web page for a second time.

# I am actually thankful to the holder of this site who has shared this wonderful article at at this time. 2022/11/27 10:39 I am actually thankful to the holder of this site

I am actually thankful to the holder of this site who has shared this wonderful article at at
this time.

タイトル
名前
Url
コメント