Java Facelets and session? - java

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}" />

Related

Identifier expected after this token

I got a error : Identifier expected after this token
Syntax error on token "tag", Identifier expected after this token
And here is my code:
<%# page import="photoshare.Picture" %>
<%# page import="photoshare.PictureDao" %>
<%# page import="photoshare.HREF_AND_IMGSRC" %>
<%# page import="photoshare.ToolsDao" %>
<%# page import="org.apache.commons.fileupload.FileUploadException" %>
<%# page import="java.util.ArrayList" %>
<%# page import="java.util.List" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Get POP Tags</title></head>
<body>
<h1>With Photos with popular Tags:</h1>
<%
String tagName = request.getParameter("tag");
List<String> popTags = new ArrayList<String>();
ToolsDao toolsDao = new ToolsDao();
popTags = toolsDao.getPopularTags();
for (tag:popTags) {%>
<a href="popTags.jsp?tag=<%= tagName %>">tagName <a>
<%}
List<HREF_AND_IMGSRC> tagPhotos = toolsDao.getHrefAndImgSrcFromTag(tagName);
for (HREF_AND_IMGSRC photo:tagPhotos) {
int pictureId = photo.getPictureID();
int selectedAlbumID = photo.getAlbumID();
String selectedAlbum_id = Integer.toString(selectedAlbumID);
%>
<td><a href="picture.jsp?id=<%= pictureId %>&album_id=<%= selectedAlbum_id%>">
<img src="/photoshare/img?t=1&picture_id=<%= pictureId %>"/></a></td>
<%
}
%>
Click here to Main Page<br>
</body>
</html>
I am finding some reasons for this but nothing similar, can anybody help me to figure out what is the problem for this specified error?
I found the problem which is :
for (tag:popTags) {%>
<a href="popTags.jsp?tag=<%= tagName %>">tagName <a>
<%}
I have to declare the type of the tag in popTags list such as:
for (String tag:popTags) {%>
<a href="popTags.jsp?tag=<%= tagName %>">tagName <a>
<%}

jsp if variable starts with

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><%
}

How to include HTML in JSP?

I've searched for this question, there are some answers, but not exactly as my question. So, here is my jsp code:
<body>
<%
if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){
out.print("<h1> Welcome ");
out.print(request.getParameter("username") + "</h1>");
} else {
out.print("<h1> Please Try Again </h1> <br />");
out.print("Either your username or password is incorrect. Please Try again <br /> <br />");
}
%>
<%# include file="LoginForm.html" %>
but instead of this, I want to include "loginForm.html" only in "else" block.
How can I do it?
Of course this doesn't work, but you'll guess what I want:
<%
if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){
out.print("<h1> Welcome ");
out.print(request.getParameter("username") + "</h1>");
} else {
out.print("<h1> Please Try Again </h1> <br />");
out.print("Either your username or password is incorrect. Please Try again <br /> <br />");
include file="LoginForm.html" ;
}
%>
I would like to show you a way, try like below. You can try with else instead with if.
<body>
<%
int x = 10;
if(x>10) {
%>
<%#include file="some.html" %>
<%
}
%>
</body>
You could also include reusable html in a jsp file function and call it for printing where needed. e.g.
config.jsp contains
<%!
public void printMenu()
{
%>
HTML HERE
<%!
}
%>
home.jsp contains
<%#include file="config.jsp"%>
<!DOCTYPE html>
<html>
<body>
<% printMenu(); %>
<div id="PeopleTableContainer" style="width: 800px;"></div>

How to set default value for drop-down/select in JSP?

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

jsp error when using if-else with html

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>

Categories