Access object instance method from and Array List in Spring View via JSTL
I'm new to Spring and not familiar very well with the framework.
I have a simple app where I create an Array List of objects, in the Controller, and pass it to the View, but in the view I cannot iterate trough the list to access object instances (and I want to access overrided toString method of an Object).
When I fill the Array List with strings (for testing purposes) I iterate them in the View just fine.
I've noticed, in the case of string filled array list, that all the values are passed via the URL query string, which is not the case when I use custom class object instances.
Can someone please explain what is the difference between these two cases, and why I can do it with strings and not with Custom class object instances?
Thank You.
Access object instance method from and Array List in Spring View via JSTL
Here is the code that works in case of strings and not in case of object instaces:
Osoba class:
Code:
package upload;
public class Osoba {
private String ime;
public Osoba(String ime)
{
this.ime = ime;
}
public String getIme() {
return ime;
}
public void setIme(String ime) {
this.ime = ime;
}
@Override
public String toString()
{
return this.ime;
}
}
The Controller:
Code:
package upload;
import java.io.File;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class FileUploadController extends SimpleFormController {
private static final String destinationDir = "C:/temp/spring/";
@Override
protected ModelAndView onSubmit(HttpServletRequest req,
HttpServletResponse res,
Object command,
BindException errors) throws Exception
{
//res.setContentType("text/plain");
if (!(req instanceof MultipartHttpServletRequest))
{
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Expected multipart request");
return null;
}
FileUploadBean bean = (FileUploadBean) command;
MultipartFile file = bean.getFile();
File destination = new File(destinationDir + file.getOriginalFilename());
file.transferTo(destination);
//res.getWriter().write("Success, wrote to " + destination.getAbsolutePath());
//res.flushBuffer();
//return new ModelAndView(getSuccessView(),"destination",destination.getAbsolutePath());
// This array works
ArrayList lista = new ArrayList();
lista.add("a");
lista.add("b");
lista.add("c");
//This one doesn't
Osoba a = new Osoba("John");
Osoba b = new Osoba("Peter");
Osoba c = new Osoba("Paul");
ArrayList<Osoba> lista = new ArrayList<Osoba>();
lista.add(a);
lista.add(b);
lista.add(c);
return new ModelAndView(getSuccessView(),"lista",lista);
}
}
The View:
Code:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload Success</title>
</head>
<body>
<c:forEach items="${paramValues.lista}" var="lista2">
<h3><c:out value="${lista2}" /></h3>
<br/>
</c:forEach>
</body>
</html>
Thank You.
+++++++++++++++++++++++++++++++++++
I've managed to make the things work by moving the controller code in the view, but then I'm thinking maybe that's the wrong thing to do?
What's the point of MVC if I do the processing in the view and not in the controller?
Here's the code. It gets the job done, but something tells me this approach is wrong, what do you think?
Code:
<%@page import="upload.Osoba"%>
<%@page import="java.util.*" %>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
Osoba a = new Osoba("John");
Osoba b = new Osoba("Peter");
Osoba c = new Osoba("Paul");
ArrayList<Osoba> lista = new ArrayList<Osoba>();
lista.add(a);
lista.add(b);
lista.add(c);
pageContext.setAttribute("lista", lista);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload Success</title>
</head>
<body>
<c:forEach items="${lista}" var="osoba">
<h3><c:out value="${osoba.ime}" /></h3>
<br/>
</c:forEach>
</body>
</html>
Re: Access object instance method from and Array List in Spring View via JSTL
Solved my problem using Http Session object.