jsp页面引入jstl标签:1.网上下载jstl.jar包和standard.jar包放到项目的lib文件夹下,jar包下载:
jar包下载;
2.然后在你的jsp页面里引入如下代码:
1 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
ok了,在你的页面使用c标签吧~
下面来说说自定义标签的开发:jsp自定义标签,用于项目便捷开发。在实际项目开发中,我们大多数时候会采用数据字典来储存项目中一些数据,比如性别、国际、类型等,用数据字典存储很 方便,因此在数据库中添加一张数据字典表t_dict_value,有做过的项目中采用两张表进行数据字典的管理,个人在设计数据字典的时候感觉一张表也 够用了,不多说看建表语句:
我的自定义标签是基于数据字典表使用的,当然后续业务中有需要也可以制作特定的自定义标签,接下来开始自定义标签,首先写一个DictSelectTag类,代码如下:
package com.infopatent.juangetljc.web.controller.core;import java.io.IOException;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.lang3.StringUtils;
import com.infopatent.juangetljc.core.DictValue;
public class DictSelectTag extends TagSupport {
private String dictName = "";
private boolean defaultValue;
private String value;
private String name;
private String id;
private String cssClass;
private String styleClass;
private String multiple;
private String onChange;
private String dataType;
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public String getCssClass() {
return cssClass;
}
public void setCssClass(String cssClass) {
this.cssClass = cssClass;
}
public String getStyleClass() {
return styleClass;
}
public void setStyleClass(String styleClass) {
this.styleClass = styleClass;
}
public String getMultiple() {
return multiple;
}
public void setMultiple(String multiple) {
this.multiple = multiple;
}
public String getOnChange() {
return onChange;
}
public void setOnChange(String onChange) {
this.onChange = onChange;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getDictName() {
return dictName;
}
public void setDictName(String dictName) {
this.dictName = dictName;
}
public boolean isDefaultValue() {
return defaultValue;
}
public void setDefaultValue(boolean defaultValue) {
this.defaultValue = defaultValue;
}
@Override
public int doEndTag() throws JspException{
DictValue dict = new DictValue();
List<DictValue> dict_list = dict.getRepository().findByProperty(DictValue.class,"dictName",dictName);
JspWriter out = pageContext.getOut();
StringBuffer sb = new StringBuffer();
sb.append("<select name='"+this.getName()+"' id='"+this.getId()+"' dataType='"+this.getDataType()+"'");
if(!StringUtils.isEmpty(this.getCssClass())){
sb.append("class=\"" + this.getCssClass() + "\" ");
}
if(!StringUtils.isEmpty(this.getStyleClass())){
sb.append("style=\"" + this.getStyleClass() + "\" ");
}
if(!StringUtils.isEmpty(this.getMultiple())){
sb.append("multiple=\"" + this.getMultiple() + "\" ");
}
if(!StringUtils.isEmpty(this.getOnChange())){
sb.append("onchange=\"" + this.getOnChange() + "\" ");
}
sb.append(">");
if(this.isDefaultValue()){
sb.append("<option value=''>--请选择--</option>");
}
for(DictValue dc:dict_list){
if(dc.getItemDesc().equals(this.getValue())){
sb.append("<option value='"+dc.getItemCode()+"' selected='selected'>");
}else{
sb.append("<option value='"+dc.getItemCode()+"'>");
}
sb.append(dc.getItemDesc()+"</option>");
}
sb.append("</select>");
try {
out.write(sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
throw new JspException(e);
}
return TagSupport.EVAL_PAGE;
}
}
再写一个DictLabelTag类用于显示字典中的值,代码如下:
package com.infopatent.juangetljc.web.controller.core;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.springframework.web.servlet.tags.form.OptionsTag;
import com.infopatent.juangetljc.core.DictValue;
public class DictLabelTag extends TagSupport {
private String dictName = "";
private String itemCode;
public String getDictName() {
return dictName;
}
public void setDictName(String dictName) {
this.dictName = dictName;
}
public String getItemCode() {
return itemCode;
}
public void setItemCode(String itemCode) {
this.itemCode = itemCode;
}
@Override
public int doEndTag() throws JspException {
DictValue dict = new DictValue();
JspWriter out = pageContext.getOut();
try {
out.write(dict.codeToName(getDictName(),getItemCode()));
} catch (IOException e) {
throw new JspException(e);
}
return TagSupport.EVAL_PAGE;
}
}
接下来配置tld文件,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>SPay JSP Form Tag Library</description>
<tlib-version>1.0</tlib-version>
<short-name>dict</short-name>
<uri>http://www.tljc.com/dict_tag</uri>
<tag>
<description>Renders an HTML 'select' element. Supports databinding to the selected option.</description>
<name>select</name>
<tag-class>com.infopatent.juangetljc.web.controller.core.DictSelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>defaultValue</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>dataType</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>dictName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cssClass</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>styleClass</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>multiple</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>onChange</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>itemdesc</name>
<tag-class>com.infopatent.juangetljc.web.controller.core.DictLabelTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<description>The Dict Name config in dict_value</description>
<name>dictName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>The Dict Code config in dict_value</description>
<name>itemCode</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
接下来在jsp页面里引入标签:
<%
@taglib
prefix=
"dict"
uri=
"http://www.tljc.com/dict_tag"
%>
这样便可以在jsp页面里使用定义的标签了:
<dict:select defaultValue=
"true"
name=
"GJ"
id=
"GJ"
dictName=
"GJ"
cssClass=
"form-control"
/>
前提是要在字典表里加上“GJ”这条数据。
OK了!
posted on 2014-05-21 17:17
小人物_Amor 阅读(11587)
评论(6) 编辑 收藏 所属分类:
web