I have the following in my jsp page (assume that client is an object )
<%
if( client == null)
%>
NO client
<%
else
%>
<a href='page.jsp?aid=<%=client.getID()%>'> and his name is <%=client.getName()%>
thanks
You're missing the brackets:
<% if( client == null) { %>
NO client
<% } else { %>
<a href='page.jsp?aid=<%=client.getID()%>'> and his name is <%=client.getName()%>
<% } %>
That said, this is an example of bad JSP code. Consider using JSTL tags / expressions instead of scriptlets.
in jstl it would be something similar to
<c:choose>
<c:when test="${client is null}">
NO client
</c:when>
<c:otherwise>
<A href="<c:url value="page.jsp" >
<c:param name="aid" value="${client.ID}" />
</c:url>"
> and his name is <c:out value="${client.name}"/>
</c:otherwise>
Related
I have the following line of code in a JSP File in my web app t
<% if (!maintenance) { %>
<customTag:DialogEntry entry="<%=new com.presentation.generic.session.DialogEntry("dialog1","225px","true","true",new String[]{"authentification.Login.action.logInApplicationLabel"},new String[]{"logInApplication()"}) %>"/>
<% } else { %>
<customTag:DialogEntry entry="<%=new com.presentation.generic.session.DialogEntry("dialog1","225px","true","true",new String[]{},new String[]{}) %>"/>
<% } %>
The error message that I get is:
/WEB-INF/tags/DialogEntry.tag According to the TLD , the value
attribute accepts no expression
How do I resolve this problem?
DialogEntry.tag:
<%# attribute name="entry" rtexprvalue="true" type="com.presentation.generic.session.DialogEntry" %>
<%# taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%# taglib uri="http://struts.apache.org/tags-html-el" prefix="html" %>
<%# taglib uri="http://struts.apache.org/tags-bean-el" prefix="bean" %>
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%# taglib uri="http://struts.apache.org/tags-logic-el" prefix="logic" %>
<script type="text/javascript">
//confirm("<c:out value="${entry.id}" /> début");
var <c:out value="${entry.id}" /> = new YAHOO.widget.Dialog("<c:out value="${entry.id}" />",
{
width: "<c:out value="${entry.size}" />",
<c:if test="${\"\" != entry.visible}">
visible:true,
</c:if>
<c:if test="${\"\" == entry.visible}">
visible:false,
</c:if>
close:false,
draggable :false
<c:if test="${\"\" != entry.effect}">
,effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5}
} );
</c:if>
<c:if test="${\"\" == entry.effect}">
} );
</c:if>
<c:if test="${\"false\" != entry.hasButtons}">
var myButtons = [
<c:forEach var="button" items="${entry.buttons}" varStatus="ind">
{ text:"<bean:message key="${button}"/>", handler:function() {<c:out value="${entry.functions[ind.index]}" />;}, isDefault:true }
<c:if test="${!empty entry.buttons[ind.index+1]}">
,
</c:if>
</c:forEach>
];
<c:out value="${entry.id}" />.cfg.queueProperty("buttons", myButtons);
</c:if>
// confirm("<c:out value="${entry.id}" /> fin - debut script");
if($.browser.msie && $('#header').size() > 0 ){
// confirm("<c:out value="${entry.id}" /> fin - debut script IE");
var menuWidth = $("div#menu").get(0).clientWidth;
var headerHeight = $("div#header").get(0).clientHeight;
var width = document.body.clientWidth;
var contentWidthConnected = width - menuWidth;
if( $('div#content_onglets').size() > 0){
// confirm("<c:out value="${entry.id}" /> fin - debut script IE Onglet");
<c:out value="${entry.id}" />.cfg.queueProperty("x", 50);
<c:out value="${entry.id}" />.cfg.queueProperty("y", 0);
}
else{
// confirm("<c:out value="${entry.id}" /> fin - debut script IE Pas Onglet");
<c:out value="${entry.id}" />.cfg.queueProperty("x", menuWidth + 50);
<c:out value="${entry.id}" />.cfg.queueProperty("y", headerHeight );
}
// confirm("<c:out value="${entry.id}" /> fin - FIN script IE");
}
else{
<c:out value="${entry.id}" />.cfg.queueProperty("fixedcenter", true);
}
// confirm("<c:out value="${entry.id}" /> fin script");
<c:out value="${entry.id}" />.render();
// confirm("<c:out value="${entry.id}" /> fin script 2");
</script>
Either escape your inner quotes:
<% if (!maintenance) { %>
<customTag:DialogEntry entry="<%=new com.presentation.generic.session.DialogEntry(\"dialog1\",\"225px\",\"true\",\"true\",new String[]{\"authentification.Login.action.logInApplicationLabel\"},new String[]{\"logInApplication()\"}) %>"/>
<% } else { %>
<customTag:DialogEntry entry="<%=new com.presentation.generic.session.DialogEntry(\"dialog1\",\"225px\",\"true\",\"true\",new String[]{},new String[]{}) %>"/>
<% } %>
Or use single quotes:
<% if (!maintenance) { %>
<customTag:DialogEntry entry='<%=new com.presentation.generic.session.DialogEntry("dialog1","225px","true","true",new String[]{"authentification.Login.action.logInApplicationLabel"},new String[]{"logInApplication()"}) %>'/>
<% } else { %>
<customTag:DialogEntry entry='<%=new com.presentation.generic.session.DialogEntry("dialog1","225px","true","true",new String[]{},new String[]{}) %>'/>
<% } %>
Reading the error message is quoted with " which must be escaped when used within the value you see that having " inside your text is the cause. Hence, either your text is not defined with " (the second solution, where you use ') or the " inside the text have a \ to escape them and make them actual characters in the text.
Escape all " with \" inside your block:
entry=""
I need view if variable starts with keys "ef", like ef1, efabc, efanythink... and if yes, show error message, i past here mix of php and jsp, of course incorrect and with errors, i no understand jsp:
<c:if test="${empty channel.getChannelName()}">
<%
if (string_starts_with(${channelName}, 'ef')) { header("location:http://google.com"); }
or show this div of error
<div class="error"> This Channel url Portected!</div>
Original file: http://pastebin.com/ach8PXY9
<%# taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%# taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "fn" %>
<c:if test='${fn:startsWith(channel.channelName, "ef")}'>
...
</c:if>
<c:if test='${not fn:startsWith(channel.channelName, "ef")}'>
...
</c:if>
OR you can use
'<c:choose> <c:when>'
Theres a JSTL method for that!
<c:choose>
<c:when test="${fn:startsWith(channel.getChannelName(), "ef")}">
<script type="text/javascript">window.location.replace("http://google.com/");</script></c:when>
<c:otherwise>
<div class="error"> This Channel url Portected!</div>
</c:otherwise>
</c:choose>
http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/fn/tld-summary.html
also possible:
if (channelName != null && channelName.indexOf("ef") == 0) {
%><div class="error"> This Channel url Portected!</div><%
}
I have an arraylist for Strings eqArray.
I need to show it in a drop-down list for which I'm using the following in my JSP:
<%
for(int count=0;count<eqArray.size();count++){ %>
<option value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%}
%>
I have an eqName String which is part of eqArray and should be the selected value by default.
How do I go about it without having to check and set the first option as eqName always?
<% for(int count=0; count<eqArray.size(); count++){ %>
<option value="<%= eqArray.get(count) %>" <%= (eqArray.get(count).equals("eqName"))?"selected":"" %> ><%= eqArray.get(count) %></option>
<%} %>
Change the index of eqName element to 0 in the array, or use a conditional Statement.
<%
for(int count=0; count < eqArray.size(); count++){ %>
<%if(eqArray.equals("eqName"){ %>
<option selected="selected" value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%} %>
<option value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%} %>
but use JSTL taglibs instead of using scriptlets.
You can do it via JQuery, which IMHO more clean:
<select data-selected="${eqName}">
<%
for(int count=0;count<eqArray.size();count++){ %>
<option value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%}
%>
</select>
At the end of the page:
<script type="text/javascript">
$("select[data-selected]").each(function() {
var selected = $(this).data("selected");
$("select[data-selected='" + selected + "'] option[value='" + selected + "']").attr("selected", "selected");
})
</script>
By this way, you just need include the js at each of your page.
BTW, I recommend you to use JSTL and EL, which is more readable:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<select data-selected="${eqName}">
<c:forEach items="${eqArray}" var="model">
<option value="${model}">${model}</option>
</c:forEach>
</select>
You can implement these in two ways
By using jsp
" selected="selected" >
By Using Javascript at the end of the select box code
document.getElementById('selectBoxID').value=""
instead of selectBoxID id use the select box id
I have the following code:
<div>
<%
TaxonomicTypeFeed ttf = new TaxonomicTypeFeed();
ArrayList<String> tmp = ttf.getTypes();
System.out.println("Going to print");
for (int i=0; i < tmp.size(); i++)
{
System.out.println(tmp.get(i));
}
%>
<form>
<select>
<%
Iterator<String> i = tmp.iterator();
while (i.hasNext())
{
String str = i.next(); %>
<option value="<%str.toString();%>"><%str.toString();%>
</option>
<%}%>
</select>
</form>
</div>
It creates a dropdown list fine, however there is no text.
This is the first time I have ever used any of these options before, so I have no idea if I am even going about it in the right manner.
You need to print the values by <%= %>. The <% %> won't print anything.
<option value="<%=str%>"><%=str%></option>
Unrelated to the problem: this is not the best practice. Consider using taglibs/EL. You end up with better readable/maintainable code.
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="taxonomicTypeFeed" class="com.example.TaxonomicTypeFeed" />
...
<select>
<c:forEach items="${taxonomicTypeFeed.types}" var="type">
<option value="${type}">${type}</option>
</c:forEach>
</select>
Instead of <jsp:useBean> you can also use a preprocessing servlet.
How can I use session for facelets ?
What's the syntax...?
I would put a code like this
<% String loginSession = (String)session.getAttribute("login"); %>
<% if(loginSession != null){ %>
Welcome <%= session.getAttribute("firstName") %> !
<% }else{ %>
Guest
<% } %>
Thanks
#{sessionScope.login}
You can't have if-s in JSF (you can with JSTL, but it has complications). Instead you can choose to render or not a component:
<h:outputText value="Guest" rendered="#{sessionScope.login != null}" />