2 wang472442236 wang472442236 于 2013.09.02 17:30 提问

我是新手,想在jsp页面显示数据库表单 ,求帮助。。

这是action

package action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import DAO.CustomerInfo;
import ADO.Customer;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

public class customerList extends ActionSupport{

private static final long serialVersionUID = 1L;
/**
 * 
 */
private static List<Customer> customer1;
private CustomerInfo customerinfo;



public CustomerInfo getCustomerinfo() {
    return customerinfo;
}

public void setCustomerinfo(CustomerInfo customerinfo) {
    this.customerinfo = customerinfo;
}

public List<Customer> getCustomer() {
    return customer1;
}

public void setCustomer(List<Customer> customer) {
    customerList.customer1 = customer;
}

public String execute() throws Exception{
       HttpServletRequest request=ServletActionContext.getRequest();
       customer1= CustomerInfo.querycustomer();
       request.setAttribute("customer",customer1);
       return SUCCESS;


}

public static void main(String[] args) {

    System.out.println(CustomerInfo.querycustomer()); 

}

}

这是操作方法

public static List querycustomer()
{
List list = new ArrayList();
Customer customer;

     Configuration cfg = new Configuration().configure();    
     SessionFactory sf = cfg.buildSessionFactory();
     Session session = sf.openSession();
     Transaction tx = session.beginTransaction();
     Query query = session.createQuery("from Customer");    
     List<?> list1 = query.list();  // 序列化函数
     Iterator<?> it = list1.iterator(); //迭代
     while (it.hasNext())
     { 
         customer = (Customer)it.next();
         //System.out.println(customer.getUsername() + "  " + customer.getPassword()+ ""  ); 
     }

     tx.commit();
     session.close();
     sf.close();
     return list;
   }

这是po
package ADO;

public class Customer {
private String id;
private String username;
private String password;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

这是jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@page import="DAO.CustomerInfo"%>
<%@page import="java.util.List"%>
<%@page import="ADO.Customer"%>
<%@page import="java.util.ArrayList"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <title>My JSP 'welcome.jsp' starting page</title>

</head>

<body>


<%=request.getAttribute("customer") %>

            </ul>
      </s:iterator>
        </table>
    </s:form>
</body>

1个回答

wangyaninglm
wangyaninglm   Ds   Rxr 2015.01.18 22:39
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!