I'm trying to separate my Java Code from my JSP files and I'm having a bit of a problem. I'm trying to find out whether the user is a guest or not and then printing the appropriate action EG login form or their Username.
Heres my index.jsp file:
<% if(view.guest) { %>
<%= "Scriptlet: Login Form Here" %>
<% } else { %>
<%= "Scriptlet: User Name Here" %>
<% } %>
<br/><br/>
<c:choose>
<c:when test="${view.guest eq true}">
JSTL Tag: Login Form Here
</c:when>
<c:otherwise>
JSTL Tag: User Name Here
</c:otherwise>
</c:choose>
This produces the following output:
Scriptlet: Login Form
JSTL Tag: User Name
As you can see the Scriptlet produces the expected results, but the JSTL tags produce the opposite. Infact if I reverse the JSTL's conditional to false (for debugging purposes) it still produces the same result "JSTL Tag: User Name"
Btw view.guest is a public boolean variable of the object view.
The JSP EL doesn't access local variables. It accesses attributes of the page, request, session or application scope. And it also assumes you're not using public fields (which should never be used), but respect the Java Bean conventions.
And, just as in Java, comparing a boolean with true is unnecessary. You just need
<c:when test="${view.guest}">
And the condition will evaluate to true if there is a page, request, session or application attribute named "view", having a public getGuest() or isGuest() method returning true.
If you didn't use scriptlets at all, you would never have local variables in your pages, and you would never have this problem.
Related
I have two portlets :
1. Blog Portlet.
2. Author Portlet.
I used the concept of Public render parameter to send data (say key "urlTitle") from Blog portlet to Author portlet
But after sending "urlTitle" from Blog portlet how can I remove the data from Public render parameter
In Blog Portlet
EX code: view.jsp
<portlet:renderURL var="viewEntryURL">
<portlet:param name="struts_action" value="/blogs/view_entry" />
<portlet:param name="redirect" value="<%= currentURL %>" />
<portlet:param name="urlTitle" value="<%= entry.getUrlTitle() %>" />
</portlet:renderURL>
Send Data
Now how I can remove "urlTitle" form the public render parameter after data is sent.
Please give feedback.
-Thanks in advance
You could think about the following:
The LiferayPortletURL (the class that models portlet render, action and resource URLs tags) offers a method called setCopyCurrentRenderParameters
https://docs.liferay.com/portal/6.2/javadocs/com/liferay/portal/kernel/portlet/LiferayPortletURL.html#setCopyCurrentRenderParameters(boolean)
which when set to false, avoids copying render parameters, and the URLs are "cleaned" from those.
The caveat with this is that you would need to create a LiferayPortletURL in the back end doing the following:
LiferayPortletURL renderUrl = PortletURLFactoryUtil.create(
httpServletRequest,
themeDisplay.getPortletDisplay().getId(),
themeDisplay.getPlid(),
PortletRequest.RENDER_PHASE);
renderUrl.setCopyCurrentRenderParameters(false);
and after that pass it to your JSP set as an attribute (maybe renderRequest.setAttribute("renderUrl",renderUrl)?). I haven't done this for render URLs, but for resource URLs and it works!
You need to set
javax.portlet.init-param.copy-request-parameters=false
in your portlet class.
Broadly: I want the test in a JSTL Core <c:when> tag to return false if either:
- A variable can't be parsed as a number; or
- Comparison of the same variable to a number-literal is false.
I know that the variable won't be parsable as a number in some cases; this should not cause an error.
Details of the use-case follow...
I have the following in a JSP file on a WebSphere Portal v7 server. This JSP is rendered by a Web Content portlet configured to use an IBM Web Content Manager JSP component.
<%# page session="false" buffer="none" %>
<%# page trimDirectiveWhitespaces="true" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v7.0/json" prefix="json" %>
<%# taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v7.0/portal-fmt" prefix="portal-fmt" %>
<%# taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v7.0/portal-core" prefix="portal-core" %>
<%# taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v7.0/portal-navigation" prefix="portal-navigation" %>
<%# page import="com.isw.portal.theme.SideNav" %> <%!
SideNav iswSideNav=SideNav.getInstance();
%>
<portal-navigation:navigation startLevel="${navTabsLevel}" stopLevel="${navTabsLevel+3}">
<%=iswSideNav.getNavHTML(wpsNavModel,wpsSelectionModel,request,response) %>
</portal-navigation:navigation>
This works consistently on normal page views.
However, when the Portal Search collection is being updated (which occurs automatically once every 6 hours and takes about 2 minutes), this JSP produces several exceptions every second.
The exceptions are always the same two repeated as below. The second exception always includes a stack trace, which I've omitted except for the line stating the NumberFormatException message.
NavigationTag E com.ibm.wps.engine.tags.NavigationTag setStartLevel EJPEJ0027E: StartLevel less than 1 is ignored.
NavigationTag E com.ibm.wps.engine.tags.NavigationTag setStartLevel EJPEJ0026E: StartLevel is not a valid number.
java.lang.NumberFormatException: For input string: ""
As these exceptions don't seem to cause any functional problems, I want to wrap the <portal-navigation:navigation> element inside a <c:choose> element, such that the navigation is rendered when navTabsLevel is parsable as a number and that number is >= 1, but otherwise show a 1-line warning.
How do I do this without causing a "string cannot be parsed to number" error?
You can use <c:catch> for that.
<c:catch var="exception">
<portal-navigation:navigation startLevel="${navTabsLevel}" ... />
</c:catch>
<c:if test="${not empty exception}">
Handle fail.
</c:if>
Alternatively, and better, create a custom EL function like matches(), isNumber(), etc.
<c:choose>
<c:when test="${my:isNumber(navTabsLevel)}">
<portal-navigation:navigation startLevel="${navTabsLevel}" ... />
</c:when>
<c:otherwise>
Handle fail.
</c:otherwise>
</c:choose>
There's at least nothing available in standard JSTL functions set for that.
I'm currently struggling with the following problem. In my Spring web application I have different content types (e.g. text, images or code). Depending on the content type I need to display it in different ways:
text: <p>some text</p>
image: <img src="path/to/my.img" />
code <pre>some code</pre>
The HTML tags should be concatened to the actual content. The problem is, if I simply build the output text in my Java class, the HTML tags won't be resolved in my view, so that <p>some text</p> will be displayed.
Is it somehow possible that the HTML tags can be resolved in my view?
If it's just the escaping part that is the problem, use:
<c:out value="${model.snippets.html12}" escapeXml="false" />
(I am assuming your HTML string is in the model.snippets.html12).
Of course, the whole idea is bad. I am not affiliated with the MVC Police, but what is the point of using a MVC framework if you feel that it's a good idea to generate HTML inside your controller and pass it, as a string - into a view? From my point of view it's a bit of a schizophrenia.
You can save a lot of sanity by just rendering the whole thing in a switch, inside the template. I mean like:
<c:choose>
<c:when test="${thing.type == 'CODE'}">
<div> some code: ${thing.content} </div>
</c:when>
<c:when test="${thing.type == 'IMAGE'}">
<img src="${thing.src}" alt="${thing.whatever}" />
</c:when>
<!-- some other choices -->
</c:choose>
Even better, create a simple tag file that will let you reuse the logic anywhere you need it.
Or ditch MVC - be honest.
If you do have jquery, set the content type in your model. Set it to the HTML.
<input type = hidden id = contentType value = "${yourmodel.contentType}"
Add span to your elements
<span id = "textspan" style = "display:none"><p>some text</p></span>
<span id = "imgspan><img src="path/to/my.img" /></span>
<span id = "codespan><pre>some code</pre></span>
write a jquery
if($("contentType").val() == text){
$("#textspan").show();
}else if($("contentType").val() == img){
$("#imgspan").show();
}else{
$("#codespan").show();
}
In my xhtml page am setting values in some myValue variable. Code is as below,
<body>
<c:set var="myValue" value="someValue" scope="request"></c:set>`
</body>
Am hitting a login.jsp (Login page) from this xhtml page and trying to print the value on my login.jsp as below,
myValue ID retrieved from request <c:out value="${param.myValue}" />
This is not printing someValue. I also checked by putting it in session scope like below,
<c:set var="myValue" value="someValue" scope="session"></c:set>
Even this is not working
And in login.jsp,
myValue ID retrieved from session <c:out value="${sessionScope.myValue}" />
You should have just ${myValue}, as param.myValue means to find value from URL params.
The task is to retrieve parameters from session via JSTL. The session parameter name is "programId" .
I tried:
<c:if test="${sessionScope.programId != null}" >
please execute
</c:if>
Then I tried:
<c:if test="${sessionScope:programId != null}" > please execute </c:if>
Here I get: The function applicationScope:programId is undefined
On top I have:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Oracle has in examples:
http://docs.oracle.com/javaee/1.4/tutorial/doc/JSTL5.html
<c:if test="${applicationScope:booklist == null}" >
<c:import url="${initParam.booksURL}" var="xml" />
<x:parse doc="${xml}" var="booklist" scope="application" />
</c:if>
where applicationScope can be swapped by sessionScope.
again "trivialism" complexity drives me nuts. Why corp. examples never work?
Thank You Guys,
You're reading the wrong tutorial page. The <c:xxx> tags does not belong to the JSTL XML taglib which supports XPath syntax. Instead, it belongs to the JSTL Core taglib for which the proper tutorial page is here.
You need to use the normal ${bean.property} notation instead.
<c:if test="${applicationScope.booklist == null}">
<c:import url="${initParam.booksURL}" var="xml" />
<x:parse doc="${xml}" var="booklist" scope="application" />
</c:if>
In normal EL (not XPath!) syntax, the : identifies the start of an EL function. See also the JSTL Functions taglib for several EL function examples for which the tutorial page is here.
See also:
Our JSTL tag wiki page
Our EL tag wiki page
I don't find in your code you have set your variable programID somewhere in your SessionScope using EL something like the one shown below.
<c:set var="programID" value="SomeValue" scope="session"/>
If you indeed didn't set that variable, try setting it first like the above then try the following.
<c:if test="${sessionScope:programId != null}" > please execute </c:if>
It should work.
if you have set session in your controller
session.setAttribute("programID",someValue);
then you should try this
${sessionScope.programID} in your jsp
which is part of jstl core library
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
and if you are getting exception
"${sessionScope:programId != null}" contains invalid expression(s): javax.el.ELException: Failed to parse the expression [${sessionScope:programId != null}]
include jasper.jar in your lib folder
java brushup for basic concepts of java