springmvc和hibernate整合
1.commens
1 package com.zhidi.commens; 2 3 import java.util.List; 4 5 /** 6 * 分页实体类 7 * 8 * @author Administrator 9 * @param <T> 10 * 11 */ 12 public class Pager<T> { 13 14 private Long currentPage = 1L; // 当前页 15 private Long pageSize = 10L;// 每页的记录条数 16 private Long totalSize = 0L;// 总记录数 17 private Long totalPage = 0L;// 总页数 18 private List<T> data;// 数据 19 20 public List<T> getData() { 21 return data; 22 } 23 24 public void setData(List<T> data) { 25 this.data = data; 26 } 27 28 public Long getCurrentPage() { 29 if (currentPage <= 0) { 30 currentPage = 1L; 31 } 32 if (currentPage > totalPage) { 33 currentPage = totalPage; 34 } 35 return currentPage; 36 } 37 38 public void setCurrentPage(Long currentPage) { 39 this.currentPage = currentPage; 40 } 41 42 public Long getPageSize() { 43 return pageSize; 44 } 45 46 public void setPageSize(Long pageSize) { 47 this.pageSize = pageSize; 48 } 49 50 public Long getTotalSize() { 51 52 return totalSize; 53 } 54 55 public void setTotalSize(Long totalSize) { 56 this.totalSize = totalSize; 57 // 设置总记录数时,获得总页数 58 getTotalPage(); 59 } 60 61 public Long getTotalPage() { 62 if (pageSize != 0) { 63 totalPage = (totalSize + pageSize - 1) / pageSize; 64 } 65 return totalPage; 66 } 67 68 public void setTotalPage(Long totalPage) { 69 this.totalPage = totalPage; 70 } 71 72 }
2.controller
1 package com.zhidi.controller; 2 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Controller; 9 import org.springframework.ui.Model; 10 import org.springframework.web.bind.annotation.PathVariable; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.bind.annotation.RequestMethod; 13 import org.springframework.web.bind.annotation.ResponseBody; 14 15 import com.zhidi.commens.Pager; 16 import com.zhidi.entity.Dept; 17 import com.zhidi.service.IDeptService1; 18 19 @Controller 20 public class Dept1Controller { 21 22 // 注入IDeptService1 23 @Autowired 24 private IDeptService1 deptService1; 25 26 @ResponseBody 27 @RequestMapping("/getAll") 28 public List<Dept> getAll() { 29 return deptService1.getAll(); 30 } 31 32 /** 33 * 分页查询 34 * 35 * @param pager 36 * @param model 37 * @return 38 */ 39 @RequestMapping("/getListPage") 40 public String getListPage(Pager<Dept> pager, Model model) { 41 pager = deptService1.getListByPage(pager); 42 model.addAttribute("pager", pager); 43 return "table"; 44 } 45 46 @RequestMapping(value="/{deptno}",method=RequestMethod.GET) 47 public String getInfo(Model model, @PathVariable("deptno") Integer deptno) 48 { 49 //根据主键查出对应的实体 50 Dept dept = deptService1.get(deptno); 51 model.addAttribute("dept", dept); 52 return "edit"; 53 } 54 55 @RequestMapping("/edit") 56 public String edit(Dept dept) 57 { 58 deptService1.update(dept); 59 return "redirect:getListPage.do"; 60 } 61 62 @ResponseBody 63 @RequestMapping(value="/{deptno}",method=RequestMethod.DELETE) 64 public Map<String, Object> delete( @PathVariable("deptno") Integer deptno) 65 { 66 Map<String, Object> map = new HashMap<>(); 67 deptService1.delete(deptno); 68 map.put("success", true); 69 map.put("msg", "删除成功"); 70 return map; 71 } 72 }
1 package com.zhidi.controller; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.ResponseBody; 10 11 import com.zhidi.entity.Dept; 12 import com.zhidi.service.IDeptService; 13 14 @Controller 15 public class DeptController { 16 17 // 把IDeptService注入进来 18 @Autowired 19 private IDeptService deptService; 20 21 @ResponseBody 22 @RequestMapping("/list") 23 public List<Dept> list() { 24 List<Dept> list = deptService.getAll(); 25 return list; 26 } 27 28 @RequestMapping("/add") 29 public String add(Dept dept, Model model) { 30 dept.setDname("张三1"); 31 dept.setLoc("武汉1"); 32 dept.setDeptno(3); 33 deptService.add(dept); 34 model.addAttribute("dept", dept); 35 return "test"; 36 } 37 38 }
1 package com.zhidi.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 public class TestController { 8 9 @RequestMapping("/test") 10 public String test() 11 { 12 return "test"; 13 } 14 }
3.dao
1 package com.zhidi.dao; 2 3 import java.util.List; 4 5 /* 6 * 基类接口 7 */ 8 public interface IBaseDao<T, PK> { 9 10 /** 11 * 增加数据 12 * 13 * @param entity 14 */ 15 void add(T entity); 16 17 /** 18 * 删除数据 19 * 20 * @param id 21 */ 22 void delete(PK id); 23 24 /** 25 * 更新数据 26 * 27 * @param entity 28 */ 29 void update(T entity); 30 31 /** 32 * 获取数据 33 * 34 * @param id 35 * @return 36 */ 37 T get(PK id); 38 39 /** 40 * 获取所有数据 41 * 42 * @return 43 */ 44 List<T> getAll(); 45 46 /** 47 * 获取总条数 48 * 49 * @return 50 */ 51 Long count(); 52 }
1 package com.zhidi.dao; 2 3 import java.util.List; 4 5 import com.zhidi.entity.Dept; 6 7 public interface IDeptDao { 8 9 10 void add(Dept dept); 11 void delete(Integer deptno); 12 void update(Dept dept); 13 Dept get(Integer deptno); 14 List<Dept> getAll(); 15 16 }
1 package com.zhidi.dao; 2 3 import java.util.List; 4 5 import com.zhidi.entity.Dept; 6 7 public interface IDeptDao1 extends IBaseDao<Dept, Integer> { 8 9 /** 10 * 获取所有的数据 11 */ 12 List<Dept> getAll(); 13 14 /** 15 * 分页获取员工的信息 16 * @param currentPage 17 * @param pageSize 18 * @return 19 */ 20 List<Dept> getListByPage( Long currentPage,Long pageSize); 21 22 /** 23 * 获得总记录数 24 */ 25 Long count(); 26 27 /** 28 * 根据主键查出实体 29 */ 30 Dept get(Integer deptno); 31 32 /** 33 * 修改数据 34 */ 35 void update(Dept dept); 36 37 /** 38 * 删除数据 39 */ 40 void delete(Integer deptno); 41 }
impl:
1 package com.zhidi.dao.impl; 2 3 import java.io.Serializable; 4 import java.lang.reflect.ParameterizedType; 5 import java.lang.reflect.Type; 6 import java.util.List; 7 8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.stereotype.Repository; 12 13 import com.zhidi.dao.IBaseDao; 14 15 @Repository 16 public class BaseDaoImpl<T, PK extends Serializable> implements IBaseDao<T, PK> { 17 18 // 注入SessionFactory 19 @Autowired 20 private SessionFactory sessionFactory; 21 22 private Class<T> entityClass; 23 24 // 提供一个无参构造方法 25 public BaseDaoImpl() { 26 Class cls = getClass(); 27 Type type = cls.getGenericSuperclass(); 28 if (type instanceof ParameterizedType) { 29 Type[] types = ((ParameterizedType) type).getActualTypeArguments(); 30 entityClass = (Class<T>) types[0]; 31 } 32 33 } 34 35 // 获取session的方法 36 protected Session getSession() { 37 return sessionFactory.getCurrentSession(); 38 } 39 40 @Override 41 public void add(T entity) { 42 getSession().save(entity); 43 } 44 45 @Override 46 public void delete(PK id) { 47 T t = get(id); 48 getSession().delete(t); 49 } 50 51 @Override 52 public void update(T entity) { 53 getSession().update(entity); 54 } 55 56 @Override 57 public T get(PK id) { 58 return (T)getSession().get(entityClass, id); 59 } 60 61 @SuppressWarnings("unchecked") 62 @Override 63 public List<T> getAll() { 64 List<T> list=(List<T>)getSession().createQuery("from "+entityClass.getName()).list(); 65 return list; 66 } 67 68 @Override 69 public Long count() { 70 return (Long)getSession().createQuery("select count(*) from "+entityClass.getName()).uniqueResult(); 71 } 72 73 }
1 package com.zhidi.dao.impl; 2 3 import java.util.List; 4 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Repository; 9 10 import com.zhidi.dao.IDeptDao; 11 import com.zhidi.entity.Dept; 12 13 @Repository 14 public class DeptDaoImpl implements IDeptDao{ 15 16 //注入SessionFactory 17 @Autowired 18 private SessionFactory sessionFactory; 19 20 public Session getSession() 21 { 22 return sessionFactory.getCurrentSession(); 23 } 24 25 @Override 26 public void add(Dept dept) { 27 getSession().save(dept); 28 } 29 30 @Override 31 public void delete(Integer deptno) { 32 Dept dept = get(deptno); 33 getSession().delete(dept); 34 } 35 36 @Override 37 public void update(Dept dept) { 38 39 } 40 41 @Override 42 public Dept get(Integer deptno) { 43 44 Dept dept = (Dept)getSession().get(Dept.class, deptno); 45 return dept; 46 } 47 48 49 @Override 50 public List<Dept> getAll() { 51 52 return (List<Dept>) getSession().createQuery("from Dept").list(); 53 } 54 55 }
1 package com.zhidi.dao.impl; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.springframework.stereotype.Repository; 7 8 import com.zhidi.dao.IDeptDao1; 9 import com.zhidi.entity.Dept; 10 11 @Repository 12 public class DeptDaoImpl1 extends BaseDaoImpl<Dept, Integer> implements IDeptDao1 { 13 14 @Override 15 public List<Dept> getAll() { 16 return super.getAll(); 17 } 18 19 @Override 20 public List<Dept> getListByPage(Long currentPage, Long pageSize) { 21 Query query = getSession().createQuery("from Dept"); 22 // 分页设置 23 query.setFirstResult((currentPage.intValue() - 1) * pageSize.intValue());// 每页开始的记录 24 query.setMaxResults(pageSize.intValue());// 每页最大的记录数 25 List<Dept> list = query.list(); 26 return list; 27 } 28 29 @Override 30 public Long count() { 31 return super.count(); 32 } 33 34 @Override 35 public Dept get(Integer id) { 36 return super.get(id); 37 } 38 39 @Override 40 public void update(Dept entity) { 41 super.update(entity); 42 } 43 44 @Override 45 public void delete(Integer id) { 46 super.delete(id); 47 } 48 }
4.entity
1 package com.zhidi.entity; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.GenerationType; 6 import javax.persistence.Id; 7 import javax.persistence.Table; 8 9 @Entity 10 @Table(name="dept") 11 public class Dept { 12 13 @Id 14 @GeneratedValue(strategy=GenerationType.AUTO) 15 private Integer deptno; 16 private String dname; 17 private String loc; 18 19 public Integer getDeptno() { 20 return deptno; 21 } 22 23 public void setDeptno(Integer deptno) { 24 this.deptno = deptno; 25 } 26 27 public String getDname() { 28 return dname; 29 } 30 31 public void setDname(String dname) { 32 this.dname = dname; 33 } 34 35 public String getLoc() { 36 return loc; 37 } 38 39 public void setLoc(String loc) { 40 this.loc = loc; 41 } 42 43 }
1 package com.zhidi.entity; 2 3 import java.util.Date; 4 5 import javax.persistence.Entity; 6 import javax.persistence.FetchType; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.GenerationType; 9 import javax.persistence.Id; 10 import javax.persistence.JoinColumn; 11 import javax.persistence.ManyToOne; 12 import javax.persistence.Table; 13 import javax.persistence.Temporal; 14 import javax.persistence.TemporalType; 15 16 import org.hibernate.annotations.Cascade; 17 import org.hibernate.annotations.CascadeType; 18 19 /** 20 * 多对一关单向关联关系中的->多方 21 * 22 * @author lx 23 * 24 */ 25 @Entity 26 @Table(name="emp") 27 public class Emp { 28 29 @Id 30 @GeneratedValue(strategy = GenerationType.AUTO) 31 private Integer empno; 32 private String ename; 33 private String job; 34 private Integer mgr; 35 private Double sal; 36 @Temporal(TemporalType.DATE) 37 private Date hiredate; 38 39 // 提供类型为Dept的成员变量 40 @ManyToOne(fetch=FetchType.LAZY) 41 @JoinColumn(name="deptno") 42 @Cascade({CascadeType.SAVE_UPDATE}) 43 private Dept dept; 44 45 public Integer getEmpno() { 46 return empno; 47 } 48 49 public void setEmpno(Integer empno) { 50 this.empno = empno; 51 } 52 53 public String getEname() { 54 return ename; 55 } 56 57 public void setEname(String ename) { 58 this.ename = ename; 59 } 60 61 public String getJob() { 62 return job; 63 } 64 65 public void setJob(String job) { 66 this.job = job; 67 } 68 69 public Integer getMgr() { 70 return mgr; 71 } 72 73 public void setMgr(Integer mgr) { 74 this.mgr = mgr; 75 } 76 77 public Double getSal() { 78 return sal; 79 } 80 81 public void setSal(Double sal) { 82 this.sal = sal; 83 } 84 85 public Date getHiredate() { 86 return hiredate; 87 } 88 89 public void setHiredate(Date hiredate) { 90 this.hiredate = hiredate; 91 } 92 93 public Dept getDept() { 94 return dept; 95 } 96 97 public void setDept(Dept dept) { 98 this.dept = dept; 99 } 100 101 }
5.service
1 package com.zhidi.service; 2 3 import java.util.List; 4 5 import com.zhidi.entity.Dept; 6 7 public interface IDeptService { 8 9 List<Dept> getAll(); 10 11 void add(Dept dept); 12 }
1 package com.zhidi.service; 2 3 import java.util.List; 4 5 import com.zhidi.commens.Pager; 6 import com.zhidi.entity.Dept; 7 8 public interface IDeptService1 { 9 10 List<Dept> getAll(); 11 12 Pager<Dept> getListByPage(Pager<Dept> pager); 13 14 Dept get(Integer deptno); 15 16 void update(Dept dept); 17 18 void delete(Integer deptno); 19 }
impl:
1 package com.zhidi.service.impl; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import com.zhidi.dao.IDeptDao; 9 import com.zhidi.entity.Dept; 10 import com.zhidi.service.IDeptService; 11 12 @Service 13 public class DeptServiceImpl implements IDeptService { 14 15 // 注入IDeptService 16 @Autowired 17 private IDeptDao deptDao; 18 19 @Override 20 public List<Dept> getAll() { 21 22 return deptDao.getAll(); 23 24 } 25 26 @Override 27 public void add(Dept dept) { 28 deptDao.add(dept); 29 } 30 31 32 33 }
1 package com.zhidi.service.impl; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import com.zhidi.commens.Pager; 9 import com.zhidi.dao.IDeptDao1; 10 import com.zhidi.entity.Dept; 11 import com.zhidi.service.IDeptService1; 12 13 @Service 14 public class IDeptServiceImpl1 implements IDeptService1 { 15 16 // 注入IDeptDao1 17 @Autowired 18 private IDeptDao1 iDeptDao1; 19 20 /** 21 * 得到所有的信息 22 */ 23 @Override 24 public List<Dept> getAll() { 25 return iDeptDao1.getAll(); 26 } 27 28 @Override 29 public Pager<Dept> getListByPage(Pager<Dept> pager) { 30 // 得到总条数 31 Long totalSize = iDeptDao1.count(); 32 pager.setTotalSize(totalSize); 33 // 查询分页记录 34 List<Dept> data = iDeptDao1.getListByPage(pager.getCurrentPage(), pager.getPageSize()); 35 pager.setData(data); 36 return pager; 37 } 38 39 @Override 40 public Dept get(Integer deptno) { 41 Dept dept = iDeptDao1.get(deptno); 42 return dept; 43 } 44 45 @Override 46 public void update(Dept dept) { 47 iDeptDao1.update(dept); 48 } 49 50 @Override 51 public void delete(Integer deptno) { 52 iDeptDao1.delete(deptno); 53 } 54 55 }
6.配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 7 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 8 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 9 10 <!-- 扫描需要Springmvc管理的JavaBean --> 11 <context:component-scan base-package="com.zhidi" /> 12 <!-- 读取jdbc.properties --> 13 <context:property-placeholder location="classpath:jdbc.properties" /> 14 <!-- 将c3p0交给Springmvc管理,配置连接数据库 --> 15 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 16 <property name="driverClass" value="${jdbc.driverClass}" /> 17 <property name="jdbcUrl" value="${jdbc.url}" /> 18 <property name="user" value="${jdbc.userName}"/> 19 <property name="password" value="${jdbc.password}" /> 20 <property name="initialPoolSize" value="3" /> 21 <property name="maxConnectionAge" value="28800" /> 22 <property name="maxIdleTime" value="21600" /> 23 <property name="maxPoolSize" value="10" /> 24 <property name="minPoolSize" value="1" /> 25 </bean> 26 27 <!-- 配置SessionFactory --> 28 <bean id="sessionFactory" 29 class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 30 <!-- 将c3p0注入到SessionFactory --> 31 <property name="dataSource" ref="dataSource" /> 32 <!-- 指定hibernate的配置信息 --> 33 <property name="hibernateProperties"> 34 <props> 35 <prop key="hibernate.show_sql">true</prop> 36 <prop key="hibernate.format_sql">true</prop> 37 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 38 </props> 39 </property> 40 <!-- 扫描实体类 --> 41 <property name="packagesToScan" value="com.zhidi.entity" /> 42 </bean> 43 44 <!-- 定义hibernate事务管理器 --> 45 <bean id="transactionManager" 46 class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 47 <!-- 将SessionFactory注入到事务管理器中 --> 48 <property name="sessionFactory" ref="sessionFactory" /> 49 </bean> 50 <!-- 配置事务通知 --> 51 <tx:advice id="tx" transaction-manager="transactionManager"> 52 <tx:attributes> 53 <tx:method name="add*" propagation="REQUIRED" /> 54 <tx:method name="save*" propagation="REQUIRED" /> 55 <tx:method name="delete*" propagation="REQUIRED" /> 56 <tx:method name="modeif*" propagation="REQUIRED" /> 57 <tx:method name="update*" propagation="REQUIRED" /> 58 <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> 59 <tx:method name="query*" propagation="SUPPORTS" read-only="true" /> 60 <tx:method name="search*" propagation="SUPPORTS" read-only="true" /> 61 <tx:method name="*" propagation="SUPPORTS" read-only="true" /> 62 </tx:attributes> 63 </tx:advice> 64 <!-- 配置切面 --> 65 <aop:config> 66 <aop:advisor advice-ref="tx" 67 pointcut="execution(* com.zhidi.service..*.*(..))" /> 68 </aop:config> 69 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 8 9 <!-- 扫描包 在此不能扫描其他如service会导致sessionFactory出错,因为在此页面没有配置sessionFactory,applicationContext.xml先运行的--> 10 <context:component-scan base-package="com.zhidi.controller" /> 11 <!-- 处理静态资源 --> 12 <mvc:default-servlet-handler/> 13 <!-- 开启Springmvc注解 --> 14 <mvc:annotation-driven /> 15 <!-- 配置视图解析器 --> 16 <bean 17 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 18 <!-- 前缀 --> 19 <property name="prefix" value="/WEB-INF/page/" /> 20 <!-- 后缀 --> 21 <property name="suffix" value=".jsp" /> 22 </bean> 23 </beans>
1 jdbc.driverClass=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/db_hibernate 3 jdbc.userName=root 4 jdbc.password=775297
1 log4j.rootLogger=info, ServerDailyRollingFile, stdout 2 3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p [%c] %m%n 6 7 log4j.category.org.springframework.beans.factory=DEBUG 8 log4j.logger.org.springframework.web.servlet=DEBUG 9 log4j.logger.org.hibernate.tool.hbm2ddl=debug 10 log4j.logger.org.hibernate.type=info 11 log4j.logger.org.hibernate=debug 12 #log4j.logger.org.apache.ibatis=DEBUG 13 log4j.logger.com.zhidi=DEBUG
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 id="WebApp_ID" version="3.0"> 6 7 <!-- --> 8 <!-- needed for ContextLoaderListener --> 9 <context-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:applicationContext.xml</param-value> 12 </context-param> 13 14 <!-- Bootstraps the root web application context before servlet initialization --> 15 <listener> 16 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 17 </listener> 18 19 <!-- 配置springmvc前端控制器 --> 20 <!-- The front controller of this Spring Web application, responsible for 21 handling all application requests --> 22 <servlet> 23 <servlet-name>springDispatcherServlet</servlet-name> 24 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 25 <init-param> 26 <param-name>contextConfigLocation</param-name> 27 <param-value>classpath:spring-mvc.xml</param-value> 28 </init-param> 29 <load-on-startup>1</load-on-startup> 30 </servlet> 31 32 <!-- Map all requests to the DispatcherServlet for handling --> 33 <servlet-mapping> 34 <servlet-name>springDispatcherServlet</servlet-name> 35 <url-pattern>*.do</url-pattern> 36 </servlet-mapping> 37 38 <!-- 配置过滤器 --> 39 <filter> 40 <filter-name>encodingFilter</filter-name> 41 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 42 <init-param> 43 <param-name>encoding</param-name> 44 <param-value>UTF-8</param-value> 45 </init-param> 46 </filter> 47 <filter-mapping> 48 <filter-name>encodingFilter</filter-name> 49 <url-pattern>*.do</url-pattern> 50 <url-pattern>*.jsp</url-pattern> 51 </filter-mapping> 52 53 54 </web-app>
7.前端页面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() 6 + path + "/";%> 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 12 <title>Insert title here</title> 13 </head> 14 <body> 15 <form action="edit.do" method="post"> 16 部门编号:<input type="text" name="deptno" value="${ dept.deptno}" readonly="readonly"/><br> 17 部门名称:<input type="text" name="dname" value="${ dept.dname}"/><br> 18 所在地址:<input type="text" name="loc" value="${ dept.loc}"/><br> 19 <input type="submit" value="提交"> 20 </form> 21 </body> 22 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 5 <% 6 String path = request.getContextPath(); 7 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() 8 + path + "/"; 9 %> 10 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 11 <html> 12 <head> 13 <base href="<%=basePath%>"> 14 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 15 <title>Insert title here</title> 16 </head> 17 <script type="text/javascript" src="static/jquery/jquery.min.js"></script> 18 <body> 19 <table border="1" cellspacing="0"> 20 <caption> 21 <h3>部门信息</h3> 22 </caption> 23 <tr> 24 <th>序号</th> 25 <th>部门名称</th> 26 <th>部门地址</th> 27 <th>操作</th> 28 29 </tr> 30 <c:forEach items="${pager.data}" var="dept" varStatus="s"> 31 <tr> 32 <td>${s.count}</td> 33 <td>${dept.dname}</td> 34 <td>${dept.loc}</td> 35 <td><a href="${dept.deptno}.do">修改</a>|<a href="${dept.deptno}.do" class="delete">删除</a></td> 36 </tr> 37 </c:forEach> 38 <tr align="center"> 39 <td colspan="4"> 40 <a href="getListPage.do?currentPage=1">首页</a> 41 <a href="getListPage.do?currentPage=${pager.currentPage-1 }">上一页</a> 42 <a href="getListPage.do?currentPage=${pager.currentPage+1 }">下一页</a> 43 <a href="getListPage.do?currentPage=${pager.totalPage }">尾页</a> 44 ${pager.currentPage}/${pager.totalPage} 45 </td> 46 </tr> 47 </table> 48 </body> 49 <script type="text/javascript"> 50 $(".delete").on("click",function(event){ 51 52 //禁止a标签的默认事件 53 event.preventDefault(); 54 var url = $(this).attr("href"); 55 //发送Ajax请求 56 $.ajax({ 57 url:url, 58 type:"DELETE", 59 data:{}, 60 dataType:"JSON", 61 success:function(result){ 62 alert(result.msg); 63 if (result.success) { 64 //删除成功重载页面 65 location.reload(); 66 } 67 } 68 }); 69 }); 70 71 </script> 72 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() 6 + path + "/";%> 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 12 <title>Insert title here</title> 13 </head> 14 <body> 15 测试成功!!! 16 ${dept.loc } 17 </body> 18 </html>
antlr-2.7.7.jar
aspectjrt.jar
aspectjtools.jar
aspectjweaver.jar
c3p0-0.9.2.1.jar
commons-fileupload-1.3.3.jar
commons-io-2.5.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
hibernate-c3p0-4.3.11.Final.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.11.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jackson-annotations-2.8.0.jar
jackson-core-2.8.9.jar
jackson-databind-2.8.9.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
jstl.jar
log4j-1.2.16.jar
mchange-commons-java-0.2.3.4.jar
mysql-connector-java-commercial-5.1.25-bin.jar
org.aspectj.matcher.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
spring-aop-4.3.8.RELEASE.jar
spring-aspects-4.3.8.RELEASE.jar
spring-beans-4.3.8.RELEASE.jar
spring-context-4.3.8.RELEASE.jar
spring-context-support-4.3.8.RELEASE.jar
spring-core-4.3.8.RELEASE.jar
spring-expression-4.3.8.RELEASE.jar
spring-jdbc-4.3.8.RELEASE.jar
spring-orm-4.3.8.RELEASE.jar
spring-tx-4.3.8.RELEASE.jar
spring-web-4.3.8.RELEASE.jar
spring-webmvc-4.3.8.RELEASE.jar
standard.jar