Skip to content

Commit

Permalink
完成comboBoxEditor的tag
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix committed Mar 13, 2016
1 parent 4f23d03 commit cdf69bc
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class BaseHtmlElementBodyTag extends RequestContextAwareTag impl
public static final String DATA_DEFINE_PREFIX = "data_define_";
public static final String VALUE_MAP_KEY = "GRID_MAP";

private BodyContent bodyContent;
protected BodyContent bodyContent;

protected String cssClass;
protected String cssStyle;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cn.wujc.web.servlet.tags.easyui.form;

import cn.wujc.web.servlet.tags.easyui.grid.editor.ComboBoxEditorTag;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
Expand All @@ -16,7 +19,10 @@
public class ComboBoxTag extends TagSupport {
private static final Logger LOG = LoggerFactory.getLogger(ComboBoxTag.class);
private static final ObjectMapper MAPPER = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

//The underlying data value name to bind to this ComboBox. Default "value"
private String valueField;
Expand All @@ -36,7 +42,9 @@ public class ComboBoxTag extends TagSupport {
private String method;
//The list data to be loaded.example:
//data: [{label: 'java',value: 'Java'},{label: 'perl',value: 'Perl'},{label: 'ruby',value: 'Ruby'}]
private String data;
@JsonIgnore
private String jsonData;
private JsonNode data;
//The additional parameters that will be sent to server when requesting remote data.Default {}
private String queryParams;
//Defines how to filter the local data when 'mode' is set to 'local'. The function takes two parameters:
Expand Down Expand Up @@ -109,11 +117,20 @@ public void setMethod(String method) {
this.method = method;
}

public String getData() {
public String getJsonData() {
return jsonData;
}

public void setJsonData(String jsonData) throws IOException {
this.jsonData = jsonData;
this.data = MAPPER.readTree(jsonData);
}

public JsonNode getData() {
return data;
}

public void setData(String data) {
public void setData(JsonNode data) {
this.data = data;
}

Expand Down Expand Up @@ -164,10 +181,6 @@ public int doEndTag() throws JspException {
} catch (IOException e) {
LOG.error("", e);
}
ComboBoxEditorTag tag = (ComboBoxEditorTag) findAncestorWithClass(this, ComboBoxEditorTag.class);
if (tag != null) {
tag.setComboBox(this);
}
return EVAL_PAGE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@
import edu.scup.web.sys.entity.SDictGroup;
import edu.scup.web.sys.service.SystemService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.tags.form.TagWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EDataGridColumnTag extends BaseHtmlElementBodyTag implements Cloneable {
private static final long serialVersionUID = 1403854988552009583L;
private static final Logger LOG = LoggerFactory.getLogger(EDataGridColumnTag.class);
private static final ObjectMapper mapper = new ObjectMapper();

private TagWriter tagWriter;
@Autowired
private SystemService systemService;
@Autowired
Expand All @@ -47,25 +51,22 @@ public class EDataGridColumnTag extends BaseHtmlElementBodyTag implements Clonea

@Override
protected int writeTagContent(TagWriter tagWriter) throws JspException {
this.tagWriter = tagWriter;
if (systemService == null) {
WebApplicationContext wac = getRequestContext().getWebApplicationContext();
AutowireCapableBeanFactory acbf = wac.getAutowireCapableBeanFactory();
acbf.autowireBean(this);
}

EDataGridColumnTag columnTag = this.clone();

boolean dicCombobox = StringUtils.equals("combobox", editor) && StringUtils.isNotBlank(dictionary);
if (dicCombobox) {
columnTag.setEditor("{type: 'combobox', options: { data: " + DATA_DEFINE_PREFIX + dictionary
setEditor("{type: 'combobox', options: { data: " + DATA_DEFINE_PREFIX + dictionary
+ ",valueField: 'value',textField: 'display',required:" + required + "}}");
} else if (StringUtils.equals("validatebox", editor) && StringUtils.isNotBlank(validType)) {
columnTag.setEditor("{type: 'validatebox', options: {validType:'" + validType + "'}}");
setEditor("{type: 'validatebox', options: {validType:'" + validType + "'}}");
}

if (StringUtils.isNotBlank(dictionary) && StringUtils.isBlank(formatter)) {
columnTag.setFormatter("format_" + dictionary);
setFormatter("format_" + dictionary);
} else if (isImage) {
String style = "";
if (StringUtils.isNotBlank(imageSize)) {
Expand All @@ -75,7 +76,7 @@ protected int writeTagContent(TagWriter tagWriter) throws JspException {
style += "height=" + size[1] + " ";
}
}
columnTag.setFormatter("function(value,rec,index){return '<image border=0 " + style + " src='+value+'/>';}");
setFormatter("function(value,rec,index){return '<image border=0 " + style + " src='+value+'/>';}");
}
if (StringUtils.isNotBlank(dictionary)) {
StringBuilder js = getSnippets(KEY_JS);
Expand All @@ -86,7 +87,7 @@ protected int writeTagContent(TagWriter tagWriter) throws JspException {
boolean addFormatter = formatter.length() < 10;
boolean defineData = dataDefine.length() < 10;
if (!addFormatter && !defineData) {
getEDataGridTag().addColumn(columnTag);
getEDataGridTag().addColumn(this);
return EVAL_PAGE;
}
if (addFormatter) {
Expand Down Expand Up @@ -132,8 +133,24 @@ protected int writeTagContent(TagWriter tagWriter) throws JspException {
}
}
}
getEDataGridTag().addColumn(columnTag);
return EVAL_BODY_INCLUDE;

return EVAL_BODY_BUFFERED;
}

@Override
public int doAfterBody() throws JspException {
JspWriter out = bodyContent.getEnclosingWriter();
try {
out.print(bodyContent.getString());
} catch (IOException e) {
LOG.error("", e);
}
return SKIP_BODY;
}

public int doEndTag() throws JspException {
getEDataGridTag().addColumn(this);
return super.doEndTag();
}

private void setFormatter(StringBuilder formatter, Map<String, Object> map) {
Expand Down Expand Up @@ -265,7 +282,7 @@ public void setRequired(boolean required) {
}

@Override
public EDataGridColumnTag clone() {
public EDataGridColumnTag clone() {//要保存Field变量给最外部的GridTag使用,更好的方式是再新建一个VO
try {
return (EDataGridColumnTag) super.clone();
} catch (CloneNotSupportedException ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public void setSortOrder(String sortOrder) {
}

public void addColumn(EDataGridColumnTag column) {
this.columns.add(column);
this.columns.add(column.clone());//JspTag会被复用,Field变量会被修改
}

public void addToolbar(DataGridToolBarTag dataGridToolBarTag) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
package cn.wujc.web.servlet.tags.easyui.grid.editor;

import cn.wujc.web.servlet.tags.easyui.grid.EDataGridColumnTag;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.wujc.web.servlet.tags.easyui.form.ComboBoxTag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ComboBoxEditorTag extends BodyTagSupport {
private static final Logger LOG = LoggerFactory.getLogger(ComboBoxEditorTag.class);
private ComboBoxTag comboBox;

public ComboBoxTag getComboBox() {
return comboBox;
}

public void setComboBox(ComboBoxTag comboBox) {
this.comboBox = comboBox;
}
private static final ObjectMapper MAPPER = new ObjectMapper().configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

@Override
public int doAfterBody() throws JspException {
return SKIP_BODY;
}

@Override
public int doEndTag() throws JspException {
EDataGridColumnTag parent = (EDataGridColumnTag) findAncestorWithClass(this, EDataGridColumnTag.class);
if (parent == null) {
throw new JspException("ComboBoxEditorTag must in EDataGridColumnTag");
}
String comboBox = bodyContent.getString();
Map<String, Object> editor = new HashMap<>();
editor.put("type", "combobox");
try {
pageContext.getOut().print(new ObjectMapper().writeValueAsString(comboBox));
editor.put("options", MAPPER.readTree(comboBox));
} catch (IOException e) {
throw new JspException(e);
}
try {
parent.setEditor(MAPPER.writeValueAsString(editor));
} catch (JsonProcessingException e) {
LOG.error("", e);
}
return EVAL_PAGE;
return SKIP_BODY;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/easyui.tld
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@
<description>A URL to load list data from remote.</description>
</attribute>
<attribute>
<name>data</name>
<name>jsonData</name>
<description>The list data to be loaded.example:
data: [{label: 'java',value: 'Java'},{label: 'perl',value: 'Perl'},{label: 'ruby',value: 'Ruby'}]
</description>
Expand Down

0 comments on commit cdf69bc

Please sign in to comment.