Gabriel Oliveira
Gabriel Oliveira

Reputation: 1

Calling bean method on jsp

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.

Upvotes: 0

Views: 2262

Answers (4)

Andrea Ligios
Andrea Ligios

Reputation: 50203

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.

Upvotes: 2

Jan
Jan

Reputation: 13858

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>

Upvotes: 0

usman
usman

Reputation: 1391

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);
%>

Upvotes: 0

Simmant
Simmant

Reputation: 1513

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

Upvotes: 0

Related Questions