Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If using struts2 I would stick to struts2 tags:</p> <p><a href="http://struts.apache.org/2.2.1/docs/tag-reference.html" rel="nofollow">http://struts.apache.org/2.2.1/docs/tag-reference.html</a></p> <pre><code>&lt;s:if test="%{false}"&gt; &lt;div&gt;Will Not Be Executed&lt;/div&gt; &lt;/s:if&gt; &lt;s:elseif test="%{true}"&gt; &lt;div&gt;Will Be Executed&lt;/div&gt; &lt;/s:elseif&gt; &lt;s:else&gt; &lt;div&gt;Will Not Be Executed&lt;/div&gt; &lt;/s:else&gt; </code></pre> <p>A notable difference is that Struts2 uses OGNL. It is quite easy to use.</p> <p>I use an interceptor to solve the issue of testing if a user is logged on or not and then letting them get to the required page or directing them to the login screen.</p> <p>The basic procedure is this: Create a log in action which places a User object in the session. Create a log in interceptor that checks if there is a User object in the session, if there is the interceptor will pass execution on, if not it will return "login", there will need to be a global mapping of "login" to the login action... then you're done. Well you'll of course need to add the login action to that stack that will be used in a package of "secure" actions.</p> <p><strong>Edit</strong>: Here is how I control if a user sees a "log in" link or a "log out username" link on a page:</p> <pre><code>&lt;s:if test="#session['User']==null"&gt; &lt;s:a namespace="/public" action="welcome" &gt;Login&lt;/s:a&gt; &lt;/s:if&gt; &lt;s:else&gt; &lt;s:a namespace="/public" action="logout" &gt;Logout &lt;s:property value="#session['User'].name"/&gt;&lt;/s:a&gt; &lt;/s:else&gt; </code></pre> <p>An interceptor will help you keep track of what pages need a user to be logged in so you don't check this in every action (in case a non authorized user simply types in the url of a pass word protected page) but it will not help in the rendering.</p> <p><strong>Edit:</strong> Try the following:</p> <p>1) The following is the preferred struts2 way for conditionals in your jsp. I tested this against your bean so it works <em>exactly</em> as is =)</p> <pre><code>&lt;s:div&gt; &lt;s:if test="loggedOn"&gt; DIV1 &lt;/s:if&gt; &lt;s:else&gt; DIV2 &lt;/s:else&gt; &lt;/s:div&gt; </code></pre> <p>2) Once your conditional is working perhaps you want more than one jsp to respond if you are logged in... you can't have them log in for every page only to have it forget that you logged in... I suppose you <em>could</em> so lets put the fact that you logged in into the session so every jsp in your application can check this...</p> <p>To do this all you need to do is add the "SessionAware" interface to your Action and your IDE should force you to add the abstract method (a setter) and you'll use that setter to set a session variable... Or more plainly in code:</p> <pre><code>package struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.Map; import org.apache.struts2.interceptor.SessionAware; public class test extends ActionSupport implements SessionAware{ private Map&lt;String, Object&gt; session; @Override public void setSession(Map&lt;String, Object&gt; session) { this.session = session; } } </code></pre> <p>Now once you have added the "SessionAware" interface so you can set a session field in your class then you can add...</p> <pre><code>public void setLoggedOn(boolean loggedOn) { //this.loggedOn = loggedOn; session.put("isLoggedOn", true); } </code></pre> <p>Now in any jsp... you can look into the session variable for the objects that you put into the map...</p> <p>So we should now be able to change your conditional display to...</p> <pre><code>&lt;s:div&gt; &lt;s:if test="#session['isLoggedOn'] == true"&gt; DIV1 &lt;/s:if&gt; &lt;s:else&gt; DIV2 &lt;/s:else&gt; &lt;/s:div&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload