action:
public class caraction extends ActionSupport{
private List<Car> list=new ArrayList();
private List<Cartyper> ctlist=new ArrayList();
private carbiz cbiz;
private Car car;
private Cartyper ct;
private int y;
public String carall(){
list=cbiz.carall();
ActionContext.getContext().getSession().put("carall", list);
y=1;
return "ss";
}
public String de(){
if(cbiz.decar(car.getId())==0);
return "su";
}
public String ct(){
ctlist=cbiz.ct();
ActionContext.getContext().getSession().put("ctall", ctlist);
return "ss";
}
public String upp(){
if(cbiz.up(car)==0);
return "ss";
}
public String up(){
System.out.println(car.getId()+"ggggg");
car=cbiz.car(car.getId());
System.out.println(car.getBrand()+car.getPrice());
ctlist=cbiz.ct();
//car.setBrand(new String(car.getBrand().getBytes("iso-8859-1"),"utf-8"));
ActionContext.getContext().getSession().put("ctall", ctlist);
return "ss";
}
public String ins(){
car.setBrand(car.getBrand());
car.setCartyper(new Cartyper(ct.getId()));
car.setPrice(car.getPrice());
if(cbiz.sa(car)==0);
return "ss";
}
public String fenye(){
int num=cbiz.getcount();
num=num%2==0?num/2:(num/2+1);
if(y==0){
y=1;
}else if(y>num){
y=num;
}
list=cbiz.queryForPage(y, 2);
ActionContext.getContext().getSession().put("carall", list);
return "ss";
}
public List<Car> getList() {
return list;
}
public void setList(List<Car> list) {
this.list = list;
}
public carbiz getCbiz() {
return cbiz;
}
public void setCbiz(carbiz cbiz) {
this.cbiz = cbiz;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public List<Cartyper> getCtlist() {
return ctlist;
}
public void setCtlist(List<Cartyper> ctlist) {
this.ctlist = ctlist;
}
public Cartyper getCt() {
return ct;
}
public void setCt(Cartyper ct) {
this.ct = ct;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
biz实现类:
@Transactional(propagation=Propagation.REQUIRED)
@Service("cbiz")
public class carbizimpl implements carbiz{
@Autowired
@Qualifier("cdao")
private cardao cd;
public cardao getCd() {
return cd;
}
public void setCd(cardao cd) {
this.cd = cd;
}
public List<Car> carall() {
// TODO Auto-generated method stub
return cd.carall();
}
public int decar(int id) {
// TODO Auto-generated method stub
return cd.decar(id);
}
public List<Cartyper> ct() {
// TODO Auto-generated method stub
return cd.ct();
}
public int sa(Car c) {
// TODO Auto-generated method stub
return cd.sa(c);
}
public List<Car> queryForPage(int offset, int length) {
// TODO Auto-generated method stub
return cd.queryForPage(offset, length);
}
public int getcount() {
// TODO Auto-generated method stub
return cd.getcount();
}
public Car car(int id) {
// TODO Auto-generated method stub
return cd.car(id);
}
public int up(Car c) {
// TODO Auto-generated method stub
return cd.up(c);
}
dao实现类:
@Repository("cdao")
public class cardaoimpl extends HibernateDaoSupport implements cardao{
@Resource(name = "sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
public List<Car> carall() {
HibernateTemplate ht=this.getHibernateTemplate();
return ht.find("from Car");
}
public int decar(int id) {
int i=0;
HibernateTemplate ht=this.getHibernateTemplate();
Car car=ht.load(Car.class, id);
try {
ht.delete(car);
} catch (DataAccessException e) {
// TODO Auto-generated catch block
i=1;
}
return i;
}
public List<Cartyper> ct() {
HibernateTemplate ht=this.getHibernateTemplate();
return ht.find("from Cartyper");
}
public int sa(Car c) {
int j=0;
// TODO Auto-generated method stub
try {
this.getHibernateTemplate().save(c);
} catch (DataAccessException e) {
j=1;
}
return j;
}
public List<Car> queryForPage(int offset, int length) {
Session session=this.getHibernateTemplate().getSessionFactory().openSession();
Query q = session.createQuery("from Car");
q.setFirstResult((offset-1)*length);
q.setMaxResults(length);
return q.list();
}
public int getcount() {
Session session=this.getHibernateTemplate().getSessionFactory().openSession();
Query q = session.createQuery("select count(*) from Car");
return Integer.parseInt(q.list().get(0).toString());
}
public Car car(int id) {
HibernateTemplate ht=this.getHibernateTemplate();
return (Car) ht.find("from Car where id=?",id).get(0);
}
public int up(Car c) {
int i=0;
HibernateTemplate ht=this.getHibernateTemplate();
Car cc=(Car) ht.find("from Car where id=?",c.getId()).get(0);
try {
cc.setBrand(c.getBrand());
cc.setPrice(c.getPrice());
cc.setCartyper(c.getCartyper());
} catch (Exception e) {
i=1;
}
return i;
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<context:component-scan base-package="biz,dao"></context:component-scan>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 配置事务管理 -->
<bean id="tx"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 支持事务的注解标签 -->
<tx:annotation-driven transaction-manager="tx" />
</beans>
struts:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encodeing" value="utf-8"/>
<package name="default" namespace="/" extends="struts-default">
<action name="carall" class="action.caraction" method="carall">
<result name="ss" >index.jsp</result>
</action>
<action name="de" class="action.caraction" method="de">
<result name="su" type="redirectAction">carall</result>
</action>
<action name="ins" class="action.caraction" method="ins">
<result name="ss">insert.jsp</result>
</action>
<action name="ct" class="action.caraction" method="ct">
<result name="ss" type="redirectAction">carall</result>
</action>
<action name="fenye" class="action.caraction" method="fenye">
<result name="ss" >index.jsp</result>
</action>
<action name="up" class="action.caraction" method="up">
<result name="ss" >up.jsp</result>
</action>
<action name="upp" class="action.caraction" method="upp">
<result name="ss" type="redirectAction">carall</result>
</action>
</package>
</struts>
webxml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>