JSP基本_EL式
1、EL式
下記二種類がある。
① ${式} : JSPの出力(レンダリング)時に評価 (JSP2.0から)
② #{式} : タグハンドラにより任意のタイミングで評価 (JSP2.1から)
2、オブジェクトへのアクセス
・オブジェクトへ: ${オブジェクト名}
・オブジェクトのプロパティへ: ${オブジェクト名.プロパティ名} また、${オブジェクト名.プロパティ1.プロパティ2}もできる
・listへ: ${list[インデックス]}
・mapへ: ${map[キー]} 或は ${map.キー}
3、演算子
演算子一覧(※演算子の括弧内は、別名)
| 分類 | 演算子 | 説明 |
| 算術 | + | 加算 |
| - | 減算 | |
| * | 乗算 | |
| / (div) | 除算 | |
| % (mod) | 剰余 | |
| 比較 | == (eq) | 等しい |
| != (ne) | 等しくない | |
| < (lt) | 小さい | |
| > (gt) | 大きい | |
| <= (le) | 以下 | |
| >= (ge) | 以上 | |
| empty | nullまたは空文字 | |
| 論理 | && (and) | 集合積 |
| || (or) | 集合和 | |
| ! (not) | 否定 | |
| 2項 | a ? b : c | 「a」の場合は「b」、「a」以外の場合は「c」 |
4、暗黙オブジェクト
| 暗黙オブジェクト | 内容 | 例 | JSP式相当 |
| pageScope | ページスコープ | ${pageScope.name} |
<%= pageContext.getAttribute("name") %> |
| requestScope | リクエストスコープ(リクエストアトリビュート) | ${requestScope.name} |
<%= request.getAttribute("name") %> |
| sessionScope | セッションスコープ | ${sessionScope.name} |
<%= session.getAttribute("name") %> |
| applicationScope | アプリケーションスコープ | ${applicationScope.name} |
<%= application.getAttribute("name") %> |
| pageContext | PageContext | ||
| param | リクエストパラメーター(URIのクエリー文字列) | ${param.name} |
<%= request.getParameter("name") %> |
| paramValues | リクエストパラメーター(配列で取得) | ${paramValues.name[0]} |
<%= request.getParameterValues("name")[0] %> |
| header | リクエストヘッダー | ${header["user-agent"]} |
<%= request.getHeader("user-agent") %> |
| headerValues | リクエストヘッダー(配列で取得) | ${headerValues["user-agent"][0]} |
|
| cookie | クッキー | ${cookie.key.name} |
<% for (Cookie c : request.getCookies())
{ |
| initParam | web.xmlに指定されている初期化パラメーター
<web-app ~> |
${initParam.name} |
<%= pageContext.getServletContext() |
5、予約語
EL式の予約語
and or div eq ge gt lt lt ne true false
instanceof mod not null empty
6、EL式をオフする
1)pageディレクティブでオフ
<%@page isELIgnored="true"%>
2)web.xmlの<jsp-config>で一括で複数のJSP上オフ
<jsp-config>
<jsp-property-group>
<url-pattern/*<url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
浙公网安备 33010602011771号