我的行动课是: -
package com.action;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.*;
import org.apache.struts2.rest.DefaultHttpHeaders;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage(value="default")
@Namespace("/")
@ResultPath(value="/")
public class noOfUsers extends ActionSupport {
private static final long serialVersionUID = 1L;
@Action(value="usersn",results={
@Result(name="create",type="tiles",location="users")
})
public static DefaultHttpHeaders create(){
EntityManagerFactory emf=Persistence.createEntityManagerFactory("tujpa");
EntityManager em=emf.createEntityManager();
EntityTransaction entr=em.getTransaction();
entr.begin();
Query query=em.createQuery("SELECT U.firstname from User U");
List <User> list = query.getResultList();
System.out.println("password");
Iterator iterator = list.iterator();
System.out.println("password1");
while(iterator.hasNext()){
String empFirstName = (String)iterator.next();
System.out.print("Emp Name:"+empFirstName );
System.out.println("password2");
}
entr.commit();
em.close();
return new DefaultHttpHeaders("create");
}
}
我想在我的jsp页面上显示我的结果列表, 所以请指导。 提前谢谢。
答案 0 :(得分:1)
好的,我可以看到为什么没有答案...你已经把一些JPA代码打印到终端然后粘贴到你从互联网上找到的S2类?
请从简单的“Hello World”应用程序开始:http://struts.apache.org/2.2.1.1/docs/getting-started.html
当它运行时,有很多使用迭代器标记的例子。此标记位于JSP上,并记录在上面列出的站点上。
然后你的行动代码会变成(快速黑客工作):
public class UserList extends ActionSupport {
List <User> list;
public String action(){
EntityManagerFactory emf=Persistence.createEntityManagerFactory("tujpa");
EntityManager em=emf.createEntityManager();
EntityTransaction entr=em.getTransaction();
entr.begin();
Query query=em.createQuery("SELECT U.firstname from User U");
list = query.getResultList();
em.close();
return SUCCESS;
}
}
现在你应该研究一些DI框架...... S2具有良好的Spring集成,将EntityManagerFactory / EntityManager处理移动到Spring会使上面的内容更加清晰。类似的东西:
public String action(){
list = em.createQuery("SELECT U.firstname from User U").getResultList();
return SUCCESS;
}