なみひらブログ

学んだことを日々記録する。~ since 2012/06/24 ~

coreライブラリを利用するための準備

以下のように、jspでcoreライブラリのようなタグライブラリを使う場合、

<c:forEach items="${items}" var="item">
  ID:<c:out value="${item.id}"/><br>
  Title:<c:out value="${item.title}"/><br>
</c:forEach>

まずjspで以下を宣言する必要があります。ネット上だと意外と書いていない(;´Д`)当たり前のことだからも知れませんが。。。

<!DOCTYPE html>
<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
<html>
<head>

※ちなみに、間違って以下のように定義したら、

<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>

以下のエラーがでました。

According to TLD or attribute directive in tag file, attribute items does not accept any expressions

あとライブラリを自身のプロジェクトに取り込む必要があります。
例:mavenの場合:

<dependency>
   <groupId>jstl</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
</dependency>

※なぜか
Maven Repository: javax.servlet.jsp.jstl » jstlで参照させているように以下のように記載したらエラーがでました(;´Д`)

<dependency>
	<groupId>javax.servlet.jsp.jstl</groupId>
	<artifactId>jstl</artifactId>
	<version>1.2</version>
</dependency>


参考

spring - Sun JSTL taglib declaration fails with "Can not find the tag library descriptor" - Stack Overflow

Javaの道:Taglibs(2.Standard Taglibsの概要)

c:forEach items does not accept any expression? - Spring Forum

java - According to TLD or attribute directive in tag file, attribute items does not accept any expressions - Stack Overflow