SSH(七)新的開始
在完成了ssh框架搭建的基礎上,我嘗試著去了解更多。新一階段還是一些簡單的增刪改查,只是提高自己的熟練度。
這一片我要創建一個登錄頁面,並查詢數據庫完成登錄。
一、創建實體:
1、1新建職員實體employee:
package com.ssh.entity; import java.util.Date; public class Employee { private int employee_id; private String username; private String password; private String sex; private String positioin; private int phone; private Date birthday; //所屬部門 private Department department; public int getEmployee_id() { return employee_id; } public void setEmployee_id(int employeeId) { employee_id = employeeId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPositioin() { return positioin; } public void setPositioin(String positioin) { this.positioin = positioin; } public int getPhone() { return phone; } public void setPhone(int phone) { this.phone = phone; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } }
1、2創建部門實體department:
package com.ssh.entity; import java.util.HashSet; import java.util.Set; public class Department { private int department_id; private String department_name; private String department_parent_id; //部門員工集合(hibernate有單向關聯,這裏用雙向關聯) private Set<Employee> employees = new HashSet<Employee>(); public int getDepartment_id() { return department_id; } public void setDepartment_id(int departmentId) { department_id = departmentId; } public String getDepartment_name() { return department_name; } public void setDepartment_name(String departmentName) { department_name = departmentName; } public String getDepartment_parent_id() { return department_parent_id; } public void setDepartment_parent_id(String departmentParentId) { department_parent_id = departmentParentId; } public Set<Employee> getEmployees() { return employees; } public void setEmployees(Set<Employee> employees) { this.employees = employees; } }
二、實體映射文件:
2、1employee的映射文件employee.hbm.xml:
(註意職員表和部門表的對應關系多對一)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.ssh.entity.Employee" table="employee"> <id name="employee_id" column="employee_id"> <generator class="native"></generator> </id> <property name="username" column="username" length="20"></property> <property name="password" column="password" length="20"></property> <property name="sex" column="sex" length="2"></property> <property name="positioin" column="positioin" length="20"></property> <property name="phone" column="phone" length="20"></property> <property name="birthday" column="birthday" ></property> <!-- 配置關聯關系,員工對部門是多對一,這裏生成的也是數據庫表的外鍵。 --> <many-to-one name="department" class="com.ssh.entity.Department" column="depart_id"/> </class> </hibernate-mapping>
2、2department實體映射文件:department.hmb.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.ssh.entity.Department" table="department"> <id name ="department_id" column="department_id"> <generator class="native"></generator> </id> <property name="department_name" column="department_name" length="20"></property> <property name="department_parent_id" column="department_parent_id" length="50"></property> <!-- 配置關聯關系 --> <set name="employees"> <key column="depart_id"></key> <one-to-many class="com.ssh.entity.Employee"/> </set> </class> </hibernate-mapping>
3、3在applicationcontext.xml→sessionFactory→mappingResources引入hibernate映射文件:
<property name="mappingResources"> <list> <!-- 映射文件全路徑 --> <value>com/ssh/entity/product.hbm.xml</value> <value>com/ssh/entity/Department.hbm.xml</value> <value>com/ssh/entity/Employee.hbm.xml</value> </list> </property>
運行項目,讓ssh為你在數據庫去創建你要的employee、department表。
三、新建登錄頁面
就在index.xml基礎上修改吧。
這裏有一個struts2的標簽可以學習下:<h1><s:actionerror/></h1>他可以將後臺的錯誤提示信息自動顯示。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!-- struts2 標簽庫 --> <[email protected] uri ="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>登錄頁面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div align="center"> <h1><s:actionerror/></h1> <h1>歡迎</h1> <s:form action="door_login" method="post" namespace="/" theme="simple"> <table border="1" width="400"> <tr> <td>用戶名:</td> <td><s:textfield name="username"/></td> </tr> <tr> <td>密 碼:</td> <td><s:textfield name="password"/></td> </tr> <tr> <!-- colspan: 合並單元格--> <td align="center" colspan="2"><input type="submit" value="登錄"> </td> </tr> </table> </s:form> </div> </body> </html>
四、控制層、業務層、持久層登錄方法的實現:
employeeAction:使用模型驅動ModelDriven接收前端數據
這裏面有一個addActionError的方法,就是將錯誤提示信息返回給前端頁面。
package com.ssh.action; import java.util.Date; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.ssh.entity.Employee; import com.ssh.service.employeeService; public class employeeAction extends ActionSupport implements ModelDriven<Employee> { private Employee employee = new Employee(); //使用模型驅動接收前段頁面數據,並將獲取數據封裝到employee對象。 public Employee getModel() { // TODO Auto-generated method stub return employee; } //註入業務層類 private employeeService employeeService; public void setEmployeeService(employeeService employeeService) { this.employeeService = employeeService; } public String login(){ Employee existEmployee = employeeService.login(employee); Date date = new Date(); if (existEmployee != null) { System.out.println("acction"+existEmployee); //登陸成功,提示登錄成功!把登錄信息存入session this.addActionMessage(existEmployee.getUsername()+"登錄成功!"+"\t"+date); ActionContext.getContext().getSession().put("existEmployee", employee); return SUCCESS; }else { //登錄失敗,提示錯誤信息,返回登錄界面。 this.addActionError("用戶名或密碼錯誤!"); return INPUT; } //return NONE; } }
employeeService:
public interface employeeService { Employee login(Employee employee); }
實現類employeeServiceImpl:
public class employeeServiceImpl implements employeeService { private employeeDao employeeDao; //註入dao public void setEmployeeDao(employeeDao employeeDao) { this.employeeDao = employeeDao; } public Employee login(Employee employee) { Employee existEmployee = employeeDao.findUsernameAndPassword(employee); return existEmployee; } }
employeeDao:
public interface employeeDao { Employee findUsernameAndPassword(Employee employee); }
實現類employeeDaoImpl:註意繼承hibernate模板
public class employeeDaoImpl extends HibernateDaoSupport implements employeeDao { public Employee findUsernameAndPassword(Employee employee) { String hql ="from Employee where username = ? and password = ?"; List<Employee> list =this.getHibernateTemplate().find(hql, employee.getUsername(),employee.getPassword()); if (!list.isEmpty()) { System.out.println("dao:"+list.get(0)); return list.get(0); } return null; } }
五、配置applicationcontext.xml以及struts.xml:
5、1控制層、業務層、持久層的註入配置:
<bean id="employeeAction" class="com.ssh.action.employeeAction" scope="prototype"> <!-- 需要手動註入service --> <property name="employeeService" ref="employeeService"></property> </bean> <!-- 配置業務層的類 --> <bean id="employeeService" class="com.ssh.service.impl.employeeServiceImpl"> <property name="employeeDao" ref="employeeDao"></property> </bean> <!-- 配置dao層:註入hibernate模板 --> <bean id="employeeDao" class="com.ssh.dao.impl.employeeDaoImpl"> <!-- ref 值與sessionFactory bean id保持一致 --> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
5.2struts.xml的action配置:
註意這裏配置的兩個返回結果標簽result,成功的跳轉addproduct。jsp,失敗的返回登錄頁。
<?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="default" namespace="/" extends="struts-default"> <action name="door_*" class="employeeAction" method="{1}"> <result name="success">/addproduct.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>
六、運行項目
成功跳轉:
SSH(七)新的開始