本文介绍了枚举在JSP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在不使用scriptlet的情况下在JSP中使用Enum值。
例如。
package com.example;
public enum Direction {
ASC,
DESC
}
所以在JSP中我想做这样的事情
< c:if test = $ {foo.direction ==<%com.example.Direction.ASC%>}> ...
解决方案
您可以将枚举中方向的网络友好文本实现为字段:
<%@ page import =com.example.Direction%>
...
< p> Direction is< ;%= foo.direction.getFriendlyName()%>< / p>
<%if(foo.direction == Direction.ASC){%>
< p>这意味着你'重返天堂!< / p>
<%}%>
但是混合了视图和模型,虽然对于简单的用途,它可以是视图无关的(升序,降序等)。
除非你不喜欢将Java直接放入JSP页面,即使用于比较等基本内容/ p>
Is there a way to use Enum values inside a JSP without using scriptlets.
e.g.
package com.example;
public enum Direction {
ASC,
DESC
}
so in the JSP I want to do something like this
<c:if test="${foo.direction ==<% com.example.Direction.ASC %>}">...
解决方案
You could implement the web-friendly text for a direction within the enum as a field:
<%@ page import="com.example.Direction" %>
...
<p>Direction is <%=foo.direction.getFriendlyName()%></p>
<% if (foo.direction == Direction.ASC) { %>
<p>That means you're going to heaven!</p>
<% } %>
but that mixes the view and the model, although for simple uses it can be view-independent ("Ascending", "Descending", etc).
Unless you don't like putting straight Java into your JSP pages, even when used for basic things like comparisons.
这篇关于枚举在JSP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!