JSP

1月 1, 2003 · Posted in JSP · Comment 

Advertisement


setProperty/getProperty/useBeanアクション

useBean : Bean(一定の基準を満たすJavaのクラス)を利用できるようにする。

<jsp:useBean id="Bean名" scope="有効範囲" class="Beanのクラス名" >

setProperty : Beanの変数に値を設定する。

<jsp:setProperty name="Bean名" property="プロパティ名" value="設定する値" >

getProperty : Beanから値を取得

<jsp:getProperty name="Bean名" property="プロパティ名" >

Beanの基準

  1. デフォルトコンストラクタがある。
  2. プロパティを参照するメソッドを持っている。
  3. パッケージングされている(推奨)

package sample;    // パッケージ

public class SampleBean{
    private int num;    // プロパティ
    public SampleBean(){num = 0;} // デフォルトコンストラクタ
    public void setNum(int n){num = n;}    // プロパティ設定用メソッド
    public int getNum(){return num;}    // プロパティ取得用メソッド
}

プロパティアクセス用メソッド:setXXX getXXX
プロパティがbooleanの場合:setXXX isXXX

setProperty/getProperty/useBeanサンプル

useBean.jsp

<%@ page contentType="text/html; charset=Shift_JIS" %>
<meta http-equiv="Content-Type" content="text/html;charset=Shift_JIS">
<html lang="ja">
<head>
<title>テスト</title>
</head>
<body>
<jsp:useBean id="profile" scope="page" class="model.Profile" />
<h1>useBean, setProperty, getProperty</h1>

<%
    if(request.getParameter("submit") != null){
        String name = request.getParameter"name");
        int age = Integer.parseInt(request.getParameter("age"));
%>
        <%-- 値の設定 --%>
        <jsp:setProperty name="profile" property="name" value="<%= name %>" />
        <jsp:setProperty name="profile" property="age" value="<%= age %>" />
<%
    }
%>
<p>

現在の値<br>
name : <jsp:getProperty name="profile" property="name" />
age :<jsp:getProperty name="profile" property="age" />
</p>

<form method="post" action="./useBean.jsp">
NAME <input type="textbox" name="name"><br>
AGE <input type="textbox" name="age"><br>
<input type="submit" name="submit" value="???M">
</form>

</body>
</html>

Profile.java

package model;

public class Profile{
    private String name;
    private int age;

    public Profile(){
        name = "???";
        age = 0;
    }

    public void setName(String s){
        name = s;
    }

    public String getName(){
        return name;
    }

    public void setAge(int i){
        age = i;
    }

    public int getAge(){
        return age;
    }

}

実行結果サンプル

(略)
現在の値
name : Tom
age :15
(略)

JSP

1月 1, 2003 · Posted in JSP · Comment 

Advertisement


forwardアクション

書式

<jsp:forward page="転送するページ" />

このアクションが実行されると、指定先ページへ転送される。

includeアクションの利用

メイン

<%@ page contentType="text/html; charset=Shift_JIS" %>
<meta http-equiv="Content-Type" content="text/html;charset=Shift_JIS">

<html lang="ja">
<head>
<title>test</title>
</head>
<body>

<%
    String next = request.getParameter("next");

    if(next != null){
        if(next.equals("1")){
%>
            <jsp:forward page="good.jsp" />
<%
        }else if(next.equals("2")){
%>
            <jsp:forward page="bad.jsp" />
<%
        }
    }
%>

<h1>問題</h1>
<p>
1 + 1 = ?<br>
<br>
[1] <a href="./forward.jsp?next=1">1</a><br>
[2] <a href="./forward.jsp?next=2">2</a>
</p>

</body>
</html>

good.jsp

<%@ page contentType="text/html; charset=Shift_JIS" %>
<meta http-equiv="Content-Type" content="text/html;charset=Shift_JIS">

<html lang="ja">
<head>
<title>正解</title>
</head>
<body>

<h1>正解</h1>
<p>
<em>○ 1 + 1 = 2<em><br>
× 1 + 1 = 1<br>
</p>

</body>
</html>

bad.jsp

<%@ page contentType="text/html; charset=Shift_JIS" %>
<meta http-equiv="Content-Type" content="text/html;charset=Shift_JIS">

<html lang="ja">
<head>
<title>不正解</title>
</head>
<body>

<h1>不正解</h1>
<p>
○ 1 + 1 = 2<br>
<em>× 1 + 1 = 1<em><br>
</p>

</body>
</html>


JSP

1月 1, 2003 · Posted in JSP · Comment 

Advertisement


includeアクション

書式

<jsp:include page="" flush="true" />

includeディレクティブとの違い:挿入ファイルの実行結果が挿入される。このため、includeアクションではJSPファイルのみ指定できる。

includeアクションの利用

メイン

<%@ page contentType="text/html; charset=Shift_JIS" %>
<meta http-equiv="Content-Type" content="text/html;charset=Shift_JIS">
<html lang="ja">
<head><title>include</title></head>
<body>

<%-- include --%>
<jsp:include page="./date.jsp" flush="true" />

</body>
</html>

date.jsp

<%@ page import="java.util.*" %>

<% Calendar cal = Calendar.getInstance(); %>

<p>
<%= cal.getTime() %>
</p>

実行結果サンプル

Thu Sep 18 21:55:32 JST 2003

次ページへ »