1)终于将Struts与Hibernate学习完了,现在就同时在一个web项目中完成一个简单小案例的开发!
2)在前面博客中已经介绍了怎样导入项目必须的jar包及环境的配置,就不再赘述,直接进入主题吧!
a)Dept.java(限制篇幅,setter和getter方法省略,下面也是一样)
package com.bighuan.entity; import java.util.HashSet; import java.util.Set; /** * 部门表对应的实体类 * @author bighuan * */ public class Dept { private int deptId; private String deptName; // 【一对多】 部门对应的多个员工 private Set<Employee> emps = new HashSet<Employee>(); public Dept() { } public Dept(int deptId, String deptName) { this.deptId = deptId; this.deptName = deptName; } } b)Dept.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.bighuan.entity" auto-import="true"> <class name="Dept" table="t_dept"> <id name="deptId"> <generator class="native"></generator> </id> <property name="deptName" length="20"></property> <set name="emps"> <key column="dept_id"></key> <one-to-many class="Employee" /> </set> </class> </hibernate-mapping> c)Employee.java package com.bighuan.entity; /** * 员工表对应的实体类 * @author bighuan * */ public class Employee { private int empId; private String empName; private double salary; // 【多对一】员工与部门 private Dept dept; public Employee(){ } } d)Employee.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.bighuan.entity"> <class name="Employee" table="t_employee"> <id name="empId"> <generator class="native"></generator> </id> <property name="empName" length="20"></property> <property name="salary" type="double"></property> <many-to-one name="dept" column="dept_id" class="Dept"></many-to-one> </class> </hibernate-mapping>
注意:通过线程方式获取session必须要在hibernate.cfg.xml中配置
Action中主要就是通过主键查询一条数据,保存到request域中
package com.bighuan.action; import com.bighuan.entity.Dept; import com.bighuan.service.DeptService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class DeptAction extends ActionSupport{ private DeptService service=new DeptService(); @Override public String execute() throws Exception { //主键查询,模拟数据 Dept dept = service.findById(11); //保存到request域中 ActionContext.getContext().getContextMap().put("dept", dept); return SUCCESS; } }a)配置拦截器,action返回"success"就跳转到index.jsp,并显示数据!
b)代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <package name="dept" extends="struts-default"> <!-- 拦截器配置 --> <interceptors> <interceptor name="sessionInterceptor" class="com.bighuan.interceptor.SessionInterceptor"></interceptor> <interceptor-stack name="myStack"> <!-- 默认拦截器栈 --> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="sessionInterceptor"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 使用拦截器 --> <default-interceptor-ref name="myStack"></default-interceptor-ref> <!-- 配置action --> <action name="show" class="com.bighuan.action.DeptAction" method="execute"> <!-- type默认就是dispatcher --> <result name="success" type="dispatcher">/index.jsp</result> </action> </package> <!-- Add packages here --> </struts> c)为了使用struts的核心功能,在web.xml中记得配置strtus的核心过滤器. <!-- 引入struts2核心过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>a)引入struts标签:<%@ taglib uri="/struts-tags" prefix="s" %>
b)显示数据:
<body> <h3>部门:<s:property value="#request.dept.deptName"></s:property></h3> <!-- 部门下的员工:(懒加载数据) --> <table border="1" align="center" cellpadding="5" cellspacing="0" width="55%"> <tr> <th>员工编号</th> <th>员工姓名</th> <th>薪水</th> </tr> <s:if test="#request.dept.emps != null"> <s:iterator var="emp" value="#request.dept.emps"> <tr> <td><s:property value="#emp.empId" /></td> <td><s:property value="#emp.empName" /></td> <td><s:property value="#emp.salary" /></td> </tr> </s:iterator> </s:if> <s:else> <tr><td colspan="3">没有员工信息!</td></tr> </s:else> </table> </body>c)访问action,效果图: