java_newbie
java_newbie

Reputation: 871

inject style attribute based on parameter from Action in select item on JSP

I've been trying to find out the solution for hours but still failed :(

here's simple div on my JSP:

<div class="message" >
    HELLO USER!
</div>

I want to it to have style like this:

<div class="message" style="display : none;" >
    HELLO USER!
</div>

but this style is dependent on list object size which is coming from Struts Action, so this JSP is a result for the Action success. I was trying smthg like just for test injecting value:

<s:set name="divStyle" value="display: none;" />
<div class="message" style="<s:property value="countyStyle" />" >
     HELLO USER!
</div>

and I tried:

<s:set name="divStyle" value="<s:if test="elementsFromAction.size > 1" >display: none;</s:if>" />
<div class="message" style="<s:property value="divStyle" />" >
     HELLO USER!
</div>

and:

<div class="message" style="<s:if test="elementsFromAction.size > 1" >display: none;</s:if>" />" >
     HELLO USER!
</div>

but it didn't work too :(

Could someone give an advice how to inject that style? thx.

Upvotes: 0

Views: 1719

Answers (3)

Georgmster
Georgmster

Reputation: 1

Given another solution.

load your style into the data bean.

And in the page onload function you can handle them and translate them to what you like.

which can avoid some trouble when the logic is too complicated.

Upvotes: 0

Andrea Ligios
Andrea Ligios

Reputation: 50281

In the <s:set/> snippet, you are nesting Struts tags, that is an error.

In your last snippet, there is a typo: you are closing an un-opened tag with />.

Change it from

<div class="message" 
     style="<s:if test="elementsFromAction.size > 1">display: none;</s:if>" />" >
     HELLO USER!
</div>

to

<div class="message" 
     style="<s:if test="elementsFromAction.size > 1">display: none;</s:if>" >
     HELLO USER!
</div>

Upvotes: 1

Predrag Maric
Predrag Maric

Reputation: 24433

Try this

<div class="message" style="${elementsFromAction.size gt 1 ? 'display : none;' : ''}" >
    HELLO USER!
</div>

Upvotes: 1

Related Questions