JSTL c:if tag condition - java

<c:if test="${config.update and not config.caseUpdate}">
<html:submit property="userComments" style="width:200px" styleId="addCommentsBtn">
<bean:message key="button.update.case"/>
</html:submit>
</c:if>
I am confusing about this working mechanism . Can you please explain on if condition.

this will execute
<html:submit property="userComments" style="width:200px" styleId="addCommentsBtn">
<bean:message key="button.update.case.with.comments"/>
</html:submit>
only when config.update will true and config.caseUpdate will false
example
<c:if test='condition'>
Generate some template text
</c:if>
if condition is true it will display template text.
if you want to display gender as male if condition is gender male then you can print male in c:if
for more information go through http://www.exampledepot.com/egs/javax.servlet.jsp.jstl.core/if.html

Here you can read about jstl scopes and statements.

This is like saying
if(config.update && !config.caseUpdate){
show a submit button
}
JSTL also works by checking if those variables are null. So if one is null it wont do anything.
Example, you can do something like
${config.update}
to print to the web browser the object config.update. If config.update is null or basically doesn't exist yet in the session, then it simply skips over it so you get no errors.

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

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>

Using if in JSP

I am trying to use an if condition to display an item or not in one of my JSP files.
I currently have
<c:if test="${fn:length(${model.listOfListProducts}) gt 0}">
<p> we got in </p>
</c:if>
I am trying to see if the length of one of my Lists< List<>> in my model object is essentially greater than 0 (not null)
But this does not display the paragraph that I have placed inside. Is my test statement syntactically wrong?
remove $ from nested expression
<c:if test="${fn:length(model.listOfListProducts) gt 0}">
<p> we got in </p>
</c:if>
See Also
if...else within JSP or JSTL

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>

Setting jsp checkbox with a value from database

Ok. I'm making a java web app with a database backend to do some CRUD on some data. When the edit button is clicked next to an item, it navigates to a form with the current data for editing. One of the fields is boolean and I would like to display it as a checkbox so that True makes it checked and False leaves it unchecked.
I have tried many different variations none seem to work. Here are some examples where <%= action.get("stable")%> returns a string with either True or False
<input TYPE=checkbox name="stable" value=<%= action.get("stable") %>
<input TYPE=checkbox name="stable" value=<%= action.get("stable")?"True":"False" %><%= action.get("stable")?"checked":"" %>
<input TYPE=checkbox name="stable" checked=<%= action.get("stable")%>/>
So how do you set a check box to checked/unchecked depending on the string returned with action.get("stable")
Thank you for any help sorry if the question is a bit trivial.
I used this, and it worked perfectly.
<input type="checkbox" <c:if test="${item.estado==2}">checked=checked</c:if> class="switch-input" >
The correct markup for a checked checkbox is checked="checked". If it's not checked, the checked attribute must not be present at all.
You should generate it using the JSTL and the JSP EL, because scriptlets are something from the past which should not be used in JSPs for years. See How to avoid Java code in JSP files?.
This would of course need some refactoring so that the action bean has a regular isStable() method returning a boolean, which would be much cleaner. But anyway, here's how it would work using your existing code :
<input type="checkbox" name="stable" <%
if ("True".equals(action.get("stable"))) {
out.print("checked=\"checked\"");
} %>/>
Note that all attributes should also be surrounded by quotes.
You need to set checked attribute of <input type="checkbox"/>
Edit:
<input type="checkbox" <%=action.get("stable") ? "checked='checked'" : "" %> />

Categories