jstl标签库的配置
* 将jstl.jar和standard.jar拷贝到WEB-INF/lib下(如果使用el表达式,不用拷贝这两个jar)
注意:jstl必须在能够支持j2ee1.4/servlet2.4/jsp2.0版本上的容器才能运行,这个环境
是目前较为常用的环境
标签库的使用
* 采用taglib指令引入
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
自定义函数库:
1、定义类和方法(方法必须是public static)
2、编写自定义tld文件,并且将此文件放到WEB-INF或WEB-INF任意子目录下
3、在jsp中采用taglib指令引入自定义函数库
4、采用 前缀+冒号+函数名 调用即可
这个工程没用到ActionForm
JstlElAction.java
package com.bjsxt.struts; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * 测试EL表达式 * @author Administrator * */ public class JstlElAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //普通字符串 request.setAttribute("hello", "hello world"); //结构 Group group = new Group(); group.setName("尚学堂"); User user = new User(); user.setUsername("张三"); user.setAge(18); user.setGroup(group); request.setAttribute("user", user); //map的用法 Map mapValue = new HashMap(); mapValue.put("key1", "value1"); mapValue.put("key2", "value2"); request.setAttribute("mapvalue", mapValue); //字符串数组 String[] strArray = new String[]{"a", "b", "c"}; request.setAttribute("strarray", strArray); //对象数组 User[] users = new User[10]; for (int i=0; i<10; i++) { User u = new User(); u.setUsername("U_" + i); users[i] = u; } request.setAttribute("users", users); List userList = new ArrayList(); //ArrayList的用法 for (int i=0; i<10; i++) { User uu = new User(); uu.setUsername("UU_" + i); userList.add(uu); //可以存放对象,而创建时不用声明是该对象 } request.setAttribute("userlist", userList); //empty request.setAttribute("value1", null); request.setAttribute("value2", ""); request.setAttribute("value3", new ArrayList()); request.setAttribute("value4", "123456"); return mapping.findForward("success"); } }
jstl_el.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
JstlCoreAction.java
package com.bjsxt.struts; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * 测试jstl核心库 * @author Administrator * */ public class JstlCoreAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //普通属性 request.setAttribute("hello", "Hello World"); //测试条件控制标签 request.setAttribute("v1", 1); request.setAttribute("v2", 2); request.setAttribute("v3", new ArrayList()); request.setAttribute("v4", "test"); //html文本 request.setAttribute("bj", "北京欢迎您"); //测试c:forEach Group group = new Group(); group.setName("尚学堂"); List userList = new ArrayList(); for (int i=0; i<10; i++) { User user = new User(); user.setUsername("user_" + i); user.setAge(18+i); user.setGroup(group); userList.add(user); } request.setAttribute("userlist", userList); //测试循环输出map Map map = new HashMap(); map.put("k1", "v1"); map.put("k2", "v2"); request.setAttribute("mapvalue", map); //测试c:forTokens request.setAttribute("strTokens", "1,2,3,4,5,6"); return mapping.findForward("success"); } }
jsdl_core.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
姓名 | 年龄 | 所属组 |
没有符合条件的数据! | ||
${u.username } | ${u.age } | ${u.group.name } |
姓名 | 年龄 | 所属组 |
没有符合条件的数据! | ||
| | |
姓名 | 年龄 | 所属组 |
没有符合条件的数据! | ||
${user.username} | ${user.age} | ${user.group.name } |
struts-config.xml 无ActionForm多Action的情况