package com.sun; import java.util.List; import java.util.Map; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.denglu.domain.User; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport{ private Map request; public Configuration config; public SessionFactory sessionfactory; public Session session; public Transaction ts; private User user; public TestAction(){ config = new Configuration(); sessionfactory = config.configure("hibernate.cfg.xml").buildSessionFactory(); session = sessionfactory.openSession(); } public String List(){ //User uu = new User(); List uu = (List) session.createQuery("from User").list(); System.out.println("lalalal "); for(int i =0;i<uu.size();i++){ User user = (User) uu.get(i); System.out.println(user.getName() + user.getPwd()); } request = (Map) ActionContext.getContext().get("request"); request.put("user", uu); session.close(); return "sucess"; } public String Del(){ ts = session.beginTransaction(); User u=(User) session.load(User.class,4); session.delete(u); ts.commit(); return "sucess"; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="" extends="struts-default"> <action name="index" class="com.sun.TestAction" method="List"> <result name="sucess"> /WEB-INF/index.jsp </result> </action> <action name="del" class="com.sun.TestAction" method="Del"> <result name="sucess" type="redirect"> index </result> </action> </package> <!-- Add packages here --> </struts>
通过配置action中的result 元素的返回方式为重定向,返回是调用某个Action就能够实现
一般删除一条记录之后,都会想跳转会原来的显示记录的页面,就跟你在百度留言后,直接显示出你的留言一样,这里通过Struts2进行实现
时间: 2024-10-28 21:36:33