zoukankan      html  css  js  c++  java
  • java 前台使用枚举方法(二)

    最近发现,前台jsp使用枚举,有一个更方便的方法。

    首先 枚举类的封装大家看一下:http://blog.csdn.net/hanjun0612/article/details/72845960

    然后,我们创建一个class

    package com.kintech.web.tag;
    
    import java.io.IOException;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
    import com.kintech.common.utils.EnumHelper;
    
    @SuppressWarnings("all")
    public class EnumTag extends SimpleTagSupport {
    	private Integer key;
    	private Class cs;
    	private String style = "0";//是否支持样式,0为否,1为是
    
    	@Override
    	public void doTag() throws JspException, IOException {
    		JspWriter out = getJspContext().getOut();
    		String html = "";
    		if ("1".equals(style)) {
    			html = "<span class=' " + EnumHelper.getEnumItem(cs, key).getItemStyleClass() + "' >"
    					+ EnumHelper.getEnumLangItem(cs, key) + "</span>";
    
    		} else {
    			html = "<span class='' >" + EnumHelper.getEnumLangItem(cs, key) + "</span>";
    
    		}
    
    		out.println(html);
    	}
    
    	public Integer getKey() {
    		return key;
    	}
    
    	public void setKey(Integer key) {
    		this.key = key;
    	}
    
    	public Class getCs() {
    		return cs;
    	}
    
    	public void setCs(Class cs) {
    		this.cs = cs;
    	}
    
    	public String getStyle() {
    		return style;
    	}
    
    	public void setStyle(String style) {
    		this.style = style;
    	}
    	
    }
    



    最后前台只要这么用:

    <c:forEach items="${pageResult.list }" var="item">
                                    <tr>
                                        <td><enum:enumTag  style="1" key="${item.state}" cs="<%=EnumList.IsDeleteEnum.class %>" /></td>
                                    </tr>
                                </c:forEach>

    或者

    (th:selected这一块是默认选中的,大家可以无视)

    <select class="select01" name="isValid" onchange="form1.submit();">
    <option value="">请选择状态</option>
    <option                            
    th:each="e:${T(com.kps.common.utils.EnumHelper).getEnumList(T(com.kps.common.EnumList.ValidEnum))}" th:value="${e.itemKey}" th:selected=" ${e.itemKey} eq ${param.isValid!=null?param.isValid[0]:''} ">[[${e.itemValue}]] </option>


    pageResult.list是action返回的模型集合。

    item.state  是值。

    EnumList.IsDeleteEnum.class  就是我们的枚举值了。

  • 相关阅读:
    Mvc+三层(批量添加、删除、修改)
    js中判断复选款是否选中
    EF的优缺点
    Git tricks: Unstaging files
    Using Git Submodules
    English Learning
    wix xslt for adding node
    The breakpoint will not currently be hit. No symbols have been loaded for this document."
    Use XSLT in wix
    mfc110ud.dll not found
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/9779759.html
Copyright © 2011-2022 走看看