天天看点

EL表达式-JSTL和MVC模式JSP加强&JSTL&MVC思想

我的个人网站欢迎访问嗷

EL表达式-JSTL和MVC模式JSP加强&JSTL&MVC思想

JSP加强&JSTL&MVC思想

JavaBean

JavaBean是java的一个可重用的组件

就是一个特殊(满足一定的规范)的类

规范:

​ 必须有包

​ 必须有公有(public)的无参构造(如果显示的给出了构造,编译器就不会自动添加无参构造)

​ 如果有字段,必须有gte(el表达式)和set方法

​ 建议字段私有化

​ toString(方便测试)

实例属性与Bean属性

实例属性–字段

JavaBean属性 = Bean属性 = get方法或者set方法上面的属性

一般情况下:实例属性与Bean属性是相同的

JavaBean的自省机制

Java对Bean类属性和方法的默认处理机制

什么是JavaBean内省机制?

内省也叫自省。内省(IntroSpector)是Java语言对 Bean类属性、事件(GUI,这里不学)的一种缺省(默认)处理方法;

我们拿到一个JavaBean,就可以拿到它的读写属性和读写方法,然后通过反射完成读写操作;

内省是基于JavaBean规范对反射进行了封装,可以很容易的获取javaBean的属性、方法和事件;

BeanUtils的使用(主要进行属性的拷备)

引入:有人对JavaBean做了一整套的解决实施方案,有些时候需要用到属性的拷贝,而且很多框架都是在用这个属性的拷贝的功能。需要导入对应的jar包

package com.ifueen.classtest.test;

import static org.junit.Assert.*;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

import com.ifueen.classtest.domian.Person;

public class TestPerson {

	/**
	 *获取对象属性
	 */
	@Test
	public void test() throws Exception {
		//获取Info对象
		BeanInfo info = Introspector.getBeanInfo(Person.class,Object.class);
		PropertyDescriptor[] prp = info.getPropertyDescriptors(); //获取属性的描述器
		for (PropertyDescriptor prop : prp) {
			System.out.println(prop);	//遍历输出对象属性
			Method get = prop.getReadMethod();	//获取get方法
			System.out.println(get);
		}

	}
	/**
	 *将一个对象复制到另一个对象
	 */
	@Test
	public void test1() throws Exception {
		//定义两个Person对象
		Person person = new Person("小花","她十八岁的影子",21,"曾经沧海难为水");
		System.out.println(person.getAge());
		Person person1 = new Person();
		//注意:第一个参数是需要拷贝到什么地方去的对象,第二个参数是将要进行拷贝的对象
		BeanUtils.copyProperties(person1, person);
		System.out.println(person1);
	}
	/**
	 *将Map中的信息传递给一个对象
	 */
	@Test
	public void test2() throws Exception {
		//定义Map
		Map<String, Object> map = new Hashtable<String, Object>();
		map.put("id", "她十八岁的模样");
		map.put("name", "白月光");
		map.put("age", 18);
		map.put("address", "鹿港的小镇");
		Person person = new Person();
		BeanUtils.copyProperties(person, map);
		System.out.println(person);
	}

}

           

EL表达式

EL表达式就是一种表达式语言,可以获取四大作用域中的共享数据

${内容}

用来与jstl结合使用,代替jsp中的Java代码

1.可以获取四大作用域中的绑定值(默认是从小到大获取)

2.获取bean的属性值

3.注意:el表达式获取作用域中的属性要依靠JavaBean的可读属性即需要有getter方法,否则无法获取

三大注意事项

1

特殊的写法:session.setAttribute(“KEY.IN.SESSION”, “你是session”)

错误写法:${sessionScope.KEY.IN.SESSION}

正确写法:${sessionScope[“KEY.IN.SESSION”]}

2

${pageContext.request.contextPath} :拿到上下文路径,页面上的href,src等属性可以使用这种方式制定绝对路径;

${pageContext.request.contextPath }
           

3

Tomcat7之后EL表达式可以调用方法

调用下面的xdsfsdf()方法:

EL表达式-JSTL和MVC模式JSP加强&amp;JSTL&amp;MVC思想
EL表达式-JSTL和MVC模式JSP加强&amp;JSTL&amp;MVC思想

JSTL

JSP standard tag library – JSP的标准标签库

作用:为了将JSP中的java代码替换了

三大标签

使用步骤

1.导入jstl的2个jar包

web工程的jar包一定要放在指定位置(WEB-INF/lib文件夹下)

2.引入jstl的核心标签库

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Demo

<C:if>

的使用:

<%@page import="com.ifueen.classtest.domian.Person"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	
	<%
	Person p = new Person("今夜我在布宜诺斯艾利斯农场","一个叫木头",30,"一个叫马尾");
	request.setAttribute("p", p);
	%>
	
	
	<hr style="border: 3px #000 solid; margin-left: 30px;margin-right: 30px;">
	<c:if test="${p.age>=20 }" var="s">
	<p style="text-align: center; font-family: '华文中宋'; font-weight: bold; font-size: 24px;">
	我站在这里
	</p>
	</c:if>
	<c:if test="!s">
	<p style="text-align: center; font-family: '华文中宋'; font-weight: bold; font-size: 24px;">
	我不想
	</p>
	</c:if>
	
	
	<hr style="border: 3px #f2658d solid; margin-left: 30px;margin-right: 30px;">
	<p style="text-align: center; font-family: '华文中宋'; font-weight: bold; font-size: 24px;color: #f2658d;">
	${pageContext.request.contextPath }
	</p>
	
</body>
</html>
           

<c:foreach>

的使用:

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.ifueen.classtest.domian.Person"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
	
	td{
		width: 100px;
		border: 3px #f2658d solid;
		text-align: center;
		font-family: "华文中宋";
		font-weight: bold;
	}
	table {
		margin-top:20px;
		margin: auto;
		border-collapse: collapse;
	}
</style>
<title>Insert title here</title>
</head>
<body>
	
	<%
	List<Person> list = new ArrayList<Person>();
	list.add(new Person("会沉寂吗","一个叫木头",31,"一个叫马尾"));
	list.add(new Person("会沉寂吗","一个叫木头",32,"一个叫马尾"));
	list.add(new Person("会沉寂吗","一个叫木头",33,"一个叫马尾"));
	list.add(new Person("会沉寂吗","一个叫木头",34,"一个叫马尾"));
	list.add(new Person("会沉寂吗","一个叫木头",35,"一个叫马尾"));
	request.setAttribute("list", list);
	%>
	
	<table>
	
	<c:forEach items="${list }" var="p">
	<tr>
	<td>${p.id }</td>
	<td>${p.name }</td>
	<td>${p.address }</td>
	<td>${p.age }</td>
	</tr>
	</c:forEach>
	
	</table>
	<hr style="border: 3px #000 solid; margin-left: 30px;margin-right: 30px;margin-top: 20px;">
	
	<hr style="border: 3px #f2658d solid; margin-left: 30px;margin-right: 30px;margin-top: 20px;">
	<p style="text-align: center; font-family: '华文中宋'; font-weight: bold; font-size: 24px;color: #f2658d;">
	${pageContext.request.contextPath }
	</p>
	
</body>
</html>
           

<c:choose>

的使用:

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.ifueen.classtest.domian.Person"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
	
	td{
		width: 100px;
		border: 3px #f2658d solid;
		text-align: center;
		font-family: "华文中宋";
		font-weight: bold;
	}
	table {
		margin-top:20px;
		margin: auto;
		border-collapse: collapse;
	}
</style>
<title>Insert title here</title>
</head>
<body>
	
	<%
	Person p = new Person("会沉寂吗","一个叫木头",35,"一个叫马尾");
	request.setAttribute("p", p);
	%>
	<hr style="border: 3px #000 solid; margin-left: 30px;margin-right: 30px;margin-top: 20px;">
	<c:choose>
	<c:when test="${p.age<18 }">
	<p style="text-align: center; font-family: '华文中宋'; font-weight: bold; font-size: 24px;color: #f2658d;">
	一只趋光的蚂蚁
	</p>
	</c:when>
	<c:when test="${p.age>=18 && p.age<=60 }">
	<p style="text-align: center; font-family: '华文中宋'; font-weight: bold; font-size: 24px;color: #f2658d;">
	查拉图斯特拉如是说
	</p>
	</c:when>
	<c:otherwise>
	<p style="text-align: center; font-family: '华文中宋'; font-weight: bold; font-size: 24px;color: #f2658d;">
	乌斯怀亚的灯塔
	</p>
	</c:otherwise>
	
	</c:choose>
	
	<hr style="border: 3px #f2658d solid; margin-left: 30px;margin-right: 30px;margin-top: 20px;">
	<p style="text-align: center; font-family: '华文中宋'; font-weight: bold; font-size: 24px;color: #f2658d;">
	</p>
	
</body>
</html>
           

MVC模式

Model View Controller

模型 视图 控制器

软件设计的思想:

view:页面(html,jsp)

Controller:控制器,连接页面和后台 (Servlet)

Model:后台java代码 (所有的后台Java代码)

后台设计三层架构

表现层:页面,控制器

业务层:业务代码

数据访问(DAO)层:专门与数据库进行交互(CRUD)

面试题

介绍一下MVC

是一种软件设计思想

M是Model–模型,所有的后台代码(业务代码+domain+dao)

V是View–视图,页面(html,jsp)

C是Controller–控制器,连接页面和模型的桥梁

继续阅读