I am trying to display the first name value for a user in a jsp page. All users have getters and setters for each of their attributes. Here's the code :
<c:out value="${sessionScope.user.getF_name()}" default="guest" />
However, I'm getting the error :
/index.jsp(77,4) The function getF_name must be used with a prefix when a default namespace is not specified
Any ideas?
just use
<c:out value="${sessionScope.user.f_name}" default="guest" />
you need to specify the bean property instead of method when using EL.
You don't need to call the getter, it would be called by the EL, so you just have to use
<c:out value="${sessionScope.user.f_name}" default="guest" />
Or even easier, just let the EL takes care about searching the user attribute in session scope for you.
<c:out value="${user.f_name}" default="guest" />
Note that for the latter to work you must have a user attribute in session scope only.
You can find more info about this on StackOverflow EL wiki.
Related
I'm trying to clean a list of objects that my JSP File receives, and I execute the following command:
<c:remove var="list" scope="session" />
<c:out value="${list}"></c:out>
But when I call the JSP page, the page shows the memory address of the list that should have been deleted and fill normally the HTML elements with the attributes of objects that are defined in the list.
EDIT
I'm using my own tag, can it influence? Object contains list.
<ec:form beanName="object" controllerUrl="/param1/param2">
And if I remove the object with:
<c:remove var="object">
Raises NullPointerException
References:
http://www.java2s.com/Tutorial/Java/0380__JSTL/RemoveVariable.htm
Here I am not sure in which scope your attribute lives.So i would suggest to
Try this
<c:remove var="list"/>
This above code removes an attribute from all the scopes (page, session, application, request). In order to be specific we must need to specify the scope attribute inside tag.The below JSTL statement will remove the variable list from session scope.
<c:remove var="list" scope="session"/>
Let's say I have a collections called imagesNames.
In my JSP file, I want to display the first element of this collections using Struts property tag.
This is what I have tried, but obviously this won't work.
<s:property value='imagesNames[0]' />
Obviously this won't work, but you can make it working if you populate the collection and provide the public accessors before you return a dispatcher result to the JSP page.
<s:property value="imagesNames.first" />
In PHP we can do the following with the help of Variable variables in PHP:
$privateVar = 'Hello!';
$publicVar = 'privateVar';
echo $$publicVar; // Hello!
Suppose we have the following chunk of Java code:
request.setAttribute("privateVar", "Hello!");
request.setAttribute("publicVar", "privateVar");
I've tried the following but an error occurs.
${${publicVar}}
Does anyone know how we can get value of privateVar via using only publicVar in JSP (JSTL)?
UPDATE 1:
I have a custom tag which allows to print a message if an object foo doesn't have a field bar.
I know I must catch exceptions in the case but I don't want to handle ones in JSP. I want to do it only in CustomTag file.
<%-- JSP file --%>
<ctf:tagName varName="foo.bar" />
<%-- CustomTag file --%>
<%# attribute name="varName" required="true" rtexprvalue="true"%>
<c:catch var="exception">
<c:set var="valX" value="${${varName}}" scope="page"/>
</c:catch>
<c:if test="${exception != null}">Can't find getter for the VAR in the OBJ.</c:if>
UPDATE 2:
JB Nizet gave me the answer and the following works well! :)
<c:set var="privateVar" value="Hello!" />
<c:set var="publicVar" value="privateVar" />
${pageScope[pageScope.publicVar]}
I don't think you can directly do this in the same way that you can in PHP. Instead you could change the attribute to use the value of the privateVar instead of the name, like this:
String privateVar = "Hello!";
request.setAttribute("privateVar", privateVar);
request.setAttribute("publicVar", privateVar);
This gives you access to the value under both names, which I think is the closest you'd get. No need to even put the attribute privateVar in the request if you are ultimately going to use publicVar on the JSP.
Ultimately you may want to rethink the design here as it doesn't really work in Java.
The basics:
That's not JSTL but Expression Language. And you should only use a single ${} evaluator. The code would be:
${publicVar}
More info:
StackOverflow Expression Language wiki
To your problem:
Expression Language doesn't allow that. You cannot have private attributes in any scope (page, request, session, application), so you can at most set the attribute twice with different names but the same value. But as you may note, this is useless.
I'm trying to pass a parameter with <portlet:actionURL> to a portlet in liferay, but it turns out that, using EL to pass values in not working, however, using JSP expression tag is working fine.
Here's my relevant code:
<%
ResultRow row = (ResultRow)request.getAttribute(WebKeys.SEARCH_CONTAINER_RESULT_ROW);
Course course = (Course) row.getObject();
long groupId = themeDisplay.getLayout().getGroupId();
String name = Course.class.getName();
String primaryKey = String.valueOf(course.getPrimaryKey());
%>
<liferay-ui:icon-menu>
<c:if test="<%= permissionChecker.hasPermission(groupId, name, primaryKey, ActionKeys.UPDATE)%>">
<portlet:actionURL name="editCourse" var="editURL">
<portlet:param name="resourcePrimaryKey" value="${primaryKey}"/>
</portlet:actionURL>
<liferay-ui:icon image="edit" message="Edit" url="${editURL}" />
</c:if>
</liferay-ui:icon-menu>
As you see, in <portlet:param> tag, I have used EL for passing value attribute. But it doesn't work, and I receive the value as 0 for "resourcePrimaryKey" in my action method, when I do:
long courseId = ParamUtil.getLong(request, "resourcePrimaryKey");
// courseId is 0 here
However, if I use JSP expression tag in place of EL, it works fine:
<portlet:actionURL name="editCourse" var="editURL">
<portlet:param name="resourcePrimaryKey" value="<%= primaryKey %>"/>
</portlet:actionURL>
Now, I get the required value for "resourcePrimaryKey".
Can anyone figure what's going on here? Surprisingly, EL at other place work fine, as you see - ${editURL} value for url attribute, is working fine, and redirecting to the corresponding url.
I came across this thread on apache mail archive regarding the same issue, but that doesn't really solve the problem.
A variable in the scriptlet cannot be used directly in an EL, you would first need to set it like:
<c:set var="primKey"><%=primaryKey %></c:set>
and use ${primKey} or set it as a request attribute:
request.setAttribute("primKey", primaryKey);
Clearly, better would be to directly use the expression.
Also regarding the ${editURL} working, it is a portlet jsp tag which sets the variable in the page context so that it is available to the EL.
Our el wiki is a good place to know these things, look out for the heading Make objects available to EL for this question :-)
Is it possible to access fields defined in Beans/Forms used within an action from a JSP page?
At the moment I can use this:
<s:action name="actionName" var="foo" executeResult="false" />
and access any fields defined in that action class using
<s:property value="#foo.bar" />
but this doesn't seem to work for values defined in a bean or form, which I would normally be able to access using the property tag in the result JSP page for an action.
Just tested this with Struts 2 version 2.3.4.1 and it worked for me to get the displayName value of the kuPerson bean. Note in my Struts action class I have a getKuPerson method that returns an object of type KuPerson and that KuPerson class has a getDisplayName method that returns a String.
<s:action name="person" var="personAction" executeResult="false" />
Display name: <s:property value="#personAction.kuPerson.displayName" />