Author
Display struts output to jsp
leo oke
Greenhorn
Joined: Jul 09, 2007
Posts: 21
posted Aug 19, 2007 03:12:00
0
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
posted Aug 20, 2007 13:33:00
0
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
posted Aug 21, 2007 08:33:00
0
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
posted Aug 26, 2007 13:16:00
0
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
subject: Display struts output to jsp