I'm looking for method to inject value into a <c:if test>. The code below works if I substitute
<% out.print(request.getSession().getAttribute("UserName").toString()); %>
with some constants. But how can I parse a value in <%%> tag into a <c:if test>?
<netui-data:repeater dataSource="pageFlow.availableBooks">
<tr>
<td><netui:label value="${container.item.bookName}" /></td>
<td>
<c:if test="${container.item.createdBy == '<% out.print(request.getSession().getAttribute("UserName").toString()); %>'}">
<netui:anchor action="removeBookAction" value="Remove" formSubmit="true">
<netui:parameter name="bookID" value="${container.item.bookID}" />
</netui:anchor>
</c:if>
</td>
</tr>
</netui-data:repeater>
Thanks in advance!
You can use sessionScope to read the value from session scope. The following will help you to resolve:
<c:if test="${container.item.createdBy == sessionScope.UserName}">
Related
This is my code
<c:set var="radioCount" scope="session" value="${radiocount}"/>
I want to create a table with no of rows equal to radiocount so trying to use the below expression but getting an error
<c:forEach begin='1' end='radioCount'>
<tr>
<td>
<input style='width:500px' type='text' / >
</td>
</tr>
</c:forEach>
Try to use bracktes in your forEach and use double quote " instead of ' in your code:
<c:forEach begin="1" end="${radioCount}">
I have a TreeMap<Long,ArrayList<String>> that I need t print it in Struts2 jsp page. I have to add an If condition in such a way that If the content of ArrayList<String> contains certain character sequence I need to print it in one color and if does not have that character sequence I need to print it in different color.
In the below code, only else part is getting printed,
<s:set name="string1" value="check_Char" />
<s:iterator value="lgMap">
<h3>
<s:property value="key" />
</h3>
<table>
<s:iterator value="value">
<s:if test="%{<s:property />.indexOf(#string1)) == -1}">
<tr>
<td><font color="green"><s:property /></font> </td>
</tr>
</s:if>
<s:else>
<tr>
<td><font color="red"><s:property /></font></td>
</tr>
</s:else>
</s:iterator>
</table>
</s:iterator>
There is problem with the if statement i believe. Can one suggest how to reach the if part?
You can't nest Struts Tags.
Change this
<s:if test="%{<s:property />.indexOf(#string1)) == -1}">
to this
<s:if test="%{top.indexOf(#string1) == -1}">
I have a JSP page used for editing some user's info. When a user logins to the website, I keep the information in the session, then in my edit page I try the following:
<%! String username=session.getAttribute("username"); %>
<form action="editinfo" method="post">
<table>
<tr>
<td>Username: </td><td> <input type="text" value="<%=username %>" /> </td>
</tr>
</table>
</form>
but it gives error saying session cannot be resolved. What can I do about it?
JSP implicit objects likes session, request etc. are not available inside JSP declaration <%! %> tags.
You could use it directly in your expression as
<td>Username: </td>
<td><input type="text" value="<%= session.getAttribute("username") %>" /></td>
On other note, using scriptlets in JSP has been long deprecated. Use of EL (expression language) and JSTL tags is highly recommended. For example, here you could use EL as
<td>Username: </td>
<td><input type="text" value="${username}" /></td>
The best part is that scope resolution is done automatically. So, here username could come from page, or request, or session, or application scopes in that order. If for a particular instance you need to override this because of a name collision you can explicitly specify the scope as
<td><input type="text" value="${requestScope.username}" /></td> or,
<td><input type="text" value="${sessionScope.username}" /></td> or,
<td><input type="text" value="${applicationScope.username}" /></td>
Use
<% String username = (String)request.getSession().getAttribute(...); %>
Note that your use of <%! ... %> is translated to class-level, but request is only available in the service() method of the translated servlet.
See how JSP code is translated to a servlet.
The reason why you are getting the compilation error is, you are trying to access the session in declaration block (<%! %>) where it is not available. All the implicit objects of jsp are available in service method only. Code of declarative blocks goes outside the service method.
I'd advice you to use EL. It is a simplified approach.
${sessionScope.username} would give you the desired output.
Suppose you want to use, say ID in any other webpage then you can do it by following code snippet :
String id=(String)session.getAttribute("uid");
Here uid is the attribute in which you have stored the ID earlier. You can set it by:
session.setAttribute("uid",id);
<%! String username=(String)session.getAttribute("username"); %>
form action="editinfo" method="post">
<table>
<tr>
<td>Username: </td><td> <input type="text" value="<%=username %>" /> </td>
</tr>
</table>
add <%! String username=(String)session.getAttribute("username"); %>
You can directly use (String)session.getAttribute("username"); inside scriptlet tag ie <% %>.
form action="editinfo" method="post">
<table>
<tr>
<td>Username:</td>
<td>
<input type="text" value="<%if( request.getSession().getAttribute(" parameter_whatever_you_passed ") != null
{
request.getSession().getAttribute("parameter_whatever_you_passed ").toString();
}
%>" />
</td>
</tr>
</table>
</form>
I have a very strange problem.
<table border ="1">
<tbody>
<c:forEach var="question" items="${questions}">
<tr>
<td>
${question.getQuestion()}
</td>
<td>
<c:forEach var="answer" items="${question.getAnswers()}">
<input type="checkbox" name ="user_answer" value="${answer.getAnswer()}">
${answer.getAnswer()}
<br />
</c:forEach>
</td>
<td>
<a href="/TutorWebApp/controller?command=edit_qestion&question=${question}">
Edit
</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
But If I use in I get next error
But if I don't use tag <a> in <td> it's OK. I don't have any ideas.
Thanks
I think this is just a bug/limitation of your editor. Try deploying your JSP and see if it works as expected or not.
That said, if your question contains characters that must be URL and/or HTML escaped, your HTML code will be invalid. You should use the c:url tag to avoid that:
<c:url var="editQuestionUrl" value="/TutorWebApp/controller">
<c:param name="command" value="edit_question"/>
<c:param name="question" value="${question}"/>
</c:url>
<%-- now the params are url-encoded --%>
Edit
<%-- now the query string is HTML-escaped --%>
You need to encode your question text (or whole URL) here by calling URLEncoder#encode()
You can look at this Q&A on how to encode a URL in JSTL.
Alternatively you can try calling JSTL's escapeXml function on your question text.
try replacing this line
<a href="/TutorWebApp/controller?command=edit_qestion&question=${question}">
with
<a href="/TutorWebApp/controller?command=edit_qestion&question='${question}'">
Spring MVC Error Messages
Hello, Spring Fellows,
I have a form that is validated by the Spring Validation once submitted. Each field on the form may contain multiple errors messages if validation fails, so error messages are displayed below the field, not next to it. Here's the code snippet.
<tr>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
Note that there is a star at the end of the path value to indicate that all error messages for the name must be displayed.
As you can see, the problem is that, if there is no error message, there will be an extra row on the page that looks out of place to the user. The code above is an overly simplied version, so the actual code has a lot more stuff in it, which prevents me from moving the <form:errors> tag inside the tag containing the field.
Is there a way to find out if there is any message associated to a given path on the JSP level? Basically, I would like to do the following:
<c:if test="${what do I write here?}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
Thanks!
You can do something like this (notice that bind is from spring taglib):
<spring:bind path = "name*">
<c:if test="${status.error}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
</spring:bind>
I solved your problem by doing this:
<table>
<form:errors path="firstName">
<tr>
<td colspan="2">
<form:errors path="firstName"/>
</td>
</tr>
</form:errors>
<tr>
<td><form:label path="firstName"><spring:message code="helloworld.label.firstName"/></form:label></td>
<td><form:input path="firstName"/></td>
</tr>
</table>
errors tag body will be evaluated only if there are errors on the path.
The simplest answer is to not use tables for page layout. Utilizing div tags alleviates this problem altogether since divs are completely dimensionless if set to hidden.