1.引入以下两个jar包
![](https://p-blog.csdn.net/images/p_blog_csdn_net/CodeDestiny/JSTLCore.png)
2.按惯例,先上struts-config.xml,(其中包含了我上一个EL测试的action)
<?
xml version="1.0" encoding="ISO-8859-1"
?>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
struts-config
>
<
action-mappings
>
<
action
path
="/jstlel"
type
="com.codedestiny.struts.JSTLELAction"
scope
="request"
>
<
forward
name
="success"
path
="/jstlel.jsp"
></
forward
>
</
action
>
<
action
path
="/jstlcore"
type
="com.codedestiny.struts.JSTLCoreAction"
scope
="request"
>
<
forward
name
="success"
path
="/jstlcore.jsp"
></
forward
>
</
action
>
</
action-mappings
>
<
message-resources
parameter
="MessageResources"
/>
</
struts-config
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
3.接下来JSTLCoreAction.java:
package
com.codedestiny.struts;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
JSTLCoreAction
extends
Action
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
request.setAttribute("hello", "Hello World");
request.setAttribute("cd", "<font color='red' size='6'>成都欢迎大家</font>");
request.setAttribute("v1", 10);
request.setAttribute("v2", 20);
List<User> users = new ArrayList<User>();
Group relatedGroup = new Group();
relatedGroup.setName("Xihua Univercity");
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i=1; i<10; i++) ...{
User u = new User();
u.setUsername("user" + i);
u.setAge(20 + i);
u.setRelatedGroup(relatedGroup);
users.add(u);
}
Map<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("key1", 1);
hashMap.put("key2", 2);
hashMap.put("key3", 3);
hashMap.put("key4", 4);
request.setAttribute("map", hashMap);
request.setAttribute("users", users);
request.setAttribute("tokenstr", "1,2,3,4,5");
return mapping.findForward("success");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
4.展示页面jstlcore.jsp:
<%
...
@ page language="java" import="java.util.*" pageEncoding="GB18030"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<
html
>
<
head
>
</
head
>
<
body
>
<
h1
>
JSTL核心库标签测试
</
h1
>
<
hr
>
<
li
>
测试c:out
</
li
><
br
>
hello(default) :
<
c:out
value
="${hello}"
></
c:out
><
br
>
b(default value) :
<
c:out
value
="${b}"
default
="no value"
></
c:out
><
br
>
b(default value) :
<
c:out
value
="${b}"
>
no value
</
c:out
><
br
>
cd(default) :
<
c:out
value
="${cd}"
></
c:out
><
br
>
cd(escapeXml=true) :
<
c:out
value
="${cd}"
escapeXml
="true"
></
c:out
><
br
>
cd(escapeXml=false) :
<
c:out
value
="${cd}"
escapeXml
="false"
></
c:out
><
br
>
<
hr
>
<
li
>
测试c:set, c:remove
</
li
><
br
>
<
c:set
var
="temp"
value
="123"
></
c:set
>
temp : ${ temp }
<
br
>
<
c:remove
var
="temp"
/>
temp : ${ temp }
<
br
>
<
hr
>
<
li
>
测试条件控制标签c:if
</
li
><
br
>
<
c:if
test
="${v1 < v2}"
var
="boolean"
>
v1
<
v2
? ${boolean} <br
>
</
c:if
>
<
hr
>
<
li
>
测试条件控制标签c:choose, c:when, c:otherwise
</
li
><
br
>
<
c:choose
>
<
c:when
test
="${v1 < v2}"
>
v1
<
v2
<br
>
</
c:when
>
<
c:otherwise
>
v1 > v2
<
br
>
</
c:otherwise
>
</
c:choose
>
<
hr
>
<
li
>
测试循环访问标签c:forEach
</
li
><
br
>
<
table
border
="1"
>
<
tr
>
<
td
>
姓名
</
td
><
td
>
年龄
</
td
><
td
>
所属组
</
td
>
</
tr
>
<
c:choose
>
<
c:when
test
="${empty users}"
>
<
tr
><
td
colspan
="3"
>
没有相关数据
</
td
></
tr
>
</
c:when
>
<
c:otherwise
>
<
c:forEach
items
="${users}"
var
="user"
>
<
tr
>
<
td
>
${user.username}
</
td
>
<
td
>
${user.age}
</
td
>
<
td
>
${user.relatedGroup.name}
</
td
>
</
tr
>
</
c:forEach
>
</
c:otherwise
>
</
c:choose
>
</
table
>
<
hr
>
<
li
>
测试循环访问标签c:forEach中varStatus的用法
</
li
><
br
>
<
table
border
="1"
>
<
tr
>
<
td
>
姓名
</
td
><
td
>
年龄
</
td
><
td
>
所属组
</
td
>
</
tr
>
<
c:choose
>
<
c:when
test
="${empty users}"
>
<
tr
><
td
colspan
="3"
>
没有相关数据
</
td
></
tr
>
</
c:when
>
<
c:otherwise
>
<
c:forEach
items
="${users}"
var
="user"
varStatus
="vs"
>
<
c:choose
>
<
c:when
test
="${vs.count % 2 == 0}"
>
<
tr
bgcolor
="red"
>
<
td
>
${user.username}
</
td
>
<
td
>
${user.age}
</
td
>
<
td
>
${user.relatedGroup.name}
</
td
>
</
tr
>
</
c:when
>
<
c:otherwise
>
<
tr
>
<
td
>
${user.username}
</
td
>
<
td
>
${user.age}
</
td
>
<
td
>
${user.relatedGroup.name}
</
td
>
<
tr
>
</
c:otherwise
>
</
c:choose
>
</
c:forEach
>
</
c:otherwise
>
</
c:choose
>
</
table
>
<
hr
>
<
li
>
测试循环访问标签c:forEach中begin, end, step的用法
</
li
><
br
>
<
table
border
="1"
>
<
tr
>
<
td
>
姓名
</
td
><
td
>
年龄
</
td
><
td
>
所属组
</
td
>
</
tr
>
<
c:choose
>
<
c:when
test
="${empty users}"
>
<
tr
><
td
colspan
="3"
>
没有相关数据
</
td
></
tr
>
</
c:when
>
<
c:otherwise
>
<
c:forEach
items
="${users}"
var
="user"
varStatus
="vs"
begin
="1"
end
="8"
step
="2"
>
<
c:choose
>
<
c:when
test
="${vs.count % 2 == 0}"
>
<
tr
bgcolor
="red"
>
<
td
>
${user.username}
</
td
>
<
td
>
${user.age}
</
td
>
<
td
>
${user.relatedGroup.name}
</
td
>
</
tr
>
</
c:when
>
<
c:otherwise
>
<
tr
>
<
td
>
${user.username}
</
td
>
<
td
>
${user.age}
</
td
>
<
td
>
${user.relatedGroup.name}
</
td
>
<
tr
>
</
c:otherwise
>
</
c:choose
>
</
c:forEach
>
</
c:otherwise
>
</
c:choose
>
</
table
>
<
hr
>
<
li
>
测试循环访问标签c:forEach,输出一个map
</
li
><
br
>
<
c:forEach
items
="${map}"
var
="v"
>
<
c:out
value
="${v.key}"
/>
=
<
c:out
value
="${v.value}"
/><
br
>
</
c:forEach
>
<
hr
>
<
li
>
测试循环访问标签c:forEach,普通标签
</
li
><
br
>
<
c:forEach
begin
="0"
end
="5"
>
aaa
<
br
>
</
c:forEach
>
<
hr
>
<
li
>
测试循环控制标签c:fortokens
</
li
><
br
>
<
c:forTokens
items
="${tokenstr}"
delims
=","
var
="v"
>
<
c:out
value
="${v}"
></
c:out
>
</
c:forTokens
>
</
body
>
</
html
>
5.最后来几张测试结果的截图:![](https://p-blog.csdn.net/images/p_blog_csdn_net/CodeDestiny/coredebug1.png)
![](https://p-blog.csdn.net/images/p_blog_csdn_net/CodeDestiny/coredebug2.png)