Results 1 to 3 of 3
- 01-05-2013, 03:42 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 24
- Rep Power
- 0
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.Last edited by mcdhappy80; 01-06-2013 at 03:24 AM. Reason: Solved the first problem with a tutorial
- 01-06-2013, 04:05 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 24
- Rep Power
- 0
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:
The Controller:Java 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 View:Java 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); } }
Thank You.Java 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>
+++++++++++++++++++++++++++++++++++
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?
Java 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>Last edited by mcdhappy80; 01-06-2013 at 05:31 PM.
- 01-25-2013, 04:27 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Spring Framework
By NoobSaibot in forum New To JavaReplies: 2Last Post: 10-10-2011, 06:47 PM -
how to learn spring framework
By m16k2002 in forum Web FrameworksReplies: 6Last Post: 04-06-2009, 02:14 PM -
What is RmiServiceExporter in Spring framework
By Java Tip in forum Spring FrameworkReplies: 0Last Post: 04-02-2008, 10:37 AM -
What is RmiServiceExporter in Spring framework
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:21 PM -
What is RMI concept in Spring Framework
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks