jstl, how to fill current request with map - jstl

I have a map which contain request parameters and their value, which will be used later on the jsp page. I'm usinng jsp page inclusion later and I don't know what exact params would I use
How I suppose, the filling should be implemented:
<c:forEach var="theParameter" items="${ parametersMap }" >
<c:set var="${theParameter.key}" value="${theParameter.value}" />
</c:forEach>
But I'm getting the error that the 'var' attribute can't use expression
Do you have any ideas about workaround?
EDIT:
Example:
parametersMap = [ 'param1' : 'value1' ; 'param2' : 'value2']
as result I would like something like this:
<c:set var="param1" value="value1" />
<c:set var="param2" value="value2" />

You need to do like this
<c:set target="${RequestScope[aNewMap]}"
property="${theParameter.key}" value="${theParameter.value}"/>

I solved my problem by adding custom tag, which on java part retrieves the Hashmap object and fill the request (which can be retieved from pageContext)

Related

How do I write an if statement in JSP-JSTL if I need to check whether the user list contains a specific user?

'theGroup' and 'groupCreator' are a model(entity) attributes coming from the controller. 'users' is the array list which is the attribute of theGroup (theGroup.getUsers() basically).
I need to check if the groupCreator exists in theGroup.users list in JSP
I've tried the code below but it didn't work
<c:if test = "${theGroup.users.contains(groupCreator)}">
</c:if>
You can use forEach to iterate through values and then compare values in it with groupCreator. Your code will somewhat look like below :
<c:forEach var="values" items="${theGroup.users}">
<c:if test="${values == groupCreator}">
<!--setting true if value match-->
<c:set var="Matched" value="true" scope="request" />
</c:if>
</c:forEach
<!--Print-->
${Matched}

how do i create an arraylist inside jsp using jstl

I want to collect values from a JSP page and pass it dynamically to another JSP page with the help of JSTL. How can I do this?
You use a request scoped HashMap for that.
Example
1) Declare the HashMap in each JSP you want to insert or access the list of values.
<jsp:useBean id="map" class="java.util.HashMap" scope="request"/>
Note: The scope="request" is what makes it accessible in other JSPs.
2) Stuff information into the HashMap
<c:set target="${requestScope.map}" property="city" value="${param.city}"/>
<c:set target="${requestScope.map}" property="state" value="${param.state}"/>
<c:set target="${requestScope.map}" property="phone" value="${param.phone}"/>
3a) You can now pull out the values in a different JSP by simply doing:
<c:out value="${requestScope.map['city']}"/>
-or-
3b) You can also iterate over that HashMap in a different JSP:
<c:forEach items="${requestScope.map}" var="item">
${item.key} = ${item.value}<br/>
</c:forEach>

How can i get a variable from request scope in jstl

I want to use below code in jstl.
String shouldFollow=(String)request.getAttribute("shouldFollow");
How can i do so in jstl
Firstly, it might do you good to research the difference between JSTL and the Expression Language.
Your line of code can be wrapped in an expression like so:
${ shouldFollow }
The PageContext object will resolve the expression by searching the attribute mapping of the pageContext, request, session, and servletContext scopes respectively given this syntax.
You can use expressions for the values of attributes of JSTL tags:
<c:if test="${ shouldFollow }">
...
</c:if>
Where the body of this tag will execute if shouldFollow is true.

spring:bind error using in a List

I have a list containing users. I am trying to print it in JSP but some how I am not able to get it to print it. Getting this exception HTTP Status 500 - javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'users[0]' available as request attribute
Code in JSP
<c:forEach items="${users}" var="user" varStatus="status">
<spring:bind path="users[${status.index}].name">
<c:out value="${status.value}" />
</spring:bind>
</c:forEach>
Controller
ModelAndView modelAndView = new ModelAndView("go_some_JSP_page");
List<UserEntity> users = userManager.getAllObjects();
modelAndView.addObject("users", users);
BTW, UserEntity has name field. If I remove the binding and try to print the user.name using <c:out value="user.name" /> it prints the value
Where am I going wrong and what do I need to do? Thanks
Not working code below. [I have to invoke formatting on field #NumberFormat so have to try it using status variable]
<spring:bind path="user.name">
<c:out value="${status.value}" />
</spring:bind>
Gets this error --> javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
So added a bean binding and then I get empty table :(. I believe thats because the instance is empty. So this does not seems like a right approach.
#ModelAttribute("user")
public UserEntity userEntityBinding() {
return UserEntity.newInstance();
}
A working code exists at https://github.com/hth/StatusInvoke.git
Let me know if you face any problem deploying it.
This question has been solved. Thanks for looking at it.
You can try using LazyList instead of simple list. If you want to take a look at the example then you can refer one of my question. In the question statement I have mentioned how to use the LazyList.
Hope that helps you. Cheers.
this, if the modelandview are returned, is the correct way to populate the list
ModelAndView modelAndView = new ModelAndView("go_some_JSP_page");
List<UserEntity> users = userManager.getAllObjects();
modelAndView.addObject("users", users);
And this is the correct way to reference the list
<c:forEach items="${users}" var="user" varStatus="status">
<spring:bind path="user.name">
<c:out value="${status.value}" />
</spring:bind>
</c:forEach>
Your problem must be elsewhere is the name field definitely populated, is the correct jsp being called... the above code is correct and should work.
The correct answer to invoke #NumberFormat annotation is by using spring:eval expression tag
<spring:eval expression="user.balance" />
This invokes the annotation and performs formatting as mentioned in the annotation
I don't think you can use spring:bind in that case, AFAIK it tries to get the variable from the ModelMap, it's not able to get it from the "for" var.

Not able to retrieve session/request scope values using JSTL

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.

Resources