I have following script in my page ; cout tag is not returning any value...
<c:set var="simple.Var" value="simple.Var"></c:set>
<c:out value="${simple.Var}" ></c:out>
Please let me know where the problem is ?
The problem is the ยท in the variable name. Change the variable name to simpleVar as follows:
<c:set var="simpleVar" value="simple.Var"/>
<c:out value="${simpleVar}"/>
Edit... shved90 makes a good point. If you have a bean in the request context named simple and that bean has a method getVar(), then all you need in the JSP is:
<c:out value="${simple.var}"/>
1-This is not a good variable name
Do not use "." in variable name.
2-if you use
<c:set var="simpleVar" value="simple.Var"/>
Without Using c:out, you can show simpleVar's value.
3-if you have a bean named simple and it has a property var( has a getVar() method) you can directly use simple.var without using any tag
Related
I have a scenario in which i am rendering a jsp page .One of my value is in my model Form entity.I am assigning it in one of my form input like below :
<s:hidden name="myModelName.myUserName"/>
I am trying to assign it to my java variable inside my jsp.I tried accessing the model directly like this
<%= String myName = myModelName.myUserName %>
But i get error message "myModelName cannot be resolved.I have then tried accessing from the hidden field.But i dont know how to use it.Anyway i need the value in java variable inside jsp for some reason.Anyone help me out how to do it
You can do something like this:
assign your model property value to variable userName
<c:set var="userName" scope="session" value="${myModelName.myUserName}"/>
and then you can output it using this tag:
<c:out value = "${userName}"/>
You can read more about JSTL tags here: Using JSTL tags
Hope this helps. Ask if you need anything else.
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.
On my JSP page I import a java class as
<%#page import="org.model.MyConstants"%>
In the MyConstants class I have a public function
public static String getBookTypeName(int bookTypeId){
//lots of if's based on id
String bookName = 'Fantasy';
return bookName;
}
Now from my JSP page I am trying to call this function passing in a request bean value as the parameter.
Essentially one property of my bean is an integer which I used to display, now I want to display the String that goes along with it but want to do so in a way where I dont need to modify the bean and can just use my MyConstants method to get the string.
I have been outputting my bean value before using
<bean:write name="BookNotifyForm" property="bookTypeId" />
I have been able to use scriplets in the past but have never had to pass a bean value into it.
<c:out value="<%=MyConstants.SOME_STRING_OUTPUT %>" />
I think I have the idea down but the syntax is what is killing me. I believe I should be able to use c:set to store the bean variable, and then use c:out calling the method passing in the variable I stored. Something along the lines of....
<c:set var="bookTypeId" value="${BookNotifyForm.bookTypeId}" />
//help with syntax, trying
<c:out value="<%=MyConstants.getBookTypeName(bookTypeId) %>" />
<c:out value="<%=MyConstants.getBookTypeName(${bookTypeId}) %>" />
It must be possible to do something like this !?
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.
What is the most beautiful way to copy the value from a hardcoded JSP variable into an EL variable?
This can be done with the following JSP
<% request.setAttribute("mode", mode); %>
but is it possible to do the same in EL? Maybe useBean is applicable somehow here?
There is not a setProperty method for request. Do you mean setAttribute? I think this is want you want: <c:set var="mode"><%= mode %></c:set> Note: use "var" attribute not "name".