Results 1 to 3 of 3
  1. #1
    mcdhappy80 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Question 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 04:24 AM. Reason: Solved the first problem with a tutorial

  2. #2
    mcdhappy80 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Question 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:
    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 Controller:
    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);
        }
    }
    The View:
    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>
    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?
    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 06:31 PM.

  3. #3
    mcdhappy80 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default Re: Access object instance method from and Array List in Spring View via JSTL

    Solved my problem using Http Session object.

Similar Threads

  1. Spring Framework
    By NoobSaibot in forum New To Java
    Replies: 2
    Last Post: 10-10-2011, 07:47 PM
  2. how to learn spring framework
    By m16k2002 in forum Web Frameworks
    Replies: 6
    Last Post: 04-06-2009, 03:14 PM
  3. What is RmiServiceExporter in Spring framework
    By Java Tip in forum Spring Framework
    Replies: 0
    Last Post: 04-02-2008, 11:37 AM
  4. What is RmiServiceExporter in Spring framework
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:21 PM
  5. What is RMI concept in Spring Framework
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •