0

I'm trying to call a method that connects that is on a Bean on a jsp file.
The method will make a request to a RMI Server and return a string.
At this point the method is just return a pre-defined string for test.

This is the bean method:

public String getListProjects() throws RemoteException {

    this.dataToSend = new Object[2];
    this.dataToSend[1] = 0;

    this.postCard = new ClientRequest("2", this.dataToSend, "tempo");

    try{
      this.postCard = this.connectToRMI.getActualProjects(this.postCard);
    }catch(Exception e){
      e.printStackTrace();
    }

    return "Hello";

}

And this is the jsp code:

<h1>Projectos Actuais</h1>

<h2><%

    fundstarter.model.ConnectToRMIBean aux = new       fundstarter.model.ConnectToRMIBean();
    try{
        aux.getListProjects();
    }catch(Exception e){
        e.printStackTrace();
    }

    %>
</h2>

I'm guiding my self from another code, and the method is called like this. But in my case it's not working, and I can't figure out what is wrong.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243

4 Answers4

2

Since you've tagged this , assuming the getListProjects() is on the Action, in JSP use:

<s:property value="listProjects" />

If instead it is on a bean, declare the bean in the Action, and expose it through a getter:

private MyBean bean;

public MyBean getBean(){ 
    return bean; 
}

and in JSP use the dot notation:

<s:property value="bean.listProjects" />

P.S: always avoid Scriptlets (<% %>), they're evil.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
0

As per the flow of Struts there should be field in beanclass with same name of getter & setter. For an example if your method name is getListPorjects then in your bean class there should be a private string variable name listprojects.

Also your method will update with following to way to return listprojects.

example:

public String getListProjects() throws RemoteException {

    this.dataToSend = new Object[2];
    this.dataToSend[1] = 0;

    this.postCard = new ClientRequest("2", this.dataToSend, "tempo");

    try{
      this.postCard = this.connectToRMI.getActualProjects(this.postCard);
      listprojects = "hello"
    }catch(Exception e){
      e.printStackTrace();
    }

    return listprojects;

}

Calling bean variable should be with ID over the JSP page.

<jsp:useBean id="aux" class="com.path.to.ConnectToRMIBean" scope="request" /> 
----
yours stuff
-----

<h1>${aux.listProjects}

hope this will help you. good luck

Simmant
  • 1,477
  • 25
  • 39
0

You are just missing the way <% %> and <%= %> are used in JSP. to print in <% %> tags use

<% out.println("Your results"); %>

and for <%= %>

<%=
   String.valueOf(1+2);
%>
usman
  • 1,351
  • 5
  • 23
  • 47
  • what you mean..??, $ it self work as a scriplet for variable. – Simmant Dec 09 '15 at 07:27
  • i am referring to what is in question posted. not your code. how will this ever print ? <% aux.getListProjects(); %> – usman Dec 09 '15 at 07:36
  • Question was updated, previously code was there to print listProjects is with $ tag. And however using a object of bean is not correct way to show any beanclass element. – Simmant Dec 09 '15 at 07:42
0

Quoting and fixing your latest change on edit with some comments:

<h1>Projectos Actuais</h1>

<h2><%
    try{        
      fundstarter.model.ConnectToRMIBean aux = new       fundstarter.model.ConnectToRMIBean();

      //Send result into generated HTML page with out.print!
      out.print(aux.getListProjects());
    }catch(Exception e){
        e.printStackTrace();
    }

    %>
</h2>
Jan
  • 13,738
  • 3
  • 30
  • 55
  • @GabrielOliveira: Technically It may work, but scriptlets are bad, very bad. If you're using some framework (i.e. Struts2) learn how to do things in a framework way. Check [Andrea's answer](http://stackoverflow.com/a/34175556/1700321). – Aleksandr M Dec 09 '15 at 18:31
  • I totally agree. This is not the best answer. Just the one that was closest to the code at time of fix. – Jan Dec 09 '15 at 18:36