ERP如何集成jstl与开启EL表达式
一、下载jar包
- jstl-1.2.jar
- standard-1.1.2.jar
下载地址:
注意:此处链接点开如果显示页面不存在,则需要手动复制链接到浏览器地址栏里打开
https://pan.baidu.com/s/1puutLHu-TtXZ6iKqZjmYsA
下载完之后将下载的jar文件拷贝出来放到ERP环境中的WEB-INF/lib下,然后右击jar包,Build Path->Add to Build Path,如何上述操作之后代码里不起作用,则再将jar放到tomcat下的tomcat6\tomcatCommon\lib_35下,自此jar包导入工作完毕。
二、web.xml修改
一开始web.xml的配置大概如下面所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp_ID">
<display-name>erp</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/xfire.xml /WEB-INF/spring.xml
</param-value>
</context-param>
<!-- ......其他servlet或listener等配置...... -->
</web-app>
此处需要将web-app的版本提高一下,修改成如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>erp</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/xfire.xml /WEB-INF/spring.xml
</param-value>
</context-param>
<!-- ......其他servlet或listener等配置...... -->
</web-app>
三、后台返回值的格式修改、jstl的引入与EL表达式的开启
代码如下:
<%@ page import="com.icsc.dpms.de.structs.dejcWebInfoOut" %>
<!-- isELIgnored:是否忽略EL表达式,设置为false,说明就启用了EL表达式 -->
<%@ page language="java" isELIgnored="false" %>
<!-- 此处是引入最常用的三个jstl库,其他的可自己自定义引入 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!-- 此处是将上下文路径放入ctx参数中,后期可直接使用${ctx}来代替 -->
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<%
//此处代码是将infoOut里的resultVO、parameter与attribute返回值统一放在request的attribute中
//后期前端取值不用再到infoOut里取,也不用操心取得值是resultVO、parameter还是attribute
dejcWebInfoOut infoOut = null;
if (request.getAttribute("infoOut") != null) {
infoOut = (dejcWebInfoOut) request.getAttribute("infoOut");
String[] keys = infoOut.getAllResultVOIds();
for (String key : keys) {
request.setAttribute(key, infoOut.getResultVO(key));
}
String[] paramKeys = infoOut.getAllParameterKeys();
for (String key : paramKeys) {
request.setAttribute(key, infoOut.getParameter(key));
}
}
%>
建议将上述代码放到一个jsp里,比如这个jsp叫wfjjHeader.jsp,然后需要用到的jsp页面统一include进去,如下图代码所示:
<%@ page pageEncoding="GBK" %>
<%@ include file="wfjjHeader.jsp" %>
<!DOCTYPE html>
<html>
<body>
<!-- jsp内容 -->
</body>
</html>
四、使用jstl与EL表达式的代码示例
<%@ page language="java" pageEncoding="GBK" %>
<%@ include file="wfjjHeader.jsp" %>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="pagerForm" method="post" action="${ctx}/wf/do?_pageId=wfjjWorkflow&_action=list">
<input type="hidden" name="pageNum" value="${page.pageNum}" />
<input type="hidden" name="numPerPage" value="${page.numPerPage}" />
<input type="hidden" name="workflowName" value="${workflowName}" />
<input type="hidden" name="sysId" value="${sysId}" />
</form>
<div>
<div class="menu">
<div class="btnArea">
<a href="${ctx}/wf/do?_pageId=wfjjWorkflow&_action=preCreate&sysId=${sysId}" target="dialog" dialogId="createNewGroupDialog" width="765" height="600" title="新增流程" class="layui-btn layui-btn-normal">
<i class="layui-icon"></i>新增
</a>
<span class="assistMiddleSpan"></span>
</div>
<div class="searchArea">
<form method="post" class="layui-form layui-form-pane" action="${ctx}/wf/do?_pageId=wfjjWorkflow&_action=list" onsubmit="return navTabSearch(this, '.workflowListPart')">
<div class="layui-form-item">
<label class="layui-form-label">流程名称</label>
<div class="layui-input-block">
<input type="hidden" name="sysId" value="${sysId}" />
<input type="text" name="workflowName" value="${workflowName}" placeholder="请输入流程名称" autocomplete="off" class="layui-input">
</div>
</div>
<button class="layui-btn">
<i class="layui-icon"></i>搜索
</button>
</form>
<span class="assistMiddleSpan"></span>
</div>
</div>
<c:if test="${fn:length(list) == 0}">
<div class="jumbotron">
<h1>当前没有流程数据!</h1>
</div>
</c:if>
<c:if test="${fn:length(list) != 0}">
<table class="layui-table">
<thead>
<tr>
<th style="width: 30px;">序号</th>
<th>流程名称</th>
<th>备注</th>
<th style="width: 35px;">条件</th>
<th style="width: 105px;">创建人</th>
<th style="width: 150px;">操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="item" items="${list}" varStatus="status">
<tr>
<td>${page.pageBeginCount + status.index + 1}</td>
<td>${item.workflowName}</td>
<td>${item.remark}</td>
<td>${item.conditionNum}</td>
<td>${item.createUserName}[${item.createUserId}]</td>
<td>
<c:if test="${currentUserId eq item.createUserId or currentUserId eq '001805' or currentUserId eq '020463'}">
<a class="layui-btn layui-btn-xs" target="dialog" dialogId="updateWorkflowDialog" width="765" height="600" href="${ctx}/wf/do?_pageId=wfjjWorkflow&_action=preUpdate&id=${item.id}&sysId=${sysId}">编辑</a>
<a class="layui-btn layui-btn-xs layui-btn-warm" target="dialog" dialogId="manageWorkflowNodeDialog" max="true" href="${ctx}/wf/do?_pageId=wfjjWorkflowNode&_action=manageNode&workflowId=${item.id}&sysId=${sysId}">节点管理</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" target="ajaxTodo" title="确认删除?" loadContainer=".workflowListPart" href="${ctx}/wf/do?_pageId=wfjjWorkflow&_action=delete&id=${item.id}">删除</a>
</c:if>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- 分页 -->
<c:import url="wfjjPanelBar.jsp">
<c:param name="container" value=".workflowListPart"/>
</c:import>
</c:if>
</div>
</body>
</html>
五、总结
使用jstl表达式与EL表达式
- 可以完美的避免在jsp中写java代码,使得前后端的职责分明更加明确
- jstl中提供了很多封装的方法,提高前端jsp代码的开发效率,提高jsp代码的可维护性
- 可以进一步提升jsp代码的整体美观性
作者:研究院-平台架构部-王玮玮









网友评论