09EL表达式和JSTL标签库

EL表达式和JSTL标签库

1.初识JavaBean

1.1 什么是javaBean:

	它是java开发中常用的组件,其实就是一个java类,它的作用就是封装数据。
	书写JavaBean需要满足五个规范:
	>>>1.这个java类,被public修饰
	>>>2.这个java类要提供公共的无参数的构造方法
	>>>3.要提供私有的属性
	>>>4.要给私有的属性提供公共的set或者get方法
	>>>5.要实现Serializable接口

比如:

public class Book implements Serializable{ 
				private String id;
				private String name;
				public Book(){};
				public void set----
				public String get---
			}

1.2 访问JavaBean的属性:

    类的成员变量和属性的一个关系:
     在普通的java类里面,成员变量可以说成就是属性。
     在JavaBean里面,成员变量和属性就有了区别。
     在JavaBean 里面什么是成员变量呢? 比如:private String id; 那么id就是成员变量
     在JavaBean 里面什么是属性?就是get或者set后面的字段名称(字段名称的首字母小写),就是属性。

比如:

public class Student {
			//一下就是javaBean的四个成员变量
			private String sid;//目的就是为了接收外界传递过来的值
			private String name;
			public Student() {
				
			}
			//属性由get或者set方法后面的字段首字母小写就是属性,属性就是id
			public String getId() {
				return sid;
			}
			public void setId(String id) {//写方法,给student写一个id
				this.sid = id;
			}
			//属性就是name
			public void setName(String name) {
				this.name = name;
			}
		}

上面的Student的属性就是id ,name。成员变量就是sid,name.

1.3 BeanUtils 工具

   BeanUtils 工具是由apache软件基金会提供的一套封装数据到JavaBean的工具类,使用简单方便,
   BeanUtils是第三方的工具(插件),所以需要导入jar包。
   常用的api:
   >>> 1.向javaBean的属性设置值: 
	setProperty(javaBean对象,javaBean的属性,javaBean属性的值);
   >>> 2.获取javaBean属性的值:
       getProperty(javaBean对象,javaBean的属性);
   >>> 3.向javaBean的属性设置值:
       populate(javaBean对象,map对象);
       注意:map的对象的key(就是javaBean属性名称),必须和javaBean的属性名一致,才能给javaBean的属性设置值。
	     提交的表单数据想封装到javaBean对象里面,要求表单name属性的值,必须和javaBean属性名称一一对应。

2.EL 表达式

1.1 初识 EL 表达式

    EL在开发中,通常是用来获取域对象中保存的值,基本语法:${域对象的名称}。
    比如说: request.setAttribute("key","value123"):  ${key},获取的值就是value123
    如果域对象的名称写错了,使用el表达式获取值,获取的是"".

1.2 EL表达式中的标识符

    在el书写过程中,会用一些符号来标记变量、函数名等,这些符号称之为标识符。
    书写规范:
    1.不能以数字开头
    2.不能包含el中的关键字:and , or 等
    3.不能使用el表达式的隐式对象。
    4.不能包含特殊符号,比如正斜杠等

1.3 EL中的变量

    基本格式:${域对象的名称},这个域对象的名称可以理解为就是el中的变量,
	      那这个变量就不需要定义了,可以直接使用。

1.4 EL 中的常量

   1.布尔常量:true或false
   2.数字常量:整型、浮点常量,使用方式和java差不多
   3.字符串常量:使用方式和java差不多,比如:${"el的字符串常量"}
   4.Null常量:${null}

1.5 EL中的运算符

   1.点运算符:获取域对象中属性的值。
   	比如: ${person.name }
   2.方括号运算符:在域对象里,有的属性包含特殊字符,所以用方括号的方式来获取值
	比如:<% 
 		Map<String,String> map= new HashMap<String,String>();
		map.put("my-name","map的值");
		request.setAttribute("user",map);
	  %>
	${user["my-name"] }
   3.算术运算符:+ — * /
   4.比较运算符: > < >= <= == !=
   5.逻辑运算符: &&(and) ||(or) !(not)
   6.empty 运算符:用来判断域对象中的值是否存在,不存在返回为true,否则返回的结果是false.
   7.三目运算符:参照java的用法。

1.6 EL 隐式对象

   1.pageContext对象:为了获取jsp中的隐式对象。
     比如:
	 获取项目的路径:${pageContext.request.contextPath }<br/>
	 获取请求的URL:${pageContext.request.requestURI }

   2.web域相关的对象
     域作用范围从小到大:pageContext--->request--->session--->application(servletContext)
     el表达式获取域对象中的值:如果域对象的名称相同,获取的是域作用范围最小的值。
                               和pageContext对象的findAttribute方法的效果是一样的。

   3.param 和 paramValues 对象: 获取表单提交的数据。
    比如:
    	num1:<input type="text" name="num1" /><br/>
	num2:<input type="text" name="num" /><br/>
	num3:<input type="text" name="num" /><br/>
	<input type="submit" value="提交" /> &nbsp;&nbsp;<input type="reset" value="重填" />
	<hr/>
	num1:${param.num1 }<br/>
	num2:${paramValues.num[0] }<br/>
	num3:${paramValues.num[1] }
   4. Cookie 对象:获取cookie的名称,获取cookie的值
	比如:
		<% response.addCookie(new Cookie("userName","itcast")); %>
		获取cookie对象:${cookie.userName }<br/>
		获取cookie的名称:${cookie.userName.name }<br/>
		获取cookie的值:${cookie.userName.value }<br/>

项目代码:

package cn.itcast.chapter07.beanutils;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

public class BeanUtilsDemo {
	public static void main(String[] args) throws Exception{
		Person p=new Person();
		//p.setName("大数据2005");
		//System.out.println(p.getName());
		/*
		 *BeanUtils.setProetry(p,"name","周敏");
		 *BeanUtils.setProetry(p,"age",20);
		 *String name=BeanUtils.getProperty(p,"name");
		 *int age=Integer.parseInt(BeanUtils.getProperty(p,"age"));
		 *System.out.println(name);
		 *System.out.println(age);
		 */
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("name", "dashuju2005");
		map.put("age", 2);
		BeanUtils.populate(p,map);
		String name=BeanUtils.getProperty(p,"name");
		int age=Integer.parseInt(BeanUtils.getProperty(p,"age"));
		System.out.println(name);
		System.out.println(age);
	}
}

package cn.itcast.chapter07.beanutils;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

/**
 * Servlet implementation class BeanUtilsServlet
 */
@WebServlet("/BeanUtilsServlet")
public class BeanUtilsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BeanUtilsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//String name=request.getParameter("name");
		//String ageform=request.getParameter("age")
		//int age=Integer.parseInt(ageform);
		Person p=new Person();
		//p.setName(name);
		//p.setAge(age);
		try {
			BeanUtils.populate(p,request.getParameterMap());
		}catch(Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.getWriter().print(p);
		System.out.println(p);
			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

package cn.itcast.chapter07.beanutils;
public class Person {
	private String name;
	private int age;
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name=name;
	}
	public int getAge(){
		return age;
	}
	public void setAge(int age){
		this.age=age;
	}
	public Person(){
		super(); 
	}
	public String toString() {
		return "Person[name=" + name + ",age=+" + age+ "]";
	}
}

package cn.itcast.chapter07.javabean;
public class Student {
	private String sid;
	private String name;
	private int age;
	private boolean married;
	//age属性的getter和setter方法
	public int getAge() {
		return age;
	}
	public void getAge(int age) {
		this.age=age;
	}
	//married属性的getter和setter方法
	public boolean isMarried() {
		return married;
	}
	public void setMarried(boolean married) {
		this.married=married;
	}
	//sid属性的getter方法
	public String getSid(){
		return name;
	}
	//name属性的setter方法
		public void setName(String name){
			this.name=name;
		}
		public void getInfo(){
			System.out.print("我是大数据2005周敏");
		}
}

package cn.itcast.chapter07.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

/**
 * Servlet implementation class myservlet
 */
@WebServlet("/myservlet")
public class myservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public myservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Person p=new Person();
		p.setName("周敏");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("my-name", "map的值");
		request.setAttribute("user", null);
		request.setAttribute("username", "周敏");
		request.setAttribute("passw", "123456");
		RequestDispatcher dis=request.getRequestDispatcher("myjsp.jsp");
		dis.forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% response.addCookie(new Cookie("username","dashuju"));%>
获取cookie对象信息:${cookie.userName}<br/>
获取cookie对象名称:${cookie.userName.name}<br/>
获取cookie对象的值:${cookie.userName.values}<br/>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                           "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form name="reg" action="/chapter07/BeanUtilsServlet" method="post">
		用户名: <input name="name" type="text" /><br/>
		年龄: <input name="age" type="text" /><br/>
		
			    <input type="submit" value="提交" id="bt" />
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  >
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${empty person}<br/>
${empty user}<br/>
----------------------------------------------------<br/>
1>3?${1>3}<br/>
----------------------------------------------------<br/>
1+3=${1+3}<br/>
----------------------------------------------------<br/>
${user.my-name}<br/>
${user["my-name"]}<br/>
----------------------------------------------------<br/>
${person.name}<br/>
----------------------------------------------------<br/>
<%=(String) request.getAttribute("username")%><br/>
<%=(String) request.getAttribute("password")%><br/>
----------------------------------------------------<br/>
------${username}-----<br/>
${password}<br/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
获取项目的路径:${pageContext.request.contextPath }<br/>
${pageContext.response.contentType}<br/>
${pageContext.servletContext.servletNames}<br/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/param.jsp" method="get">
num1:<input type="text" name="num1"/><br/>
num2:<input type="text" name="num"/><br/>
num3:<input type="text" name="num"/><br/>
<input type="submit" value="提交"/>&nbsp;&nbsp;<input type="reset" value="重填"/>
<hr>
num1:${param.num1}<br/>
num2:${paramValues.num[0]}<br/>
num3:${paramValues.num[1]}<br/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%pageContext.setAttribute("page", "PAGE"); 
request.setAttribute("dashuju", "REQUEST"); 
session.setAttribute("session", "SESSION"); 
application.setAttribute("app1", "APPLICATION");
%>
${pageScope.page}=====${page}<br/>
${requestScope.dashuju}=====${dashuju}<br/>
<%
pageContext.setAttribute("aa1", "PAGE"); 
request.setAttribute("aa2", "REQUEST"); 
session.setAttribute("aa", "SESSION"); 
application.setAttribute("aa", "APPLICATION");
%>
-------------------<br/>
${aa}<br/>
${sessionScope.aa}<br/>
</body>
</html>

项目实现:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

大数据2005 周敏 2020080605048
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值