In MVC passing a list from JSP to the Controller - spring

I'm trying to get an example of passing a list from JSP to the Controller to work:
http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/
the problem I have with this example is that I do not get the JSP /WebContent/WEB-INF/jsp/add_contact.jsp to work. He has the line:
<c:foreach items="${contactForm.contacts}" var="contact" varstatus="status">
and i'm getting errors with the varstatus variable. From where is he getting the values for this variable? He is using it as a list row index, but from where should the values come? I get the warning in eclipse that it is a not defined variable varstatus and if i still deploy it to tomcat, then i get the error that jstl foreach tag does not support more than one variable (as i already have the contact there).

The forEach tag and attributes are case sensitive.
<c:forEach items="${contactForm.contacts}" var="contact" varStatus="status">
<c:out value="${status.index}" />: <c:out value="${contact}" />
</c:forEach>
It's possible the error messages generated by Eclipse are misleading. It's also possible the error is originating from somewhere else on the page. If this does not resolve the problem, post the full JSP and stacktrace for better understanding.

Related

spring form:form data-binding inside c:forEach

I would like to get some data-binding working the following way:
<c:forEach var="form" items="${forms}" varStatus="status">
<form:form modelAttribute="form">
<form:input path="key"/>
</form:form>
</c:forEach>
But the problem is, I think that the model attribute can't be a dynamic reference or at least I don't know how to express that.
forms is a collection of objects which have an attribute key.
I found many other solutions, which for instance use the status variable like
<form:input path="${forms[status.index].key}"/>
but i need to reference "key" directly instead of this array access prefix.
any ideas how to achieve that?
Try this,
<form:form modelAttribute="${form}">
<form:input path="${form.key}"/>
</form:form>
Also, if you are using the Map collection then you can access the value using ${form.value}.
In your code <form:form modelAttribute="form"> express string form as modelAttribute name and not the actual value which you wish to bind as modelAttribute.

Use of spring security jstl tag <sec:authority access="hasPermission(#domain, 'permission')> inside a jstl core loop

I'm using facelets (JSF2.1) and I'm trying to do something like:
<c:forEach var="domainObject" items="#{MB.listOfDomainObjects}">
<sec:authorize access="hasPermission(#domainObject,'PERMISSION_X')">
hello world
</sec:authorize>
</c:forEach>
I've tried changing #domainObject by #domainObject, #{domainObject}, $domainObject and a lot of other combinations with the same result: domainObject is not processed correctly. Sometimes I get an error saying the page cannot be constructed, others domainObject is null.
It's like the scope where the tag c:forEach puts the variable domainObject is not scanned by sec:authorize to find it.
I've tried also to force the use of a scope using the tag <c:set ... scope="view"/>. I've also tried to use <ui:repeat> with the same results, but considering the tag sec:authorize is a jstl one, I suppose is executed in build time (like c:forEach) and not in render time (like ui:repeat), so I think I must use c:foreach and not ui:repeat.
Any chance to solve my problem?

Spring binding errors: display only some messages in JSP

In a JSP I'm listing all the binding/validation errors together:
<ul>
<c:forEach items="${status.errorMessages}" var="error">
<li><c:out value="${error}"/></li>
</c:forEach>
</ul>
However, I have some custom behavior and would like to ignore all the "typeMismatch" errors. Is there a way to do something like the following?
<c:if test="${not error.isTypeMismatch}">
<li><c:out value="${error}"/></li>
</c:if>
Since ${error} is just a plain old String, I don't know how I could determine something like this.
Thanks for your ideas!
Since errors are just strings, I don't see a way to separate type mismatch errors from others other then checking if the error actually contains some text expected to be in type mismatch errors.
${not error.contains('[some type mismatch text]')}

Cannot access variable inside the included JSP

I am using Spring 2.5 in my application. In the view I have main jsp in which I have included another jsp. I have declared a variable using c:set tag in main jsp which I can not access inside the jsp. Below is code
main.jsp
<c:set var="hPediquestStudy"><spring:message code="study.hpediquest.mapping" /></c:set>
<c:set var="currentStudy" value='<%=(String)session.getAttribute("currentStudy")%>'/>
<html>
<head>
</head>
<body>
<c:if test="${currentStudy eq hPediquestStudy}">
Variables are equal
</c:if>
<c:if test="${currentStudy ne hPediquestStudy}">
Variables are not equal
</c:if>
<jsp:include page="/WEB-INF/jsp/included.jsp"></jsp:include>
</body
</html>
included.jsp
<c:if test="${currentStudy eq hPediquestStudy}">
hPediquestStudy Variable is accessible
</c:if>
<br/>currentStudy : ${currentStudy}
<br/>hPediquestStudy : ${hPediquestStudy}
I am getting output
Variables are equal
currentStudy : hPediquest
hPediquestStudy :
Why on main jsp both values are equal while in included jsp I dont see its value?
Why currentStudy displays its value inside the included jsp?
is there any solution which helps me to access the variable set in the parent jsp ans can be accessed in the included jsps?
I can see the value of hPediquestStudy value if I set that variable in included jsp like in main jsp. But I dont want to set it every time when I include the jsp. Please help
Why on main jsp both values are equal while in included jsp I dont see its value?
Because <c:set> by default stores them in the page scope.
Why currentStudy displays its value inside the included jsp?
Because it's available as session attribute as well.
is there any solution which helps me to access the variable set in the parent jsp ans can be accessed in the included jsps?
You need to set the scope attribute of <c:set> to request or above. The (default) page scope is exposed to current JSP only, not any included JSPs.
Note that the line
<c:set var="currentStudy" value='<%=(String)session.getAttribute("currentStudy")%>'/>
is unnecessary. The ${currentStudy} will already scan for variables in page, request, session and application scopes. As you've apparently already set it in the session scope, you don't need to copy it into the page scope. So, just remove that line. All with all, your top 2 <c:set> lines should be replaced with this single line:
<c:set var="hPediquestStudy" scope="request"><spring:message code="study.hpediquest.mapping" /></c:set>
and then it'll work the way you intended.
See also:
Our EL wiki page

Spring Framework - Display Warning Message as opposed to Error

I have Use Cases that require the User to be show a Warning message as opposed to an Error message. The user will be shown once and then they can proceed. My problem is since it's not a Validation error the flow goes thru and the operation is performed without displaying the message. Therefore the user performs an action, he is warned once and then let thru.
Am I missing something? I looked around but there doesn't seem to be the concept of a warning message in Spring. Thanks for your help or suggestion.
In my ignorance of Spring and Spring-MVC, I created a Messages class that contains lists of errors and messages. I store an object of this type in the request scope and reference it in my JSP somewhat like this:
<c:if test="${Messages.hasMessages}">
<c:forEach items="${Messages.messages}" var="message">
${message}<br/>
</c:forEach>
</c:if>
<c:if test="${Messages.hasErrors}">
<c:forEach items="${Messages.errors}" var="error">
${error}<br/>
</c:forEach>
</c:if>
In my actual JSP, I style it as needed and generally dump messages and errors in a <ul> list.

Resources