天天看點

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–控制器,連接配接頁面和模型的橋梁

繼續閱讀