wood burning stoves 2.0*
The moose likes Struts and the fly likes Display struts output to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Display struts output to jsp" Watch "Display struts output to jsp" New topic
Author

Display struts output to jsp

leo oke
Greenhorn

Joined: Jul 09, 2007
Posts: 21
I am trying to display userList from my struts action to jsp page,what's the code to do it using java beans or something simple?

part code:
action class
------------------------------------------------------------------------------------------------------------------------
public ArrayList userList = new ArrayList();


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {


//get connection from database
javax.sql.DataSource dataSource;
java.sql.Connection myConnection = null;



// Here the method that connects to the datasource is called:
dataSource = getDataSource(request);
myConnection = dataSource.getConnection();

UserDAO dao = DAOFactory.createUserDAO(myConnection);
userList = dao.getUser();
HttpSession session = request.getSession();

// extract data
if(userList !=null){

//code here
return mapping.findForward("success");
}else{
return mapping.findForward("fail");
}



}
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
request.setAttribute("userListBean",userList);
leo oke
Greenhorn

Joined: Jul 09, 2007
Posts: 21
Thanks for the reply..
However, I've created the bean class ...below,but I don't see the output userlist only hello...why?

part code;
-----------------------
public class UserListBean {

public ArrayList userList;

public ArrayList getUserList() {
return userList;
}

public void setUserList(ArrayList userList) {
this.userList = userList;
}

public UserListBean(ArrayList userList) {
this.userList = userList;
}

---------------------------------------------------
The jsp out

part code

<title>success</title>
</head>
<body>

<h2>Hello</h2>

<logic resent name="UserListBean">

<h1>
Welcome: <bean:write name="UserListBean" property= "userList"/>
</h1>


</logic resent>






</body>
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
Did you set the response as I suggested in the action?
leo oke
Greenhorn

Joined: Jul 09, 2007
Posts: 21
Thanks for your reply again,I know is something simple making it not working,but I've put the response like this...

part code
----------------------------------------------------------------------------

public ArrayList userList = new ArrayList();


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {

............................................................................

Is this correct or how is it done?
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
I don't see you putting it in the response anywhere in that code snippet.
leo oke
Greenhorn

Joined: Jul 09, 2007
Posts: 21
I've put the response snipped in like this ....
HttpSession session = request.getSession();

in the code is this correct?
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Leo,

Chris gave you the answer in his first reply: just put the list in the request, using the same name as in the .jsp (should not be difficult)

request.setAttribute("UserListBean",userList);

Herman
 
wood burning stoves
 
subject: Display struts output to jsp