您的位置:首页 > Web前端 > JavaScript

JSTL与自定义标签

2017-08-10 00:00 260 查看

使用JSTL有两种方式

一是在JSP中使用<@ taglib >指令引入;

二是在web.xml中配置<jsp-config><taglib></taglib></jsp-config>标签

core标签

<c:out> <c:set> <c:remove>

<!--使用c:set存储的值,可以使用EL表达式取出来-->
<c:set var="balance" value="0.01" />

<!--EL表达式可以直接在页面输出内容,哪些情况下必须用c:out标签进行输出?-->
<c:out value="${i}"/>


<c:if> <c:choose> <c:when> <c:otherwise>

<c:if test="${ }">

</c:if>

<c:choose>
<c:when test="${ }">
</c:when>
</c:choose>


<c:forEach> <c:forTokens>

<c:forEach
items="<object>"
begin="<int>"
end="<int>"
step="<int>"每一次迭代的步长
var="<string>"
varStatus="<string>">
>

</c:forEach>

<c:forTokens
items="<string>"
delims="<string>"分隔符
begin="<int>"
end="<int>"
step="<int>"
var="<string>"
varStatus="<string>">
>

</c:forTokens>


<c:param> <c:redirect>

fmt标签

数字格式化<fmt:formatNumber> <fmt:parseNumber>

<!--type属性:NUMBER,CURRENCY,或 PERCENT类型,默认number;-->

<!--小数点位数格式化-->
<fmt:formatNumber type="number" value="${688.87 * 0.8 }" pattern="0.00" maxFractionDigits="2"/>

<!--
货币格式化:在数字前加上货币符号,数字部分会用“,”隔开,保留两位小数
输出:¥1,000.12
货币符号取决于默认区域
-->
<fmt:formatNumber value="1000.123" type="currency"/>

<!--格式化百分比-->
<fmt:formatNumber type="percent" maxFractionDigits="1" value="0.1234" />


时间格式化标签<fmt:formatDate> <fmt:parseDate>

<!--注意:MM表示月份,mm表示分钟,HH表示24小时制,hh表示12小时制-->
<fmt:formatDate value="${activity.endDate}" type="both" pattern="yyyy-MM-dd  HH:mm:ss"/>


绑定资源<fmt:bundle> <fmt:setBundle>

时区地区<fmt:setLocal> <fmt:timeZone> <fmt:setTimeZone>

其他<fmt:message> <fmt:requestEncoding>

JSTL函数fn

字符串处理函数:fn:contains();split();等

sql标签操作数据库

<sql:setDataSource>

<sql:query>

x标签处理XML文档

<x:out>

<x:forEach>

问题:JSTL标签中的输入(value,items,test)都是用的${}表达式,能不能用<%= %>表达式?

自定义标签

编写tld文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
7fe0

<jsp-version>1.1</jsp-version>
<short-name>h2y-tags</short-name>
<uri>/h2y-tags</uri>

<!--input tag-->
<tag>
<name>input</name>
<tag-class>com.xgh.edusite.tag.InputTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>type</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>css</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>style</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>title</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>dictcode</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>onclick</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>initoption</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>readonly</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>onchange</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>disabled</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

编写Java类

package com.xgh.edusite.tag;

import com.xgh.mng.entity.SysDictDetail;
import org.apache.commons.lang.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 自定义标签(select、checkbox、radio)
*/
public class InputTag extends TagSupport {

/**
*
*/
private static final long serialVersionUID = 1L;
private String id;//元素id
private String name;//元素名称
private String type;//编辑类型
private String dictcode;//字典编码
private String title;//标题信息
private String value;//元素值
private String css;//元素class属性
private String style;//元素style样式
private Object initoption;//select 初始化选项value,text:value,text
private String readonly;//只读属性
private String onclick;//单击事件
private String onchange;//事件
private String disabled;//禁用

private static String inputRadioF = "<input type=\"radio\" id=\"%s\"  name=\"%s\"    value=\"%s\"   class=\"%s\"  title=\"%s\"  style=\"%s\"  %s    /><label for=\"%s\">%s</label>  ";
private static String inputCheckboxF = "<input type=\"checkbox\"  id=\"%s\"  name=\"%s\"    value=\"%s\"   class=\"%s\"  title=\"%s\"  style=\"%s\"   %s   /><label for=\"%s\">%s</label>  ";
private static String inputSelectF = "<select  id=\"%s\"  name=\"%s\" class=\"%s\"  title=\"%s\"  style=\"%s\"   %s    >";
private static String selectOptionF = "<option value=\"%s\" title=\"%s\"  %s>%s</option>";

private void clear() {
this.id = null;
this.css = null;
this.dictcode = null;
this.name = null;
this.style = null;
this.title = null;
this.type = null;
this.onclick = null;
this.initoption = null;
this.readonly = null;
this.onchange = null;
this.disabled = null;
}

public int doEndTag() throws JspException {
clear();
return Tag.EVAL_PAGE;
}

public int doStartTag() throws JspException {

//sysCacheService = (ISysCacheService) IocUtil.getBean("sysCacheService");

String htmlText = null;
if ("checkbox".equals(this.getType())) {
htmlText = getCheckboxHtml();
} else if ("radio".equals(this.getType())) {
htmlText = getRadioHtml();
} else if ("select".equals(this.getType())) {
htmlText = getSelectHtml();
}

try {
pageContext.getOut().write(htmlText);
} catch (Exception e) {
e.printStackTrace();
throw new JspException("write   error!");
}
return SKIP_BODY;
}

/**
* 得到checkBox控件html
*
* @return
*/
public String getRadioHtml() {

StringBuffer html = new StringBuffer();

List<SysDictDetail> sysDictDetails = getListDictDetail();

if (sysDictDetails != null && !sysDictDetails.isEmpty()) {

for (SysDictDetail sysDictDetail : sysDictDetails) {

html.append(String.format(inputRadioF,
this.getId() + "_" + sysDictDetail.getCode(),
this.getName(),
sysDictDetail.getCode(),
this.getCss(),
this.getTitle(),
this.getStyle(),
this.getValue().equals(sysDictDetail.getCode()) ? " checked=\"checked\" " : "" +
this.getOnclick() + this.getOnchange() + this.getReadonly() + this.getDisabled(),
this.getId() + "_" + sysDictDetail.getCode(),
sysDictDetail.getValue()
));
}
}
return html.toString();
}

/**
* 得到checkBox控件html
*
* @return
*/
public String getCheckboxHtml() {

StringBuffer html = new StringBuffer();

String[] vals = this.getValue().split(",");

List<SysDictDetail> sysDictDetails = getListDictDetail();

if (sysDictDetails != null && !sysDictDetails.isEmpty()) {

for (SysDictDetail sysDictDetail : sysDictDetails) {

html.append(String.format(inputCheckboxF,
this.getId() + "_" + sysDictDetail.getCode(),
this.getName(),
sysDictDetail.getCode(),
this.getCss(),
this.getTitle(),
this.getStyle(),
this.getCheckboxChecked(vals, sysDictDetail.getCode()) +
this.getOnclick() + this.getOnchange() + this.getReadonly() + this.getDisabled(),
this.getId() + "_" + sysDictDetail.getCode(),
sysDictDetail.getValue()
));
}
}
return html.toString();
}

/**
* 得到select控件html
*
* @return
*/
public String getSelectHtml() {

StringBuffer html = new StringBuffer();

html.append(String.format(inputSelectF,
this.getId(),
this.getName(),
this.getCss(),
this.getTitle(),
this.getStyle(),
this.getOnclick() + this.getOnchange() + this.getReadonly() + this.getDisabled()
));

List<SysDictDetail> sysDictDetails = getListDictDetail();

if (sysDictDetails != null && !sysDictDetails.isEmpty()) {

for (SysDictDetail sysDictDetail : sysDictDetails) {
html.append(String.format(selectOptionF,
sysDictDetail.getCode(),
sysDictDetail.getValue(),
this.getValue().equals(sysDictDetail.getCode()) ? "selected=\"selected\"" : "",
sysDictDetail.getValue()
));
}
}
return html.toString() + "</select>";
}

/**
* 得到复选框是否选中
*
* @param vals
* @param value
* @return
*/
private String getCheckboxChecked(String[] vals, String value) {

if (vals != null && vals.length > 0) {
for (String string : vals) {
if (string.equals(value)) {
return " checked=\"checked\" ";
}
}
}
return "";
}

/**
* 获取字典列表项,一级初始化的选项
*
* @return
*/
private List<SysDictDetail> getListDictDetail() {

List<SysDictDetail> list = new ArrayList<SysDictDetail>();

//添加自定义选项列表到集合中
addOptionsToList(list);

if (this.getDictcode() != null && !this.getDictcode().equals("")) {
//list.addAll(DictUtil.getDetailListByMainCode(sysCacheService.getLoginunitId(pageContext.getSession().getId()), this.getDictcode()));
}
return list;
}

/**
* 添加自定义选项列表到集合中
*
* @return
*/
private void addOptionsToList(List<SysDictDetail> list) {

//字符串
if (this.getInitoption() instanceof String) {

String temp = (String) this.initoption;
String[] atemp = temp.split(":");
if (atemp != null) {
for (String astr : atemp) {
String[] items = astr.split(",");
if (items.length == 2) {
SysDictDetail sysDictDetail = new SysDictDetail();
sysDictDetail.setCode(items[0]);
sysDictDetail.setValue(items[1]);
list.add(sysDictDetail);
}
}
}
} else if (this.getInitoption() instanceof ArrayList<?>) {//列表

List<Map<String, Object>> initList = (ArrayList<Map<String, Object>>) this.initoption;
for (Map<String, Object> obj : initList) {
SysDictDetail sysDictDetail = new SysDictDetail();
sysDictDetail.setCode(obj.get("value").toString());
sysDictDetail.setValue(obj.get("text").toString());
list.add(sysDictDetail);
}
}
}

public String processOnClick() {

return " onclick=\"" + onclick + "\" ";
}

public String processOnChange() {
return css;
}

public String processReadonly() {
return css;

}

public String processDisabled() {

return css;
}

public String getId() {

if (id == null) {
return "";
}
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {

if (name == null) {
return "";
}
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {

if (type == null) {
return "";
}
return type;
}

public void setType(String type) {
this.type = type;
}

public String getDictcode() {
if (dictcode == null) {
return "";
}
return dictcode;
}

public void setDictcode(String dictcode) {
this.dictcode = dictcode;
}

public String getTitle() {
if (title == null) {
return "";
}
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getValue() {

if (value == null) {
return "";
}
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getCss() {
if (css == null) {
return "";
}
return css;
}

public void setCss(String css) {
this.css = css;
}

public String getStyle() {

if (style == null) {
return "";
}
return style;
}

public void setStyle(String style) {
this.style = style;
}

public String getOnclick() {

if (StringUtils.isNotBlank(onclick)) {
return " onclick=\"" + onclick + "\" ";
}
return "";
}

public void setOnclick(String onclick) {
this.onclick = onclick;
}

public Object getInitoption() {
return initoption;
}

public void setInitoption(Object initoption) {
this.initoption = initoption;
}

public String getReadonly() {

if (StringUtils.isNotBlank(readonly) && readonly.equals("readonly")) {
return " readonly=\"readonly\" ";
}
return "";
}

public void setReadonly(String readonly) {
this.readonly = readonly;
}

public String getOnchange() {

if (StringUtils.isNotBlank(onchange)) {
return " onchange=\"" + onchange + "\" ";
}
return "";
}

public void setOnchange(String onchange) {
this.onchange = onchange;
}

public String getDisabled() {

if (StringUtils.isNotBlank(disabled) && disabled.equals("disabled")) {
return " disabled=\"disabled\" ";
}
return "";
}

public void setDisabled(String disabled) {
this.disabled = disabled;
}
}

在页面引入标签

<%@ taglib uri="/h2y-tags" prefix="h2y" %>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: