5月 11th, 2008at 0:15
Tags: Java
JSF1.1でログインページを作ってみる
BackingBeanとは、JSPと連動してくれるJavaBeansのこと。
以下、ログインページを作ってみながら説明。
- ユーザIDとパスワードを入力して文字列が正しければ成功画面へ遷移する
- 入力した内容に間違いがある場合は同じ画面に遷移する
Loginクラスの作成
JavaBeanには表示されるWEBページの
- ユーザID入力欄
- パスワード入力欄
に対応したアクセッサ(Getter、Setter)を作成する。
また、ログインボタンを押した時に実行するメソッドも作成する。
package com.daipresents.web.login;
import javax.faces.context.ExternalContext;import javax.faces.context.FacesContext;import javax.servlet.http.HttpSession;
import com.daipresents.web.session.SessionConst;
public class Login {
private static final String RETURN_OK = "OK";
private static final String RETURN_NG = "NG";
/** ユーザID */ private String userID; /** パスワード */ private String password;
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getUserID() { return userID; }
public void setUserID(String userID) { this.userID = userID; }
public String login(){
FacesContext context = FacesContext.getCurrentInstance(); ExternalContext externalContext = context.getExternalContext(); HttpSession session = (HttpSession)externalContext.getSession(true);
if("test".equals(this.userID)){ if("aaaa".equals(this.password )){ session.setAttribute(SessionConst.IS_LOGIN, Boolean.TRUE); return RETURN_OK; } }
session.setAttribute(SessionConst.IS_LOGIN, Boolean.FALSE);
return RETURN_NG;
}
}
loginメソッドでは、ユーザ名とパスワードの比較を実行し、マッチすればSessionオブジェクトに「IS_LOGIN」という名前の値に「True」を設定している。これによってユーザのログイン状態を確認できるようにする。
作られたJavaBeanはServletのようにRequestやResponseを引数で受け取っていないため使うことができない。なのでSessionオブジェクトは以下のようにして取得している。JSFを使う場合はこういう作法になるのかもかも。
FacesContext context = FacesContext.getCurrentInstance(); ExternalContext externalContext = context.getExternalContext(); HttpSession session = (HttpSession)externalContext.getSession(true);
JSPの作成
今回作るのは「ログインページ」と「成功ページ」のみ。「成功ページ」はWEBAPのルートにある「index.jsp」を使うことにするので、実質1枚作成する。
<%@ page contentType="text/html; charset=UTF-8" %><%@ page import="java.util.*" %><%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %><%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view> <h:form> <h:panelGrid columns="2">
<h:outputText value="ユーザID" /> <h:inputText value="#{Login.userID}" />
<h:outputText value="パスワード" /> <h:inputSecret value="#{Login.password}" />
<h:commandButton action="#{Login.login}" value="ログイン" />
</h:panelGrid> </h:form></f:view>
JSFのタグに作ったJavaBeanのアクセッサをひも付ける感じだ。Bottonのaction属性にメソッドを関連付けているのもわかる。loginメソッドの戻り値は「OK」か「NG」なので、その文字列がaction属性にわたされfaces-config.xmlに設定した動きをする。
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<navigation-rule> <from-view-id>/login/login.jsp</from-view-id> <navigation-case> <from-outcome>OK</from-outcome> <to-view-id>/index.jsp</to-view-id> </navigation-case> <navigation-case> <from-outcome>NG</from-outcome> <to-view-id>/login/login.jsp</to-view-id> </navigation-case> </navigation-rule>
<managed-bean> <managed-bean-name>Login</managed-bean-name> <managed-bean-class>com.daipresents.web.login.Login</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean>
</faces-config>
loginメソッドから帰ってきた「OK」、「NG」という文字に対しての遷移情報を記載する。「to-view-id」タグがそれにあたるが、ここにはWEBAPのコンテキストを除いたパスを入力しないといけないっぽい。
I'm a software engineer who like travel to island in Japanese. Recently I am enjoying agile manager, coach, product owner for my work. The person grows like that.








