spring:bind error using in a List - spring

I have a list containing users. I am trying to print it in JSP but some how I am not able to get it to print it. Getting this exception HTTP Status 500 - javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'users[0]' available as request attribute
Code in JSP
<c:forEach items="${users}" var="user" varStatus="status">
<spring:bind path="users[${status.index}].name">
<c:out value="${status.value}" />
</spring:bind>
</c:forEach>
Controller
ModelAndView modelAndView = new ModelAndView("go_some_JSP_page");
List<UserEntity> users = userManager.getAllObjects();
modelAndView.addObject("users", users);
BTW, UserEntity has name field. If I remove the binding and try to print the user.name using <c:out value="user.name" /> it prints the value
Where am I going wrong and what do I need to do? Thanks
Not working code below. [I have to invoke formatting on field #NumberFormat so have to try it using status variable]
<spring:bind path="user.name">
<c:out value="${status.value}" />
</spring:bind>
Gets this error --> javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
So added a bean binding and then I get empty table :(. I believe thats because the instance is empty. So this does not seems like a right approach.
#ModelAttribute("user")
public UserEntity userEntityBinding() {
return UserEntity.newInstance();
}
A working code exists at https://github.com/hth/StatusInvoke.git
Let me know if you face any problem deploying it.
This question has been solved. Thanks for looking at it.

You can try using LazyList instead of simple list. If you want to take a look at the example then you can refer one of my question. In the question statement I have mentioned how to use the LazyList.
Hope that helps you. Cheers.

this, if the modelandview are returned, is the correct way to populate the list
ModelAndView modelAndView = new ModelAndView("go_some_JSP_page");
List<UserEntity> users = userManager.getAllObjects();
modelAndView.addObject("users", users);
And this is the correct way to reference the list
<c:forEach items="${users}" var="user" varStatus="status">
<spring:bind path="user.name">
<c:out value="${status.value}" />
</spring:bind>
</c:forEach>
Your problem must be elsewhere is the name field definitely populated, is the correct jsp being called... the above code is correct and should work.

The correct answer to invoke #NumberFormat annotation is by using spring:eval expression tag
<spring:eval expression="user.balance" />
This invokes the annotation and performs formatting as mentioned in the annotation

I don't think you can use spring:bind in that case, AFAIK it tries to get the variable from the ModelMap, it's not able to get it from the "for" var.

Related

Spring mvc: I cant map form action url to controller action

I apologize in advance if this or a similar question has already been asked, but I could not find a suitable answer.
I have a simple form like this in EditUser.jsp (mapped to: .../admin/users/edit/{userId}):
<form action="/admin/users/edit/addRole/${user.userId}" method="POST">
<select name="role">
<c:forEach var="role" items="${roles}">
<option value="${role}">${role}</option>
</c:forEach>
</select>
<button type="submit" value="AddRole">Add Role</button>
</form>
And #RequestMapping like this:
#RequestMapping(value = "/admin/users/edit/addRole/${userId}", method = RequestMethod.POST)
public String addUserRole(
Model model,
#RequestParam("role") String role,
#PathVariable(value="userId") long userId)
{
...
return "redirect:/admin/users/edit/${userId}";
}
The problem is the result of the request: HTTP Status 404 - /admin/users/edit/addRole/7- "The requested resource is not available" (7 is some user id). A cannot map the POST request to the controller action. I already tried with th:action but it redirects me to the previous page .../admin/users.
Any help pointers appreciated .
I think you url is wrong. As long as you do not deploy the application in the servlets container root path, it will not work because the url is missing the applications name. So a correct url would be something like:
<form action="myAppName/admin/users/edit/addRole/${user.userId}" method="POST">
But better would been using <c:url> or <spring:url>-tag this adds the application name to the url (if the given url starts with an /)
<form action="<c:url value="/admin/users/edit/addRole/${user.userId}" />" method="POST">
for some more information have a look at this two answers:
How to use relative paths without including the context root name? (the highes ranked answer of BalusC is for an quite old jsp version (<2.0)) so take the answer with the c:url tag
How to use with an tag?
I finally found the error - $ sign in #RequestMapping annotation. A just removŠµ $ from annotation and from return "...url" and that's all.

Path attribute in form:select - spring 2.5 JSP

I've been put on the task of learning spring (2.5), and I've got into a problem using the attribute where, when the form is loaded, I wont get any pre-selected values. So the situation is as follows:
In my system I have Companies, Customers and Users. Customer extends Company, and a Company can have a List of Users (the getMethod is public so hence Customer also have a List of Users).
So this is how the form select looks in my .JSP:
<form:select multiple="true" id="selectAccountManager" path="${customer.users}" cssClass="input select_img" >
<c:forEach items="${customerUsers}" var="user" >
<form:option value="${user.id}
<c:out value="${user.displayName}" />
</form:option
</c:forEach>
</form:select>
Right now, I get an error on the path="${customer.users}". If I use path="users" my system works, but then I wont get any of the values within the form as "pre-selected" when the page loads. I've tried with path="customer.users" but when I do this I get a referenceError in js-console.
The .JSP is mapped to an EditCustomerController where customerUsers is put into a map by
map.put("customerUsers", UserControlHelper.getAllUsers());
So I guess this is where the problem lies as I always get all users from the system?
TL;DR: How do I set selected values on a form where I load the items from one class, and want the select-filter to come from another?
(First of all, Spring 2.5 is old and I suggest you to use 3.2.x or even 4.0.x)
To pre-select option you should set appropriate field on the model and Spring will pre-select it automagically. Like that:
<form:form method="post" modelAttribute="myForm">
<form:select multiple="true" path="users">
<form:options items="${customerUsers}" itemLabel="displayName" itemValue="id" />
</form:select>
</form:form>
And in the controller:
User defaultUser = new User(1, "DEFAULT");
MyForm myForm = new myForm();
myForm.getUsers().add(defaultUser);
model.addAttribute("myForm", myForm);
So I found out what the problem was.
When I created my map I only put the users in there. What the User-object actually was mapped upon was the id of the user (something I retrieved through ${user.id} as can be seen in the originalpost. What I had to do was to create the path in a correct way as such:
Controller - java:
Map<Int,String> users = new HashMap<Int,String>();
for(User user: UserControlHelper.getAllUsers()){
users.add(user.getId(), user.getDisplayName())
}
model.put("customerUsers", users);
and my jsp:
<form:select path="users" items="${customerUsers}"></form:select>

Spring MVC : How to pass Model object from one method in controller to another method in same controller?

I have integrated Spring Security in my application , and would like to display an error message to the user in case of Bad credentials.
jsp:
<c:out value='${secerror}' /> //prints nothing on screen
<c:if test="${empty secerror}">
error is empty or null. //always prints this line on screen
</c:if>
<c:if test="${not empty secerror}">
<div class="errorblock">
Your login attempt was not successful, try again.<br /> Caused :
${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
</div>
</c:if>
<c:set var = "url" value = "/j_spring_security_check" />
<form:form method="post" commandName="portalLogin" action="${pageContext.servletContext.contextPath}${url}" name="f">
[Update]: Sorry all, i realized that my Model object was getting overriden after i redirect to portalLogin.html as i had created a new model object created there previously.
But i tried few easy options by which i can pass Model object from one controller method to another method in the same controller. But nothing worked.
I tried using forward: prefix instead of redirect prefix. For this, i didn't get error message at all.
I tried below code in loginerror method.
return new ModelAndView("portalLogin","secerror","true");
I was getting following error for the above code:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'portalLogin' available as request attribute
I did come across this link, but i found it to be a very lengthy solution and wasn't sure if iv'e to write that much code.
I wasn't sure if i can use Model object for #ModelAttribute annotations#ModelAttribute.
Please provide me with code snippets / examples which i can try out.
My controller method is like this:
#RequestMapping("/portalLogin")
public ModelAndView goToLogin(Model model) {
try {
System.out.println("Enter goToLogin************************");
} catch (Exception exception) {
}
return new ModelAndView("portalLogin", "portalLogin", new LoginModel());
//return new ModelAndView("portalLogin", model);
}
#RequestMapping(value="/loginfailed", method = RequestMethod.GET)
public ModelAndView loginerror(ModelMap model) {
//model.addAttribute("secerror", "true");
//return "redirect:portalLogin.html";
return new ModelAndView("portalLogin","secerror","true");
}
[Update]: As a work around i added goToLogin method logic inside loginerror method as my only intention is to get portalLogin page. Error was thrown as expected.
#RequestMapping(value="/loginfailed", method = RequestMethod.GET)
public ModelAndView loginerror(Model model) {
model.addAttribute("secerror", "true");
return new ModelAndView("portalLogin", "portalLogin", new LoginModel());
}
But still i would like to know if i can pass Model object from one controller method to another method in the same controller through some way.
You can also try something like this
<c:if test="${not empty secerror == 'true'}">
<tr>
<td colspan="2" align="center"><font style="color: red">Your login attempt was not successful, try again</font>
</td></tr></c:if>
Let`s make things easy, if you want to show a Bad credentials message, you can simply do something like this:
In your spring-security.xml:
<sec:form-login login-page="/login.jsp"
default-target-url="/default-url-when-login-correct"
authentication-failure-url="/login.jsp?error=true"
login-processing-url="/login" always-use-default-target="true" />
In your login.jsp:
<c:if test="${not empty param.error}">
<span>${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}</span>
</c:if>

Creating a form in Spring

I have a simple contact form in my spring project, which is meant to access a backing object, but I get this error
"Neither BindingResult nor plain target object for bean name 'indexBacking' available as request attribute"
My form looks like this:
<form:form action="index.htm" enctype="multipart/form-data" method="post" commandName="indexBacking" accept-charset="UTF-8">
<form:label path="personName">Name</form:label>
<form:input id="personName" path="personName" autocomplete="false" /><br />
<form:label path="personEmail">Email</form:label>
<form:input id="personEmail" path="personEmail" autocomplete="false" /><br />
<form:label path="personComments">Your Comments</form:label>
<form:input id="personComments" path="personComments" autocomplete="false" /><br />
<input type="submit" alt="Submit"/>
</form:form>
Which is meant to access my controller and save the fields "personName", "personEmail" and "personComments" into my backing object called "indexBacking".
My controller method that I am trying to access is here:
#RequestMapping(value = PAGE_NAME, method = RequestMethod.POST)
public String handleContactForm(ModelMap map, HttpServletRequest request, #ModelAttribute("indexBacking") IndexBacking bo, BindingResult result) {
return MODEL_NAME;
}
But I am not sure hot it links with the backing object. Any ideas what I am doing wrong?
Thanks
Jon
Try using modelAttribute="indexBacking" on form:form instead of commandName="indexBacking".
Also, take a look at this answer; it might have useful information for your case.
The problem was very simple, I was just being an idiot. I saw a colleague of mine working on a form and assumed a few of his classes were part of Spring as default. All I had to do was handle the received data at the other end properly (by calling the appropriate methods in the controller) and it worked fine.
Thanks for your help chaps - credit to #nobeh for pointing me in the right direction.
The problem is in your controller!
The following may help you much for your request Check This

SpringMVC - JSP Iteration Issue

I have the following class that I am attempting to use as a command object
public class Member {
private String datePeriod;
private String status;
private ArrayList <Project> projectList;
}
On the JSP, I would like the form to prefill with a pre-existing values.
<c:forEach items="${member.projectList}" var="project">
<tr>
<td><form:input path="**<var???>**" value="${project.projectEntry.projectDesc}" /></td>
</tr>
</c:forEach>
I am making an error with path which is causing an error in jsp. Does anyone know the proper syntax with regards to each iteration? Thanks.
My Spring MVC is bit rusty but if I remember correctly, path gets translated as the input name property in HTML eventually. So you can put any Label value in there and that should work.
<form:input path="ProjectDescription" value="${project.projectEntry.projectDesc}" type="text" />
This should get translated to:
<input name="ProjectDescription" type="text" value="<whatever_you_have_in_backing_bean>"/>
Do you want to look up name from a backing bean instead? If so you should be able to do something like below. Without knowing your data structure I am assuming it to be status.
<form:input path="member.status" value="${project.projectEntry.projectDesc}" type="text" />
More on the form tag here.

Resources