JSP中输出Enum

来源:互联网 发布:mac系统怎么关闭程序 编辑:程序博客网 时间:2024/05/17 08:02

Enum类

/** *  */package com.example.enums;/** * @author Administrator * */public enum EnumType {    BLOG("blog", "博客"),    QUESTION("question", "提问"),    ESSAY("essay", "随笔");    private String code;    private String desc;    private EnumType(String code, String desc) {        this.code = code;        this.desc = desc;    }    public static EnumType getCode(String code) {        for (EnumType type : EnumType.values()) {            if (type.getCode().equals(code)) {                return type;            }        }        return null;    }    /**     * @return the code     */    public String getCode() {        return code;    }    /**     * @param code the code to set     */    public void setCode(String code) {        this.code = code;    }    /**     * @return the desc     */    public String getDesc() {        return desc;    }    /**     * @param desc the desc to set     */    public void setDesc(String desc) {        this.desc = desc;    }}

页面

<%@page import="com.example.enums.EnumType"%><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><c:set var="types" value="<%= EnumType.values() %>"/><c:forEach var="type" items="${types}">    ${type.getCode() }, ${type.getDesc() }</c:forEach>

参考链接:http://blog.csdn.net/kinginblue/article/details/51458576

原创粉丝点击