How to access model attribute fields in JSP scriptlet? - java

In my controller I have code like this:
List<FeesReceiptIntegrationModel> FRIList = feesReceiptIntegrationService.listInstituteWiseCollectionSummary(model, request);
model.addAttribute("FRIList", FRIList);
I want to access this FRIList and its fields in Scriptlet of JSP page.
I tried something like this:
String fcash = request.getParameter(FRIList.cashamount);
but it does not work.
List myMap = (ArrayList) request.getAttribute("FRIList.cashamount");
I don't want to access this via JSTL tags but I would like to access this only in scriptlet.
Can anybody tell me how this can be achieved?

Using scriplets is a bad idea. Try to avoid using java codes inside JSP page.
You can use JSTL c:forEach for your purpose
Simple example
<c:forEach items="${FRIList}" begin="0" end="1" var="test">
${test.cashamount}
</c:forEach>

You cant print the values in the list as it is, you need to iterate them after you get the list from the model. As said,
I don't want to access this via JSTL tags but I would like to access this only in scriptlet
<% List<FeesReceiptIntegrationModel > myMap = (ArrayList<FeesReceiptIntegrationModel >) request.getAttribute("FRIList");
for(FeesReceiptIntegrationModel obj : myMap ){
obj.getcashamount(); // your getter method here
}
%>
but it is not advisable to use the scriptlets, please have a look at How to avoid Java code in JSP files?

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.

How to update an attribute dynamically set in JSP PageContext?

Please correct me if I am wrong. I am developing a small web application for learning purpose. I have a jsp in which a list of top scorers in the the game are to be displayed in a table. For that I wrote a ServletContextListener and in the contextInitialized() method I have set an attribute(LinkedHashSet) in the ContextScope, which is the list of top 10 scorers in the game. I think it can be accessed using EL. But how can I update this collection?
You can use request.getSession().getServletContext().getAttribute("your_attribute_name_here") and can access the LinkedHashSet, once you get it you can add/remove/update values in it and again set it back to put updated values like request.getSession().getServletContext().setAttribute("your_attribute_name_here", "update_LinkedHashSet"); As best of my knowledge using EL you can access it but can not put back the updated value in attribute.
Note: while accessing the attribute you will need the explicit type casting.
You can update attribute like :
<%((Set<String>)pageContext.getServletContext().getAttribute("set")).add("Second"); %>
<% Set<String> set = (Set<String>) pageContext.getServletContext().getAttribute("set"); %>
from servlet context
<c:forEach items="${set}" var="s">
<c:out value="${s}"/>
</c:forEach>

How to pass a java object to jsp page

I have a serveresource method which is invoked on clicking a link. The serveresource method takes the input from the parameter that was passed and retrieves a row from the database. Now all the values in a row are in set using the mutator methods. I have everything in a java object. I need to pass this object to a jsp page to print the values of a single row on the jsp page. I am not sure how to handle that java object in the jsp page instead of setting each value as an attribute in the serveresource method. Need help from experts.. Thanks in Advance
UPDATE
It was because I have an Ajax call and when I set values it is in a completely different life cycle which is causing the problem. I figured it out.
You should defining the java object as Bean in JSP.
The Bean in JSP can be defined using < jsp:useBean..> standard jsp tag. And set and get property using < jsp:setProperty..> and < jsp:getProperty..> standard jsp tags.
Refernces:
Use Bean
Using Beans in JSP. A brief introduction to JSP and Java Beans
UseBean syntax
< jsp:setProperty> and < jsp:getPropety> syntax
The usual method is to add it to the HttpServletRequest object, thus:
MyBean myBean = new MyBean();
myBean.setValue("something);
myBean.setAnotherValue("something else");
// ... stuff ...
request.setAttribute("myBean", MyBean);
This can be accessed from the jsp page using EL thus:
<table>
<tr>
<td>${myBean.value}</td>
<td>${myBean.anotherValue}</td>
</tr>
</table>
you can bind with request object
In Servlet or JSP
request.setAttribute("strIdentifire", yourJavaObject);
In JSP
YourJavaObjectClass obj = (YourJavaObjectClass)request.getAttribute("strIdentifire");

How to access query string using Struts 2 tags from a JSP?

I am redirecting to a JSP that has to print the whole incoming query string. Like in this other question, Request parameter in jsp page, I do not want to access one parameter but the whole query string which I would accomplish in a scriptlet like: <%= request.getQueryString() %>
Thanks!
You can get the paramater object by OGNL stack value #parameters
http://struts.apache.org/2.0.14/docs/ognl-basics.html
If you want to iterate it, you can do something like ( this example create hidden input for each param)
<s:iterator value="#parameters" var="param">
<s:hidden name="%{#param.key}" value="%{#param.value}" />
</s:iterator>
You can user s:iterator tag in struts2 and you can get your string value in Jsp by OGNL lang which is supported by Struts2 without writing code in Scriptlet.
Please check below links for your reference.
http://www.vaannila.com/struts-2/struts-2-example/struts-2-ognl-expression-language-example-1.html
http://www.vaannila.com/struts-2/struts-2-example/struts-2-iterator-tag-example1.html

Categories