前提
ssh环境的搭建
java代码
1、继承requestAware接口
2、把集合作为一个元素封装在map里面
package cn.com.service;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import cn.com.bean.Posted;
@Repository(value = "queryPosted")
@Scope("prototype")
public class QueryPosted implements RequestAware{
//可以查询表中所有的数据并且显示在页面上
@Autowired
private SessionFactory sf;
private Map<String, Object> req;
public Map<String, Object> getReq() {
return req;
}
public void setReq(Map<String, Object> req) {
this.req = req;
}
@Transactional
public String query_all(){
//返回数据
Session session = sf.getCurrentSession();
String sql="from Posted";
Query query=session.createQuery(sql);
List<Posted> list=query.list();
//把数据返回给前台页面
req.put("list", list);
return "success";
}
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
req=arg0;
}
}
html页面
1、s标签
2、循环遍历
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title> 显示所有的发帖 </title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<!-- 显示所有的发帖 -->
<%String username=(String)session.getAttribute("user_name"); %>
<table>
<tr><td>种类</td><td>标题</td><td>提问人</td><td>回复数</td></tr>
<!-- 循环遍历 -->
<s:iterator value="#request.list" var="post">
<tr>
<td>${post.post_kinds}</td>
<td><a href="details.kk?post_id=${post.post_id}">${post.post_title}</a></td>
<td>${post.post_byname}</td>
<td>${post.answer_num}</td>
</tr>
</s:iterator>
</table>
</body>
</html>