jsp引入JSTL后实现jsp的解耦

需求:

1、做一个固定资产的系统,里面实体类,业务逻辑(接口+实现类)+jsp显示所有信息。

2、由于没有数据库,所以在实现类模拟2条数据。

步骤:

1、建立一个工程assets

2、先建立好4个包entity ,action,service,dao(本例用不上)

3、建立实体类(先写属性,然后添加get/set)

FixedAssets.java

4、在service包建立service接口(业务逻辑)接口,有几个业务逻辑,就有几个方法

FixedAssetsService.java

//显示所有资产

   public List<FixedAssets> getAllFixedAssets();

5、在service包建立service的实现类FixedAssetsServiceImpl.java

public List<FixedAssets> getAllFixedAssets() {

      List<FixedAssets> fixedAssetsList = new ArrayList<FixedAssets>();

      //fixedAssetsList.add( 具体的实体的实例 );

 

       return fixedAssetsList;

     }

6、在action包中根据业务需要添加Servlet

新建一个FixedAssetsServletGetAllInfo.java

       在doGet方法中写代码;

a.呼叫service

b.把结果集List放到request对象

c.跳转到jsp中

7、新建一个jsp

a、倒4个jstl的包

b、添加<%@ taglib prefix="c"…

c、写遍历结果集合的代码

这段代码

${fixedAssetsList}直接取代

<%@ page import="java.util.List" %>

<%@ page import="entity.House" %>

<%

List<House> houseList = (List)request.getAttribute("houseList_label");

%>

其中本例中的${fixedAssetsList}

其实它完整写法: ${requestScope.fixedAssetsList}

 

原理EL表达式,可以直接取request范围内变量

EL表达式,定义了多个隐式对象,参考书本168页

原文地址:https://www.cnblogs.com/xieguohui/p/6894142.html