注册 登录
编程论坛 J2EE论坛

数据页面传递问题,菜鸟的提问!

zhangjipeng 发布于 2008-01-02 11:01, 1730 次点击
我用hibernate+struts的,我在action页面用了查询,然后想把查询结果放到Action跳转后的页面能使用,我该怎么做。下面是我的代码:action 页面代码:
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        String FD="ok";
        List Li = null;
        Stu stu=new Stu();
        WelcomeForm welcomeForm = (WelcomeForm) form;// TODO Auto-generated method stub
        Session session = HibernateSessionFactory.getSession();
        String hql = "from Stu t where t.name='"+welcomeForm.getName()+"' and t.password='"+welcomeForm.getPassword()+"'";        
        Query query=session.createQuery(hql);
        Li = query.list();
        HttpSession sess2=request.getSession();
        sess2.setAttribute("Li", Li);
        
        return mapping.findForward(FD);
    }

 跳转后jsp页面使用代码:
<%List Li= (List)request.getParameter("Li"); %>(报错说String不能往List转)
12 回复
#2
hwoarangzk2008-01-02 12:42
可以用EL来显示结果。
比如你已经放到Li里面了。
<c:forEach var="user" items="${Li}">
这里你把Li放在了user里面,如果Li有id,name,password属性,那么
<c:out value="${user.id}" />这里就显示出了id属性,以此类推
#3
神vLinux飘飄2008-01-02 13:29
struts2.0 直接用便簽取值啊。。。。
#4
gongbing2008-01-02 13:52
<s:property  value="">
#5
hwoarangzk2008-01-02 13:53
厄,的确,Struts里面有个bean标签,可以列出你某个bean的所有信息。我说的那个是重复列出所有bean的所有属性用到的
#6
神vLinux飘飄2008-01-02 14:21
4樓說的沒錯這種<s:property  value="">   循環列表也可以:
<s:iterator value="filesInfoList" status="fileList">
<s:property  value="id">   
  </s:iterator>
#7
hwoarangzk2008-01-02 14:25
没用过那种,有待学习...
#8
神vLinux飘飄2008-01-02 15:02
哈哈,struts1.0和2.0差別很大,struts2.0的核心是webwork哦
#9
hwoarangzk2008-01-02 15:12
目前还在用Struts1.0......有空学习下
#10
zhangjipeng2008-01-03 09:43
解决方法不清楚
谢谢各位大哥。。。不过我最后是用最土的办法做的,因为我照版主说的做做不出来,以下是我照版主说的写的代码:
 <td width="25%">name</td><td width="25%">password</td><td width="25%">sex</td></tr>
    <c:forEach var="user" items="${Li}"></c:forEach>
    <tr><td><c:out value="${user.name}" /></td><td><c:out value="${user.sex}" /></td><td><c:out value="${user.password}" /></td></tr>
不知道 错哪里,下面是我用土办法写的,可以实现,各位大哥帮我看下用上面的方法为什么不行!小弟先谢过了。

<%List Li= (List)session.getAttribute("Li");%>
   <% for (int i=0;i<Li.size();i++){  
   Stu stu=(Stu)Li.get(i);
   String l=stu.getName();
   String l2=stu.getPassword();
   String l3=stu.getSex();%>
   <%=l%>你好,你的密码是: <%=l2%>,性别是: <%=l3%>。  
<%}%>
#11
hwoarangzk2008-01-03 10:44
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java. %>
<%@taglib prefix="fmt" uri="http://java. %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>All users information</title>
</head>
<body>
<center>
<h2>All the Users' Information
    <table width="100%" border="1" cellspacing="2" cellpadding="3">
        <tr bgcolor="#CCCCCC" class="font">
            <td>
                <div align="center">ID</div>
            </td>
            <td>
                <div align="center">Username</div>
            </td>
            <td>
                <div align="center">Age</div>
            </td>
            <td>
                <div align="center">Address</div>
            </td>
            <td>
                <div align="center">Job</div>
            </td>
        </tr>
            <c:forEach var="user" items="${info}">
                <tr bgcolor="#FFFF88" class="font">
                    <td>
                        <div align="center"><c:out value="${user.id}" /></div>
                    </td>
                    <td>
                        <div align="center"><c:out value="${user.username}" /></div>
                    </td>
                    <td>
                        <div align="center"><c:out value="${user.age}" /></div>
                    </td>
                    <td>
                        <div align="center"><c:out value="${user.address}" /></div>
                    </td>
                    <td>
                        <div align="center"><c:out value="${user.job}" /></div>
                    </td>
                </tr>
            </c:forEach>
    </table>
    <br/>
    <a href="index.jsp">Back to Index</a>
</center>
</body>
</html>
我以前写的一个页面,可以参考下。注意那些该包括的头文件都写了没。
#12
zhangjipeng2008-01-03 13:34
你“info”数据在前个页面是怎么传过来的?是request.setAttribute还是其他的?
#13
hwoarangzk2008-01-03 14:04
session.setAttribute("info", list);
1