Question

I have a jsp file and a bean file. I learnt how primitive data types are converted using 'valueOf' method and the bean property is set , however i am still confused how class type values are set. The code below will make the query more clear.

Bean.java :

private Object myObject ;

   public Object getMyObject() {
        return myObject;
    }

   public void setMyObject(Object myObject) 
    {
        System.out.println("my object - " + myObject);

        File file = (File)myObject;
        System.out.println("path - " + file.getPath());

        this.myObject = myObject;
    }

Index.jsp :

    <jsp:useBean id="aBean" class="com.Bean" />    
        <%
            File file = new File("some path");
        %>
  <jsp:setProperty name="aBean" property="myObject" value="<%= file %>" />

I am quite confused with how the thing value="<%= file %>" works. Thanks.

Was it helpful?

Solution

File extends Object (like all classes do). So the above is simply compiled by the JSP container to something like

com.Bean aBean = new com.Bean();
File file = new File("some path");
aBean.setMyObect(file);

There's nothing to convert, since a File is an Object.

Note that jsp:useBean and jsp:setProperty are obsolete for a looooong time. You shouldn't use these directives anymore. Use an MVC controller (or at least a self-implemented MVC pattern), and use the JSTL and the JSP EL to access beans created and stored in request attributes by the controller. The view (i.e. the JSP) shouldn't create and populate beans. That's not its job.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top