Get session value in struts/sitemesh - struts2

I'm using sitemesh with struts2. I want to create a login/logout link on the main layout page. How do we get the value of session there.

Simple example
decorators.xml
<decorators defaultdir="/decorators">
<decorator name="default" page="default.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
/decorators/default.jsp
With Struts2 Tag
<s:set var="isNoLogin">${empty pageContext.request.remoteUser}</s:set>
<s:if test="#isNoLogin">
<s:a action="login">Login</s:a>
</s:if>
<s:else>
<s:a action="logout">Logout</s:a>
</s:else>
or
<s:set var="remoteUser">${pageContext.request.remoteUser}</s:set>
<s:if test="#remoteUser != null && !#remoteUser.isEmpty()">
<s:a action="login">Login</s:a>
</s:if>
<s:else>
<s:a action="logout">Logout</s:a>
</s:else>
With JSTL
<c:if test="${empty pageContext.request.remoteUser}">
<s:a action="login">Login</s:a>
</c:if>
<c:if test="${not empty pageContext.request.remoteUser}">
<s:a action="logout">Logout</s:a>
</c:if>
or
<c:choose>
<c:when test="${empty pageContext.request.remoteUser}">
<s:a action="login">Login</s:a>
</c:when>
<c:otherwise>
<s:a action="logout">Logout</s:a>
</c:otherwise>
</c:choose>

Related

Dynamic cc:insertChildren render

I got the problem when control to render cc:insertChildren dynamically.
<cc:implementation>
My text 1
<c:if test="#{testData}">
<cc:insertChildren /> <!-- My content will be loaded here-->
</c:if>
My text 2
<c:if test="#{!testData}">
<cc:insertChildren /> <!-- or other will be loaded here-->
</c:if>
</cc:implementation>
but the content rendered in both cc:insertChildren tags could not be rendered.
Do you have any ideas ?
Thank you.

Unable to get values in JSP in Struts2 Liferay6.2 Portlet

Brief Overview: I am trying to port a webapp based on Struts2 ,into a Struts2 Liferay portlet.
Problem definition: The values are fetched as NULL/Blank even if if the action Class method is setting the values properly in my variables.
I have tried printing out the values when my method is returned in Action Class and also in my jsp. The values are getting properly set as I can see it from the loggers put in place just before returning SUCCESS, but in the jsp I can see all the required set variables are blank!!
Code snippet:
In jsp the code is fetching the values as below:
For instance
<br>
<s:if test="%{xyz!= null}">
<br />
<div style="margin-left:9em;">
<s:property value="xyz" escapeHtml="false"/> //never displayed as xyz is NULL
</div>
<br /><br />
</s:if>
<br>
on printing out xyz: <s:text name="%{xyz}"/> , I get blank values , this happens for all the required values in the jsp.
The options I have tried out:
I have tried using : %{#xyz}, %{xyz}, but didn't helped.
struts.xml
<action name="selectSite" class="com.abc.xyz.LandingPageAction"
method="selectSite">
<interceptor-ref name="abAjaxCallInterceptorStack" />
<result name="success" >/WEB-INF/jsp/portal/LandingContent.jsp</result>
</action>
liferay-portlet.xml:
<liferay-portlet-app>
<portlet>
<portlet-name>xyz-portlet</portlet-name>
<icon>/icon.png</icon>
<private-session-attributes>false</private-session-attributes>
<requires-namespaced-parameters>false</requires-namespaced-parameters>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
<add-default-resource>true</add-default-resource>
</portlet>
</liferay-portlet-app>
jsp page:
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<br>
<s:if test="%{xyz!= null}">
<br />
<div style="margin-left:9em;">
<s:property value="xyz" escapeHtml="false"/>
</div>
<br /><br />
</s:if>
<br>
<br />
<sj:tabbedpanel id="billTab" >
<sj:tab id="overview" href="%{overviewURL}" label="Overview"
target="overviewDiv" indicator="loadingInd" />
<sj:tab id="invoices" href="%{invoiceURL}" label="Invoices"
target="invoicesDiv" indicator="loadingInd" />
<div id="overviewDiv" ></div>
<div id="invoicesDiv" ></div>
</sj:tabbedpanel>
try
<s:if test="xyz!= null">
if xyz is on the valuestack then you don't need to use %
You can also use Set tag to expose these values such as
<s:set var="varName" value="xyz"/>then just refer to it as varName instead of #varName
If you are accessing vars in a different portlet you will have set the value for the variable as at least request scope like this
<s:set var="varName" value="xyz" scope="request"/>
Then you can access them like this from another portlet in the same page
<s:if test="#request.xyz != null">

How to calculate sum in nested iteration in Struts2 tags

Total spots is not printed in the span
<s:set var="totalSpots" value="0"/>
<s:iterator var="spotSchedule" value="spotSchedules">
<s:set var="totalSpots" value="#totalSpots + %{spotSchedule.noOfSpots}" />
</s:iterator>
<span style='cursor:pointer;text-decoration:underline;padding-left:5px;color:blue;'>
<s:property value="#totalSpots"/>
</span>
Use # sign to reference your spotSchedule variable inside <s:iterator> tag.
<s:set var="totalSpots" value="0"/>
<s:iterator var="spotSchedule" value="spotSchedules">
<s:set var="totalSpots" value="#totalSpots + #spotSchedule.noOfSpots" />
</s:iterator>
<span style='cursor:pointer;text-decoration:underline;padding-left:5px;color:blue;'>
<s:property value="#totalSpots"/>
</span>

blank_target in struts2

can any one suggest how to write target="_blank" in struts2. I tried something like below but the hyperlink was not working
<s:url id="imageDownload" namespace="/" action="downloadImage" var="urlTag">
<s:param name="ImageFileName" value="%{ImageFileName}"></s:param>
<s:param name="id" value="%{id}"></s:param>
</s:url>
<a class="linkView" href="<s:property value="#urlTag" />" target="_blank">
<s:property value="ImageFileName" />
</a>
thanks
I think
<a class="linkView" href="<s:property value="#urlTag" />" target="_blank">
<s:property value="ImageFileName" />
</a>
is correct.

comparing two valuestack string values in JSP - struts2

Thanks in advance for your time.
I need to preselect a radio button if it has a saved value. I basically need to compare 2 strings in the valuestack to determine this.
(I can't use <s:radio at the moment because of some business rules I need to attach based on other input elements in the form).
I tried to do <s:set the value of the saved id inside s:iterate like below and then compare them like below but obviously I didnt get it right.
<s:set var="savedId" value="%{flag.problemId}"/>
<s:iterator value="problemIdList">
<s:set var="currentId" value='<s:property value="id"/>' />
<s:if test="%{#currentId.equals(#savedId)}" >
<input checked="checked" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:if>
<s:else>
<input type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:else>
</s:iterator>
Basically I need to compare the two strings, my code is below. I know I can't compare with equals() like I have below - any ideas?
Thanks a bunch!
<s:set var="savedId" value="%{flag.problemId}"/> <s:iterator value="problemIdList">
<s:if test=' <s:property value="id"/>.equals(<s:property value="savedId"/>) '>
<input checked="checked" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:if>
<s:else>
<input type="radio" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:else>
Regards,
VeeCan
Have you tried:
<s:if test='id.equals(savedId)'>
If "id" is a string OGNL will allow you to use methods of String.
Access value form <s:iterator> into <s:if> (ognl with <s:if> and <s:iterator>)
For example: Suppose loadUserList is the iterator(containing UserName and Address) to be iterated in jsp,
UserName:<s:property escape="false" value="userName" />
Address:<s:property escape="false" value="address" />
<s:if test='userName.equals("Admin")'>This is admin User</s:if>
<s:else>This is not an admin user!</s:else>
OGNL with s:if and s:iterator

Resources