bean对象主要代码如下所示:
public class Student implements Serializable {
private int sid;
private String sname;
private String description;
}
dao层对象主要代码如下所示:
public class StudentDao extends HibernateDaoSupport implements IstudentDao {
public void addStudent(Student stu) {
this.getHibernateTemplate().save(stu);
}
public void deleteStudent(Serializable key) {
Student stu = this.getHibernateTemplate().get(Student.class, key);
if (stu != null) {
this.getHibernateTemplate().delete(stu);
}
}
public List<Student> getAll() {
List<Student> list = this.getHibernateTemplate().find("from Student");
return list;
}
public Student getByKey(Serializable key) {
return this.getHibernateTemplate().get(Student.class, key);
}
public void update(Student stu) {
this.getHibernateTemplate().update(stu);
}
}
Service层主要代码如下所示:
public class StudentService {
private IstudentDao studao;
public IstudentDao getStudao() {
return studao;
}
public void setStudao(IstudentDao studao) {
this.studao = studao;
}
public void addStudent(Student stu) {
studao.addStudent(stu);
}
public void deleteStudent(Serializable key) {
studao.deleteStudent(key);
}
public List<Student> getAll() {
List<Student> list = studao.getAll();
return list;
}
public Student getByKey(Serializable key) {
return studao.getByKey(key);
}
public void update(Student stu) {
studao.update(stu);
}
}
Control层代码如下所示:
@Controller
@RequestMapping("/student")
public class Controller {
private StudentService stuService;
public StudentService getStuService() {
return stuService;
}
@Resource(name = "stuService")
public void setStuService(StudentService stuService) {
this.stuService = stuService;
}
// /student/save.action
@RequestMapping("/save")
public String saveStudent(Student stu, HttpServletRequest req,
Map<String, Object> map) {
System.out.println(stu);
System.out.println(stuService);
stuService.addStudent(stu);
return "redirect:all.action";
}
@RequestMapping("/delete")
public String deleteStudent(int sid) {
System.out.println("deleteStudent" + sid);
stuService.deleteStudent(sid);
return "redirect:all.action";
}
@RequestMapping("/update")
public String updateStudent(Student stu) {
stuService.update(stu);
return "redirect:all.action";
}
@RequestMapping("/key")
public String getByKey(int sid, HttpServletRequest req) {
Student stu = stuService.getByKey(sid);
req.setAttribute("stu", stu);
return "showStu";
}
@RequestMapping("/all")
public String getAll(HttpServletRequest req) {
List<Student> list = stuService.getAll();
req.setAttribute("list", list);
return "showAll";
}
}
<!-- 防止session提前关闭处理 将Hibernate的Session生命延长,为了解决由于Hibernate延迟加载,Session过早关闭问题-->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>test</servlet-name>
<!-- 负责加载springmvc的配置文件 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 负责加载spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/db.xml,/WEB-INF/beans.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
springmvc配置文件中的主要配置代码:test-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.mvc"></context:component-scan>
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
bean对象配置文件中的主要配置代码:beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<import resource="db.xml" />
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.mvc"></context:component-scan>
<bean id="studao" class="com.mvc.dao.StudentDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="stuService" class="com.mvc.service.StudentService">
<property name="studao" ref="studao"></property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="tx" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="add*" />
<tx:method name="delete*" />
<tx:method name="update*" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.mvc.service.*.*(..))"
id="pt" />
<aop:advisor advice-ref="tx" pointcut-ref="pt" />
</aop:config>
</beans>
前台通过jstl标签与EL表达式进行提取即可