Accessing a javabean from a jsp page - java

I've a session bean with some string properties.
How could I access it and its properties from a jsp page, checking if one of them is equal or not to a given string?
I think I've to use <c:choose> and <bean:write> tag, but how? An example?

<c:if test="${beanName.beanProp == 'the string'}">
It's equal.
</c:if>

Related

In JSP Spring Framework, is it possible to create a c:if statement logically based on a user role and another condition?

For example, I want a field to show up if it's past 12 PM and the user role is teacher.
EDIT: I'm asking if there is a way to get the role of the user in JSP. What's the proper way to retrieve that?
Can I write it like this:
<c:if test ="${(Bean.after12PM) and (hasAnyRole('teacher'))}">
//some code
</c:if>
Is it the syntax for the jstl if or the value which is passed from a class or a object , not sure what your looking for
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
hope this helps
<c:set property="currentTime" value="${System.CurrentTimeMillis()}"/>
<c:set property="midnight" value="SET YOUR CUSTOM MIDNIGHT"/>
<c:set property="role" value="SET YOUR CUSTOM ROLE"/>
<c:if test="${currentTime > midnight && role=="teacher"}">
.....
</c:if>
Should be something like this, check for the correct "and" sintaxe inside that "test" it is been a while I can't recall it exactly.
Remember you can set any of those variables "midnight, role, currentTime" on your controller and set it on session or send it through request

JSP: Test if a variable from pageContext.findAttribute("") exists

I set this context.setAttribute("isGood", isGood) in a servlet to be able to get the value on a JSP file with pageContext.findAttribute("isGood").
Sometimes context.setAttribute("isGood", isGood) could not be set so it doesn't exist and my JSP gives me an error because it can't get it.
How can I test if pageContext.findAttribute("isGood") exists in my JSP?
Thanks.
You can use conditional check tag as:
<c:if test="${pageContext.getAttribute("isGood") != null }">
</c:if>

Why doesn't String.equals() method work inside the JSTL Core tag <c:if>?

Purpose of code: To validate an input string from the user. If the user inputs his name, stored as 'n', as "James" then the message "Validated!" is displayed.
(A separate HTML form takes care of the input string)
Although there aren't any errors, the test inside the tag fails and the message is not displayed regardless of whether the input string is "James" or not.
<body>
<% String n = (String)request.getParameter("n");
String t = "James";
%>
Message <!-- Default message displayed to show that HTML body is read. -->
<c:if test="${t.equals(n)}">
<c:out value="Validated!"/>
</c:if>
</body>
If I were to replace the test condition with true inside the curly braces, the if condition passes and the message "Validated!" is displayed.
Why doesn't the equals() method work inside the JSTL tag?
You haven't saved your variables to the scope.
You've to do this otherwise EL won't see your variables.
Save the variables to request scope:
<c:set var="n" value="${param.n}" scope="request"/>
<c:set var="t" value="James" scope="request"/>
You need to use EL's eq operator instead of Java's .equals().
Change your code like this:
<c:if test="${t eq n}">
<c:out value="Validated!"/>
</c:if>
P.S. Your JSP file contains scriptlets that is bad practice and the approach is insecure.
Would be even better to separate the logics and the views as described here
You can use normal == comparison operator in this way:
<c:if test="${t == n}">
<c:out value="Validated!"/>
</c:if>
If you need to compare string values rather than object's attribute, you can do this:
<c:if test="${t == 'Any string can be here'}">
<c:out value="Validated!"/>
</c:if>

How to test jsp attribute type

I have Spring MVC app with user settings. Settings are objects with strings Name, Value, User and Type field.
To edit values form with table is generated and Values are editable strings.
For Settings that have value checkbox in field Type, I would like display checkbox, ie:
<c:if test="setting.type=='checkbox'">
<form:checkbox path="setting.name" />
</c:if>
I cannot figure it out. Any suggestions?
did you try to change
<c:if test="setting.type=='checkbox'">
to
<c:if test="${setting.type=='checkbox'}">
?
of course, the 'setting' object must be passed to jsp correctly.
Did you try ? :
<c:if test="${setting.type=='checkbox'}">
<form:checkbox path="setting.name" />
</c:if>

How to compare 2 variables with <c:if>

I am using jsp + spring mvc, and on jsp page I have some list - catList with idand name and also have some variable test. I am trying to compare cat.id and test, but can't because every time have runtime syntax errors:
<c:forEach var="cat" items="${catList}" varStatus="i">
<c:out value="${cat.id}"/>
<%-- comparison and some action--%>
</c:forEach>
tried:
<c:if test="${category.id == test}" >
<c:if test="${category.id eq test}" >
<c:if test="${category.id eq ${test}}">
Update: I resolved this problem, simply it was error with server redeploy
All JSTL tags (as well as XML and HTML tags) require an opening tag and a closing tag. The opening tag defines where the tag body starts (and also allows you to define attributes). The closing tag defines where the tag body ends.
In the following code sample, the closing tag is on the last line.
<c:if test="${category.id == test}" >
<b>Test passed!</b>
</c:if>
As you can see, it has the same name as the opening tag, and starts with a / (and has no attributes).
The first two opening tags that you included in your question should work.

Categories