jdk6+myeclipse8.5+tomcat6+oracle+spring2.0+hibernate3+struts2好了,开始我们的第一个ssh2之旅吧。
首先先分析一下我们的第一个ssh2项目的需求,简单的说就是有一张表单,让你填写用户名和密码,提交后存入数据库。
就这么简单,呵呵。
第一步:。
我们首先新建一张mysql数据表。
Sql如下:Create table t_employee(Id integer not null primary key,Name varchar(20),Age varchar(20),Sex varchar(20),Tel varchar(20),Address varchar(20))数据创建好后结构如下:第二步打开myeclipse,新建一个web项目,命名为ssh2-2, java ee规范我们选择5,如图第三步务必小心的一步导入ssh2的各个jar,步骤如下:选中当前的项目后,点击菜单烂的myeclipse---project capablities----add hibernate项,跳出如图务必按照图示选择,尤其是copy checkde……一项,然后点击next,默认next,去掉specify database……复选框,next,去掉create session……复选框,finish。
再次选中选中当前的项目后,点击菜单烂的myeclipse---project capablities----add spring 项,跳出如图依然务必按照如是选择,jar文件选择如下5个:点击next,之后按下选择,务必,next后finsh即可。
然后导入struts2的jar 如下5个放到lib下然后放入我们的数据库驱动jar mysql-connector-java-5.0.8-bin.jar 没得话附件中有至此,包都导入完毕第四步配置web.xml 代码如下注意注释:Xml代码1. <?xml version="1.0"encoding="UTF-8"?>2. <web-app version="2.5"3. xmlns="/xml/ns/javaee"4. xmlns:xsi="/2001/XMLSchema-instance"5. xsi:schemaLocation="/xml/ns/javaee6. /xml/ns/javaee/web-app_2_5.xsd">7.8. <!-- struts2的常规配置 -->9. <filter>10. <filter-name>struts2</filter-name>11. <filter-class>12. org.apache.struts2.dispatcher.FilterDispatcher13. </filter-class>14. </filter>15. <filter-mapping>16. <filter-name>struts2</filter-name>17. <url-pattern>/*</url-pattern>18. </filter-mapping>19.20. <!-- 在web应用启动是自动形成一个spring容器 -->21. <listener>22. <listener-class>23. org.springframework.web.context.ContextLoaderListener24. </listener-class>25. </listener>26. <welcome-file-list>27. <welcome-file>index.jsp</welcome-file>28. </welcome-file-list>29. </web-app>第五步:从视图层入手;在webroot目录下新建一个add.jsp文件如下代码;Jsp代码<%@page language="java"import="java.util.*"pageEncoding="UTF-8"%> <%@taglib uri="/jsp/jstl/core"prefix="c"%><%@taglib prefix="s"uri="/struts-tags"%><%String path = request.getContextPath();String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServ erPort()+path+"/";%><!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'start.jsp' starting page</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><h1align="center">员工信息</h1><formaction="${pageContext.request.contextPath}/emp/addSaveemployee.action "method="post"><table align="center"border="2"bgcolor=""><tr><td align="center"colspan="2">员工信息录入</td></tr><tr><td>姓名</td><td><input type="text"name=""></td></tr><tr><td>年龄</td><td><input type="text"name="employee.age"></td></tr><tr><td>性别</td><td>男<input type="radio"name="employee.sex"value="0"checked>女<input type="radio"name="employee.sex"value="1"></td></tr><tr><td>电话</td><td><input type="text"name="employee.tel"></td></tr><tr><td>地址</td><td><input type="text"name="employee.address"></td></tr><tr><td align="center"colspan="2"><input type="submit"value="提交"></td></tr></table></form></body></html>接下去顺水推舟,自然开发action,及控制层新建包com.ucap.emp.action新建一个EmployeeAction.java 代码初步如下:Java代码package com.ucap.emp.action;import java.util.List;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;import com.ucap.emp.pojo.Employee;import com.ucap.emp.service.EmployeeService;public class EmployeeAction extends ActionSupport {private Employee employee;private List<Employee> list;private EmployeeService employeeService;public List<Employee> getList() {return list;}public void setList(List<Employee> list) {this.list = list;}public EmployeeService getEmployeeService() {return employeeService;}public void setEmployeeService(EmployeeService employeeService) {this.employeeService = employeeService;}public Employee getEmployee() {return employee;}public void setEmployee(Employee employee) {this.employee = employee;}//查询public String list(){list = this.employeeService.findAll(employee);// ServletActionContext.getRequest().setAttribute("list", list);return"list";}//增加public String add(){return"add";}//增加保存public String addSave(){this.employeeService.save(this.employee);employee=new Employee();return list();}//更改之前public String updateBefore(){employee=this.employeeService.findById(employee.getId());return"update";}//更改public String update(){this.employeeService.update(employee);employee=new Employee();return list();}//删除public String delete(){this.employeeService.delete(employee);return list();}}于是,自然我们要新建一个pojo新建包com.ucap.emp.pojo新建Employee.javaJava代码package com.ucap.emp.pojo;public class Employee {private Integer id;private String name;private String age ;private String sex ;private String tel;private String address;public Employee(){}public Employee(Integer id, String name, String age, String sex, String tel,String address) {super();this.id = id; = name;this.age = age;this.sex = sex;this.tel = tel;this.address = address;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) { = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getTel() {return tel;}public void setTel(String tel) {this.tel = tel;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}好了,现在我们先把控制层的action放在这里,开发业务层和持久层(DAO)新建包com.ucap.emp.dao建类EmployeeDAO.java代码如下:Java代码package com.ucap.emp.dao;import java.util.List;import com.ucap.emp.pojo.Employee;public interface EmployeeDao {public void saveEmployee(Employee employee); public void removeEmployee(Employee employee); public void updateEmployee(Employee employee);public Employee findEmployeeById(Integer id);public List<Employee> findAllEmployee(Employee employee);}这是一个接口在开发dao的实现类新建包com.ucap.emp.dao.impl建类EmployeeDAOImpl.java代码:Java代码package com.ucap.emp.dao.impl;import java.util.List;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.ucap.emp.dao.EmployeeDao;import com.ucap.emp.pojo.Employee;public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {public List<Employee> findAllEmployee(Employee employee) { String hql="from Employee ";String where =" where 1= 1 ";if(employee.getName()!=null&&!"".equals(employee.getName())){ hql+=where+" and name like '%"+employee.getName()+"%' order by id desc";}return this.getHibernateTemplate().find(hql);}public void saveEmployee(Employee employee) {this.getHibernateTemplate().save(employee);}public void updateEmployee(Employee employee) {this.getHibernateTemplate().update(employee);}public void removeEmployee(Employee employee) {this.getHibernateTemplate().delete(employee);}public Employee findEmployeeById(Integer id) {Employee employ=(Employee)this.getHibernateTemplate().get(Employee.class, id);return employ;}}好了,数据持久层(dao)开发好了,按顺序,现在应该开发业务层了,新建包com.ucap.emp.service建类EmployeeService.java代码:Java代码package com.ucap.emp.service;import java.util.List;import com.ucap.emp.pojo.Employee;public interface EmployeeService {public void save(Employee employ);public void update(Employee employ);public void delete(Employee employ);public List<Employee> findAll(Employee employee);public Employee findById(Integer id);}这是一个接口类然后写实现类新建包com.ucap.emp.service.impl建类EmployeeServiceImpl.javaJava代码package com.ucap.emp.service.impl;import java.util.List;import com.ucap.emp.dao.EmployeeDao;import com.ucap.emp.pojo.Employee;import com.ucap.emp.service.EmployeeService;public class EmployeeServiceImpl implements EmployeeService {private EmployeeDao employeeDao;public EmployeeDao getEmployeeDao() {return employeeDao;}public void setEmployeeDao(EmployeeDao employeeDao) {this.employeeDao = employeeDao;}//服务层方法public void delete(Employee employ) {this.employeeDao.removeEmployee(employ);}public List<Employee> findAll(Employee employ) {return this.employeeDao.findAllEmployee(employ);}public Employee findById(Integer id) {return this.employeeDao.findEmployeeById(id);}public void save(Employee employ) {this.employeeDao.saveEmployee(employ);}public void update(Employee employ) {this.employeeDao.updateEmployee(employ);}}业务和持久层都开发好了,现在我们回到控制层action修改action的代码如下:Java代码(查看前面完整的action层代码)这里就不做出来了。