How to use if, else condition in jsf to display image - jsf-2

I have a condition where I have an enrollment form in which if userid is 0 it should show the dummy image and when I edit user from any update, I check for if userid which is not equal to 0 then display the image corresponding to userid.
I used JSTL inside jsf page. But always it tries to go to else loop for showing image. The functionality is working fine. The only thing is I can't display the dummy image when I visit the page first. Here s my code.:
<c:if test="${'#{user.userId}' == '0'}">
<a href="Images/thumb_02.jpg" target="_blank" ></a>
<img src="Images/thumb_02.jpg" />
</c:if>
<c:otherwise>
<a href="/DisplayBlobExample?userId=#{user.userId}" target="_blank"</a>
<img src="/DisplayBlobExample?userId=#{user.userId}" />
</c:otherwise>
Can I use this JSTL tag or can I do it using jsf?

For those like I who just followed the code by skuntsel and received a cryptic stack trace, allow me to save you some time.
It seems c:if cannot by itself be followed by c:otherwise.
The correct solution is as follows:
<c:choose>
<c:when test="#{some.test}">
<p>some.test is true</p>
</c:when>
<c:otherwise>
<p>some.test is not true</p>
</c:otherwise>
</c:choose>
You can add additional c:when tests in as necessary.

It is illegal to nest EL expressions: you should inline them. Using JSTL is perfectly valid in your situation. Correcting the mistake, you'll make the code working:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jstl/core">
<c:if test="#{not empty user or user.userId eq 0}">
<a href="Images/thumb_02.jpg" target="_blank" ></a>
<img src="Images/thumb_02.jpg" />
</c:if>
<c:if test="#{empty user or user.userId eq 0}">
<img src="/DisplayBlobExample?userId=#{user.userId}" />
</c:if>
</html>
Another solution is to specify all the conditions you want inside an EL of one element. Though it could be heavier and less readable, here it is:
<img src="#{not empty user or user.userId eq 0 ? '/Images/thumb_02.jpg' : '/DisplayBlobExample?userId='}#{not empty user or user.userId eq 0 ? '' : user.userId}" target="_blank"></img>

Instead of using the "c" tags, you could also do the following:
<h:outputLink value="Images/thumb_02.jpg" target="_blank" rendered="#{not empty user or user.userId eq 0}" />
<h:graphicImage value="Images/thumb_02.jpg" rendered="#{not empty user or user.userId eq 0}" />
<h:outputLink value="/DisplayBlobExample?userId=#{user.userId}" target="_blank" rendered="#{not empty user and user.userId neq 0}" />
<h:graphicImage value="/DisplayBlobExample?userId=#{user.userId}" rendered="#{not empty user and user.userId neq 0}"/>
I think that's a little more readable alternative to skuntsel's alternative answer and is utilizing the JSF rendered attribute instead of nesting a ternary operator.
And off the answer, did you possibly mean to put your image in between the anchor tags so the image is clickable?

Related

Get anchor tag value when anchor tag is inside for each loop

<c:forEach items="${customer.iscutomer}">
<div>
<i>${customer.customerNumber}. </i>
<li>
${customer.customerName}
</li>
</div>
</c:forEach>
i don't know how to get customer name in Struts2 when user selects any hyerlink(customer name come from customer object).
You can use Struts anchor tag and add parameter to the URL.
<s:a href="url"><s:param name="customerName">${customer.customerName}</s:param>CustomerName</s:a>
Note, that you can use EL in the body of the tag.
May be this can help you to start:
<c:forEach items="${customer.iscutomer}">
<div>
<i>${customer.customerNumber}. </i>
<li>
<s:a name="foo" namespace="bar">
<s:param name="customerName">${customer.customerName}</s:param>
<s:param name="customerNubmber">${customer.customerNumber}</s:param>
</s:a>
</li>
</div>
Please consider I find that the s:a url does not add parameters to link. Now your generated link will be /foo/bar/action?customerName=joe&customerNumber=1
No in your JS you can have:
$('a').bind('click',function(){
var url = ($(this).attr('href'));
var customerName= getURLParameter(url, 'customerName');
var customerNumber= getURLParameter(url, 'customerNumber');
//calling the ajax function
pop(cat, typ)
});
If you want to have ajax calls please have a look at struts 2 jquery plugin.

struts2 fileupload maximum size error showing in form page

I am using struts2 for file uploading .When the file size exceeds configured length it is giving
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException:
How i can show in my form page this error.Because it is raising in interceptor.
Define an input result for your Action, pointing to the same JSP of the success result.
In JSP, check for actionErrors or fieldErrors, and print them if there are, like:
<s:if test="actionErrors != null && actionErrors.size > 0">
<div class="feed_ko">
<s:actionerror escape="false" />
</div>
</s:if>
<s:if test="fieldErrors != null && fieldErrors.size > 0">
<div class="feed_ko">
<s:fielderror />
</div>
</s:if>

Display table tag in struts 2

<display:table export="true" id="data" name="${sessionScope.forbesList}"requestURI="">
<a href="#" rel="tooltip" content="<span>
Name:<s:property value="#data.rank" /><br/>
Email: <a><s:property value="#data.name" /></a><br/>
Phone:<s:property value="#data.age" /> </span>"> <display:column property="rank" title="Rank" sortable="true" /></a>
<display:column property="name" title="Name" sortable="true" />
<display:column property="age" title="Age" sortable="true" />
</display:table>
In Struts 2 I'm trying to use <s:property> tag inside <display :column> tag but I can only see the <display:column> values and not the values that I access with <s:property>. How to do it?
#1: Anchor (<A>) Tag does not have any content attribute.
Please refer to the documentation to see which attributes it supports and how to use them.
#2: in HTML, when putting the character " into an attribute of a Tag, you can:
substitute " with ', like in
myAttribute="this is 'right'" or
myAttribute='this is "right" too', or
encode the " with the html entity ", like in
myAttribute="this is "right""
You must encode the < and > too, with < (Less Than) or > (Greater Than).
#3: that said, you could put your HTML inside the Anchor Tag, in the place where it is supposed to be inserted, like :
<a href="#" rel="tooltip">
Name:<s:property value="#data.rank" />
</a>
but, in your case, you are injecting a lot of HTML, including another Anchor, that is not possible. I think you should simply use a div or something similar as a container, like:
<div rel="tooltip" >
<span>
Name:<s:property value="#data.rank" /><br/>
Email: <a><s:property value="#data.name" /></a><br/>
Phone:<s:property value="#data.age" />
</span>
</div>
Resolve this before worrying about DisplayTag
EDIT
#4
Change
<s:property value="#data.rank" />
(and all the other tags) to
<s:property value="#attr.data.rank" />
#attr is the OGNL used to access the pageContext,
if it is available, otherwise it searches request, session and application scopes.

How to get rid of the auto generated j_idt ID in composite component

I'm using below composite component:
<composite:interface>
<composite:attribute name="inputId" />
</composite:interface>
<composite:implementation>
<h:panelGrid id="myTableId">
<h:inputText id="#{cc.attrs.inputId}" value="..." />
...
</h:panelGrid>
</composite:implementation>
And I'm using it in my form as below:
<h:form id="myForm">
<myCompositeComp:test inputId="myInputTextBoxId" />
<h:form>
I've verified the view source for the page and this is how it is generated:
<table id="myForm:j_idt90:myTableId">
...
<input type="text" id="myForm:j_idt90:myInputTextBoxId" />
</table>
How can I get rid of j_idt90 here? Is it the id of my composite component? I read from one of BalusC post that this issue will get fixed if I declare id as static. But I'm not able to identify the place to declare it in my code. Also can I assume <h:panelGrid> is a kind of UINamingContainer?
Yes it is the id of your composite component, <h:panelGrid> is not a UINaminContainer but the composite component is (it has to be, otherwise you would have duplicate IDs if you use it several times inside the same form for example).
Why do you need to get rid of the ID? You can set it yourself if that solves your problem:
<h:form id="myForm">
<myCompositeComp:test id="myComp" attr1="" attr2="" />
<h:form>
the genereated html should look like this:
<table id="myForm:myComp:myTableId">
....
<input type="text" id="myForm:myComp:myInputTextBoxId"
</table>

Disable Button postback in JSF2.0

My JSF page is as follows
<h:head>
<title>Ajax Test</title>
<h:outputScript name="giveEffect.js" library="jquery"/>
<h:outputScript name="jquery.js" library="jquery"/>
</h:head>
<h:body>
<div id="div1">
<h:button id="b1" onclick=" button1Click(this)" value="#{kp.firstname}"/>
<h:button id="b2" onclick="button1Click(this)" value="#{kp.home}"/>
</div>
</h:body>
</html>
button1Click is a jquery function which does ajax call and updates my page. But unable to see ajax update. Due to postback/jsf life cylce call, I changes are getting lost.
On inspecting the Dom using firebug. I found the following code.
<div id="div1">
<input id="b1" type="button" value="Kar" onclick="button1Click(this); window.location.href='/AJAXTest/faces/index.xhtml'; return false;">
<input id="b2" type="button" value="Alike" onclick="button1Click(this); window.location.href='/AJAXTest/faces/index.xhtml'; return false;">
</div>
Suppose I eliminate window.location.href='/AJAXTest/faces/index.xhtml' using firebug I get the expected output.
Is there any tag or mechanism to remove postback call?
The button component's outcome attribute is used to determine the target URL which is activated by onclick. The entire target URL string will be written out to the onclick attribute of the button as "window.location.href = ''". If you have a custom onclick method the window.location.href will be appended at the end of your script. Since you did not mention an outcome it will result in a GET request to your current view.
You can instead use
<h:commandButton> and mention type="button" like this:
<h:commandButton id="b1" onclick=" button1Click(this)" value="#{kp.firstname} "type="button">
Documentation

Resources