aspose file tools*
The moose likes Struts and the fly likes Struts2 and setting data before a form is displayed. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 and setting data before a form is displayed." Watch "Struts2 and setting data before a form is displayed." New topic
Author

Struts2 and setting data before a form is displayed.

Michael Remijan
Author
Ranch Hand

Joined: May 29, 2002
Posts: 125
    
    5

I know this is a newbie question but I haven't been able to find an answer yet so please bear with me. I have a form on a page which looks like this:

Simple enough right? What I want to do is get data from the database so I can show it in the form for the user to update. My question is, where should this database access code be? I've created an ActionSupport class to process the form once it's submitted but those methods are not called when the page is loaded using a "portalmanagement_input" action. So what method is called?

Thanks,
Mike


Java EE Evangelist — Author, EJB 3 in Action 2nd Edition — Java Community Process Member
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
One way to do it would be to have your action class implement the com.opensymphony.xwork2.Preparable interface. Then override the prepare method to do whatever needs to be done prior to the page being displayed.


Merrill
Consultant, Sima Solutions
Michael Remijan
Author
Ranch Hand

Joined: May 29, 2002
Posts: 125
    
    5

Ok, that sounds easy enough. But looking at the interface for Preparable I don't think this quite answers my question. What is the preferred way of implementing the MVC pattern using struts2? What I mean is, typically a request goes to an "action" the action does some work, puts data into a request attribute, forwards onto a view, and the view picks up the data from the request attribute. How does one do this if there is no access to the request object?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Michael Remijan:
How does one do this if there is no access to the request object?

First of all, the assumption that there is no access to the request object is false. You do have access to it if you want it. All you have to do is implement ServletRequestAware and Struts will call your setServletRequest method, providing you with an instance of HTTPServletRequest.

It's just that Struts 2 tries to avoid use of the javax.servlet.* classes, mainly because direct references to these object make your classes harder to unit test. The paradigm in struts 2 is to put most everything in an action class and then let the JSP reference the data in the Action class.

One way to get access to the objects in the request without actually referencing it is to implement RequestAware instead of ServletRequestAware. RequestAware gives you a map of the objects attached to the Request which you can both read from and write to. This is much easier to unit test, as all your unit test has to do is provide a map for what is expected to be in the request, rather than having to try and build an HTTPServletRequest object.
Michael Remijan
Author
Ranch Hand

Joined: May 29, 2002
Posts: 125
    
    5

Ok, I think I get it. So the idea is to have the action object hold all of the dynamic data which is displayed on the page. I can then use the Preparable interface to pre-load the data before sending it to the jsp, right?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Yes, that's right.
roseline ruba
Greenhorn

Joined: Mar 21, 2007
Posts: 20
Thanks for the information.
i want to use Dynamic Beans in Struts2.0.9
how can i achieve this?
My aim is ..when i change the database table structure , i dont want to change the java class, instead i want to change the configuration file(xml file) as in struts1.
how can I achieve this is struts2.0.9.?
Please share your ideas..Thanks.


Roseline
rahuk Soni
Greenhorn

Joined: Oct 11, 2007
Posts: 10
Hi

I have also question about the struts2.0.9 and the database connection. I have created the class that get the data from the database means BeanClass of bean that contains the all the variables and getter and setter methods described below:
public class Person {
private String name;
private String address;
private String phone;
// getters and setters for the above properties
}

and then DBClass that query in the database that is as follow:

List listOfPeople = new ArrayList();

// get the database connection, create an SQL statement, and execute it to get a ResultSet rs

while (rs.next()) {
Person person = new Person();
person.setName(rs.getString("NAME"));
person.setAddress(rs.getString("ADDRESS"));
person.setPhone(rs.getString("PHONE"));
listOfPeople.add(person);
}


and then I am creating the object of the database class that returns the list and now I have to display the data from that list my ActionClass is as follow:

public class DBDisplay extends ActionSupport{
private String name ;
List list = null;

public String execute() throws Exception
{
list = DBClass.getInstance().list();
Iterator it = list.iterator();
while(it.hasNext())
{
name = (String) it.next();
}
setName(name);
}
public String getname()
{
return name;
}
public void setName(String name)
{
this.name = name;
}

}


I dont know there is changes from the struts 1.x to 2.x
I have read the article and according to that make the changes
the article is:

http://www.javaworld.com/javaworld/jw-10-2007/jw-10-adoptingstruts2.html?page=5

and then I have the jsp page that display the contain:

<s roperty value="name"/>

but it does not display the contents i dont know how to do that for the struts 2.0.9. I know that we have <html:bean > and <logic:iterate> class for the doing same thing in the struts1.x but how can i do that for the struts 2.0.9. Please Help me to found the solution. If I have done something wrong then also point out that and how can I do that to get the solution. I got the error is : classcastException of that object but i have to display the string something wrong with my code but i dont know what is wrong about that.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Rahuk,

Please do not post the same question in multiple threads. It wastes the time of those willing to give a response.

I'm closing this thread, but those wishing to respond may do so in this thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2 and setting data before a form is displayed.