jsp入门——4、标准标签

jstl标准标签:主要为了减少jsp页面中写java代码,增强代码的可重用性,提高开发速度,使用前要添加jstl架包

set标签使用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>




神头鬼脸


<%request.setAttribute("name1", "黑无常...") ;%>





黄泉沼

${applicationScope.a }

jsp入门——4、标准标签_第1张图片

out标签使用

<%@ page language="java" import="com.jstl.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>




黑白无常   鬼呀


<%
out.print("黑无常....
"); %>
<% int age = 1000; request.setAttribute("age", new Integer(age)); %>
${age}
${per.name } ${per.age }

jsp入门——4、标准标签_第2张图片

条件判断标签if

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   




武林高高手




 你是传奇...


判断标签choose、when、otherwise放在一起用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    




Insert title here




   优秀
   良好
   just so so
   so bad


迭代循环:forEach迭代循环输出集合中元素

java类

package com.jstl;

public class Person {

	private int age;
	private String name;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

jsp页面

<%@ page language="java" import="java.util.*,com.jstl.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>





<%
   List list = new ArrayList();
   list.add("A");
   list.add("B");
   list.add("C");
   list.add("D");
   list.add("E");
   list.add("F");
   request.setAttribute("List", list);
   
   List perlist = new ArrayList();
   Person per = new Person();
   per.setAge(15);
   per.setName("rose");
   
   Person per2 = new Person();
   per2.setAge(16);
   per2.setName("hese");
   
   Person per3 = new Person();
   per3.setAge(17);
   per3.setName("ttom");
   
   perlist.add(per);
   perlist.add(per2);
   perlist.add(per3);
   
  /*  共享 */
  request.setAttribute("personlist", perlist);
%>
ding ding ...




   



${i }
${i}

jsp入门——4、标准标签_第3张图片

你可能感兴趣的:(Servlet&Jsp)