Reputation: 5679
I want to create a custom tag which would mark a field readonly on JSP. Any suggestions how to accomplish this?
Upvotes: 0
Views: 8366
Reputation: 90467
How about this? You have a bean which has several checking methods to return the boolean checking result. Then use the <c:choose>
,<c:when>
and <c:otherwise>
to simulate the if -then-else
<c:choose>
<c:when test="${!bean.isAuthorized}">
<input type="text" name="name" readonly="readonly">
</c:when>
<c:when test="${bean.isCondition2}">
...........
</c:when>
<c:when test="${bean.isCondition3}">
...........
</c:when>
<c:otherwise>
...............
</c:otherwise>
</c:choose>
Upvotes: 0
Reputation: 175
I'm not sure if a custom tag would be the way to go here..
I understand that you don't want to bloat your code with multiple if else statements surrounding your readonly attributes.. If you have say 20 input fields that all need to be made readonly depending when a single condition is met (e.g. when a user has readonly privaleges).. you don't really want the processing overhead of running that single check 20 times..
Have you considered creating a seperate readonly view of your form? and and wrapping your if else condition around a set of includes? ok - you've got code replication (which there are ways to minimise), but it means you've got only 1 check instead of 20? Another advantage of keeping the read only view seperate would be that the data doesn't necessarily have to be displayed inside form elements and you're free to style how you want..
Upvotes: 0
Reputation: 5796
I guess there are elegant and ugly ways to solve this, I would start with the standard if
tag unless you have particular needs, like this:
<input type="text" name="name" <c:if test="condition">readonly</c:if>>
Upvotes: 2