因?yàn)轫?xiàng)目需要,需要做輸入框自動(dòng)提示功能,之前我只是使用過這種功能,相信大家也都使用過,百度~現(xiàn)在要讓我自己做,好吧,還是百度一下,百度搜索“輸入框自動(dòng)提示”,彈出一對頁面,也看了一堆文章,有自己開發(fā)的,也有插件的。
最后經(jīng)過幾次試驗(yàn),我選擇了JQuery.Autocomplete這款插件,還行吧用著,支持本地也支持ajax訪問服務(wù)器,這款插件是基于jquery的,各位若有項(xiàng)目需要使用的,可以嘗試一把。
源碼下載:
JQuery.AutoComplete本地自動(dòng)提示的方式我就不說了,各位下載源碼之后看一下也就明白了,當(dāng)然不明白的也可以問我,說一下ajax訪問服務(wù)器的方式吧,直接上代碼最為直接:
$(function(){
var onAutocompleteSelect =function(customer) {
$('#customerIdString').val(customer.data);
};
var options = {
serviceUrl: '${pageContext.request.contextPath}/TCustomer/getAllCustomer.koala',//獲取數(shù)據(jù)的后臺(tái)頁面
onSelect: onAutocompleteSelect,//選中之后的回調(diào)函數(shù)
extraParams: {selectedCustomerName:$.trim($("#selectedCustomerName").val())},//動(dòng)態(tài)參數(shù)值
paramName: "selectedCustomerName",
noCache: false, //是否啟用緩存 默認(rèn)是開啟緩存的
max:10,
autoFill:false
};
$('#selectedCustomerName').autocomplete(options);
});
這樣我們就可以把填入輸入框的值作為extraParams動(dòng)態(tài)參數(shù)傳遞到后臺(tái)去,后臺(tái)直接request.getParameter("selectedCustomerName");就可以了。
值得注意的是,后臺(tái)應(yīng)該返回什么樣的數(shù)據(jù)格式,這個(gè)插件需要注意的地方就是這塊了,如果不是他要求的格式的話,頁面會(huì)報(bào)js錯(cuò)誤的,要求的數(shù)據(jù)格式必須是這樣的json串:
{"query":"A","suggestions":[{"data":"114e69b4-87a9-4c2b-aed4-1727568a92a7","value":"AAA111"},{"data":"531b59ca-8618-48f4-a6e8-963320e10159","value":"小人物_Amor"}]}
query后面的A是我輸入框傳入的參數(shù),根據(jù)A模糊查詢出兩組數(shù)據(jù),并以json的格式放在key為suggestions里。
后臺(tái)代碼:
@ResponseBody
@RequestMapping("/getAllCustomer")
public Object getAllCustomer(HttpServletRequest request,HttpServletResponse response)throws IOException{
List<Object> queryCustomerList = new ArrayList<Object>();
List<TCustomerDTO> allCustomer = new ArrayList<TCustomerDTO>();
//獲取前臺(tái)帶過來的動(dòng)態(tài)參數(shù)
String selectedCustomerName = request.getParameter("selectedCustomerName");
//得到包含selectedCustomerName參數(shù)的對象
allCustomer = tCustomerApplication.findAllTCustomer();
for (Iterator iterator = allCustomer.iterator(); iterator.hasNext();) {
TCustomerDTO tCustomerDTO = (TCustomerDTO) iterator.next();
if(tCustomerDTO.getName().contains(selectedCustomerName)){
Map<String, Object> result = new HashMap<String, Object>();
result.put("value", tCustomerDTO.getName());
result.put("data", tCustomerDTO.getId());
queryCustomerList.add(result);
}
}
//構(gòu)造規(guī)定的json數(shù)據(jù)格式
Map<String, Object> query_result = new HashMap<String, Object>();
Object json = JSONArray.toJSON(queryCustomerList);
query_result.put("query", selectedCustomerName);
query_result.put("suggestions", json);
Object json_map = JSONArray.toJSON(query_result);
System.out.println(json_map);
return json_map;
}
OK了,其實(shí)很簡單,當(dāng)然你需要引入jquery,以及他所要求的其他兩個(gè)js文件,各位下載源碼后看示例就知道啦~
posted @
2014-05-22 18:11 小人物_Amor 閱讀(1399) |
評(píng)論 (0) |
編輯 收藏
jsp頁面引入jstl標(biāo)簽:1.網(wǎng)上下載jstl.jar包和standard.jar包放到項(xiàng)目的lib文件夾下,jar包下載:
jar包下載;
2.然后在你的jsp頁面里引入如下代碼:
1 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
ok了,在你的頁面使用c標(biāo)簽吧~
下面來說說自定義標(biāo)簽的開發(fā):jsp自定義標(biāo)簽,用于項(xiàng)目便捷開發(fā)。在實(shí)際項(xiàng)目開發(fā)中,我們大多數(shù)時(shí)候會(huì)采用數(shù)據(jù)字典來儲(chǔ)存項(xiàng)目中一些數(shù)據(jù),比如性別、國際、類型等,用數(shù)據(jù)字典存儲(chǔ)很 方便,因此在數(shù)據(jù)庫中添加一張數(shù)據(jù)字典表t_dict_value,有做過的項(xiàng)目中采用兩張表進(jìn)行數(shù)據(jù)字典的管理,個(gè)人在設(shè)計(jì)數(shù)據(jù)字典的時(shí)候感覺一張表也 夠用了,不多說看建表語句:

我的自定義標(biāo)簽是基于數(shù)據(jù)字典表使用的,當(dāng)然后續(xù)業(yè)務(wù)中有需要也可以制作特定的自定義標(biāo)簽,接下來開始自定義標(biāo)簽,首先寫一個(gè)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;
}
}
再寫一個(gè)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頁面里引入標(biāo)簽:
<%
@taglib
prefix=
"dict"
uri=
"http://www.tljc.com/dict_tag"
%>
這樣便可以在jsp頁面里使用定義的標(biāo)簽了:
<dict:select defaultValue=
"true"
name=
"GJ"
id=
"GJ"
dictName=
"GJ"
cssClass=
"form-control"
/>
前提是要在字典表里加上“GJ”這條數(shù)據(jù)。
OK了!
posted @
2014-05-21 17:17 小人物_Amor 閱讀(11610) |
評(píng)論 (6) |
編輯 收藏