creating immutable objects in JSPs - java

I know you can use the <jsp:useBean> tag to instantiate objects within JSPs without resorting to scriptlet code. However I'd like to instantiate an Integer who value is the result of an EL expression, something like:
<jsp:useBean id="total" class="java.lang.Integer">
<jsp:setProperty name="amount" value="${param1 + param2}"/>
</jsp:useBean>
Of course this won't work because Integer objects don't have a property named 'amount', the only way their value can be set is via a constructor parameter (i.e. Integer objects are immutable). Is there any way to instantiate such an object and set it's value without using scriptlet code?
Thanks,
Don

<c:set var="amount" value="${param1 + param2}" scope="page" />

Primitive wrappers also have no default constructor so you can't even initialize one that way.
I'm not sure that EL is supposed to be used in that way. It is more of a template language. It isn't clear what advantage what you are trying to do has over something like:
<%
Integer total = new Integer(param1 + param2);
%>
And then just using <%= total %> where you need the value to be output. You could also do:
<%
pageContext.setAttribute("total", new Integer(param1 + param2));
%>
if you want the value to be in the page scope like useBean will do.

If you have a bean, can you just update the bean with param1 and 2? Create a method, setAmount(param1, param2), and set it before you use getAmount(), which is what the bean is going to call.

Related

Assigning form input value to java variable in jsp

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.

PHP's variable variables analog in JSTL

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.

jsp pass bean parameter into scriplet function

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 !?

JSTL cout is not working

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

How to copy value from hardcoded variable to setPorperty one in JSTL/EL?

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".

Categories