How to test jsp attribute type - java

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>

Related

Accesssing model attribute in JPA doesn't work

I am having problems accessing a model attribute in my controller, using Spring.
When adding to the model, I write the status code as a key and the enumeration name as a value. The status code is e.g. AVAILABLE, NOTAVAILABLE, etc.:
String code = status.getCode();
String enumerationName = enumerationService.getEnumerationName(status, currentLocale);
model.addAttribute(code, enumerationName);
On my JPA page, I am trying to access the corresponding value using the key (status code, e.g. AVAILABLE):
<div data-availability>
<c:forEach items="${StockLevelDeliveryStatus.values()}" var="status">
<c:set var="textStyle" value="text-success" />
<c:if test="${status.code.toLowerCase() == 'notavailable'}">
<c:set var="textStyle" value="" />
</c:if>
<div class="d-none display-22 pb-2 ${textStyle}" data-availability-item data-${status.code.toLowerCase()}>
${status}
</div>
</c:forEach>
</div>
For example, the value of status is AVAILABLE and this is what is output in ${status}. However, I want the value AVAILABLE to be used as a key to return me the correct value that I set in the model above. If I change the ${status} statement to, say, ${AVAILABLE} instead, which is the concrete key, the appropriate value from the model is returned:
<div class="d-none display-22 pb-2 ${textStyle}" data-availability-item data-${status.code.toLowerCase()}>
${AVAILABLE}
</div>
If I understand it correctly, then instead of passing the enum value as a key, I need to somehow teach Spring to search in the model for the appropriate key.
As recommended in one of the replies, I also tried writing the Map<StockLevelDeliveryStatus, String> directly into the model:
Map<StockLevelDeliveryStatus, String> statusMap = new HashMap<StockLevelDeliveryStatus, String>();
for (StockLevelDeliveryStatus status : StockLevelDeliveryStatus.values()) {
statusMap.put(status, enumerationService.getEnumerationName(status, currentLocale));
}
model.addAttribute("statusMap", statusMap);
And the JSP accordingly:
<div data-availability>
<c:forEach items="${StockLevelDeliveryStatus.values()}" var="status">
<c:set var="textStyle" value="text-success" />
<c:if test="${status.code.toLowerCase() == 'notavailable'}">
<c:set var="textStyle" value="" />
</c:if>
<div class="d-none display-22 pb-2 ${textStyle}" data-availability-item data-${status.code.toLowerCase()}>
${statusMap[status]}
</div>
</c:forEach>
</div>
Here it already fails when accessing the model, because with this approach I do not get any output on the JSP.
Does anyone have any ideas on how to make this work?
Why not simply put a Map<StockLevelDeliveryStatus, String> into the model? You could then simply use ${statusMap[status]}.

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>

passing string array as hidden value from one jsp to another jsp

I am trying to pass String array from one jsp to another. I am using JSTL in my JSP.
In my first JSP i am doing like this
<c:if test="${fn:length(empBean.additionalEmailAddr) gt 0}">
<c:forEach begin="0" end="${fn:length(empBean.additionalEmailAddr) - 1}" var="ind" >
<input type="hidden" name="inbdAdditionalEmailAddr" value="${empBean.additionalEmailAddr[ind]}"/>
</c:forEach>
</c:if>
and trying to access the values in another jsp as follows
<%
String[] inbdAddEmlAddr = request.getParameter("inbdAdditionalEmailAddr");
%>
and i am planning to use JSTL to print the array values.
In the second jsp i am getting type mismatch error. Please help.
Is this the right approach ? Any help is appreciated
Thanks
request.getParameter() returns a String which the code attempts to assign to a String[], causing the exception.
Use request.getParameterValues('inbdAdditionalEmailAddr'); to retrieve parameters as an array.
See the documentation.

JSTL c:if tag condition

<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.

Categories