Alexander K
Alexander K

Reputation: 65

Struts2 disable <s:textfield> using some expression

I have two roles in my app, ROLE_ADMIN & ROLE_USER Also I have a form with some fields. I would like to have the following: When user has ADMIN role he is able to edit specific group of fields, when user has USER role he is not able to edit these fields. I don't want to have two different forms, or two copies of fields for every role.

I tried to do something like that:

<s:textfield key="..." disabled="${myFlag}" >, where myFlag evaluated from roles.

But I have warning "disabled" does not support runtime expressions.

Is there any way to do this? Or maybe there's better way, for example, by replacing generated input to label ?

Thanks in advance.

Upvotes: 4

Views: 18594

Answers (2)

lschin
lschin

Reputation: 6811

You should use the OGNL expression by %{expression} syntax in a Struts2 tag.

<s:textfield disabled="%{myFlag}" />

how can I set such variable on page?

Doesn't work with %{myFlag}? Or you could set the the variable by

<s:set var="flag">${myFlag}</s:set>

Implementation :

<s:textfield disabled="#flag" />

Upvotes: 14

Anupam
Anupam

Reputation: 8016

Add cssClass attribute to those fields and write a javascript function which should e called on body onload event

function calledonbodyload() {
 <s:if test="%{role=='ROLE_USER'}">
   document.getElementsByClass("yourClass").disabled=true;
 </s:if>
}

I am not sure about getElementByClass() in plain javascript but jQuery's $(".yourClass").attr('disabled',true); worlks perfectly

Upvotes: 0

Related Questions