how to pass parameters while calling a function through JSTL - java

How can i pass parameters to a function while calling it through JSTL.
<c:forEach items="${sessionScope.student_var.details(x)}" var="display_details"> </c:forEach>
I am trying to pass parameter x to a function but it is not working

If the variable is defined as follows in the JSP:
<% String x = "age"; %>
You have two options to get the expression to evaluate using x as an argument.
1. Leave the definition as is and set a PageContext attribute
<% pageContext.setAttribute("x", "age"); %>
An expression evaluates variables by looking for attributes by key name. So when your expression is evaluated, the engine is searching for an attribute with the key name 'x' in the pageContext, request, session, and servletContext scopes.
2. Move the definition of the variable to the backend
request.setAttribute("x", "age");
This statement would be found in your servlet/controller, setting the attribute before we reach the JSP.

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.

How is attribute a String value from a message like on here?

I am new in jsp domain and I don't know how to attribute a String value from a message because it give me error
<%String mesg=%>
<fmt:message key="msg_valid" bundle="${message}" />
<%;%>
The error is this:
Syntax error on token "=", ; expected
As nicely stated in one of the post by Aniket Kulkarni:
JSTL variables are actually attributes, and by default are scoped at
the page context level. As a result, if you need to access a JSTL
variable value in a scriptlet, you can do so by calling the
getAttribute() method on the appropriately scoped object (usually
pageContext and request)
You can try this:
<%
String mesg= (String)pageContext.getAttribute("message");
out.println(resp);
%>

Passing value from one jsp to another

First jsp page contains code:
<a href='select.jsp?param1=${person.name}'>link to other jsp</a>
In html this link refers to:
http://sitename/select.jsp?param1=gsdf
A code from select.jsp page:
<c:out value="${param1}">No name</c:out>
<br/><%=request.getParameter("param1")%>
But I get:
No name
gsdf
Why the value of param1 did not pass to second jsp in the case of using c:out?
you need to use EL (JSP Expression Language).
from javaDoc :
param: Maps a request parameter name to a single value
so you juste need to do something like
<c:out value="${param.param1}"/>
You can send Using Session object.
session.setAttribute("prsonName", prsonName);
These values will now be available from any jsp as long as your session is still active.
Object userid = session.getAttribute("prsonName");

How to access arraylist in object stored in session variable [duplicate]

If I have a
ArrayList<Person> persons
How do I access it in EL?
<c:foreach items="${what goes here??}" var="person">${person.title}</c:foreach>
The expression ${foo} uses behind the scenes JspContext#findAttribute() which searches for attributes in PageContext, HttpServletRequest, HttpSession and ServletContext in this order by their getAttribute("foo") method whereby foo from ${foo} thus represents the attribute name "foo" and returns the first non-null object.
So, if you do in a servlet
ArrayList<Person> persons = getItSomehow();
request.setAttribute("persons", persons); // It's now available by ${persons}
request.getRequestDispatcher("/WEB-INF/persons.jsp").forward(request, response);
And call this servlet by URL, then you'll be able to iterate over it in page.jsp as follows:
<c:foreach items="${persons}" var="person">
${person.title}
<c:forEach>
The above is also equally valid when you put it in the session scope instead
request.getSession().setAttribute("persons", persons);
or even in the application scope
getServletContext().setAttribute("persons", persons);
EL will for title in ${person.title} implicitly look for a public instance (not static!) method prefixed with get in Person class like below:
public String getTitle() {
return title;
}
The field title does not necessarily need to exist in the class (so you can even return a hardcoded string and keep using ${person.title}), and it does not necessarily need to be an instance field (so it can also be a static field, as long as the getter method itself isn't static).
Only boolean (not Boolean!) getters have a special treatment; EL will implicitly look for a public method prefixed with is. E.g. for a ${person.awesome}:
public boolean isAwesome() {
return awesome;
}
See also:
Our EL wiki page
How do servlets work? Instantiation, sessions, shared variables and multithreading
How to avoid Java code in JSP files?
Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
Use EL ${XY} directly in scriptlet <% XY %>
How does Java expression language resolve boolean attributes? (in JSF 1.2)
<c:forEach var="item" items="${names}"> ${item.title} </c:forEach>
names should be in the set as attribute available for the view
If you are using Servlets or action class for creating your list and then forwarding it to your JSP, then you must be having following line in your servlet or action class.
ArrayList<Person> names = "get from somewhere";
request.setAttribute("personNames",names);
<c:foreach var="item" items="${personNames}"> ${item.title} </c:foreach>

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