一步一教你搭建SSH框架力喷,實(shí)現(xiàn)簡單的增刪改查功能
一刽漂、首先創(chuàng)建一個(gè)Java web項(xiàng)目演训,并按下圖建好項(xiàng)目目錄
二弟孟、在網(wǎng)上搜索ssh所需jar包,并將其放到WEB_INF下的lib里面(這里截圖了部分)
三样悟、如下圖配置web.xml(添加spring監(jiān)聽器以及struts過濾器等)
ssh
index.jsp
OpenSessionInView
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
singleSession
true
OpenSessionInView
/*
setCharactor
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
setCharactor
/*
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.do
*.jsp
FORWARD
REQUEST
INCLUDE
contextConfigLocation
/WEB-INF/classes/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
四拂募、在bo包建立User實(shí)體類及其Hibernate映射文件
User.java
package com.suo.bo;
/**
* User entity. @author MyEclipse Persistence Tools
*/
public class User implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private String password;
private String email;
private String address;
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
}
User類的Hibernate映射文件User.hbm.xml
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
Mapping file autogenerated by MyEclipse Persistence Tools
-->
五庭猩、Dao層編寫? (該層用于數(shù)據(jù)的持久化操作)
UserDAO.java
package com.suo.dao;
import com.suo.bo.User;
public interface UserDAO {
/**
* 保存
* @param user
*/
void saveUser(User user);
/**
* 更新
* @param user
*/
void updateUser(User user);
/**
* 刪除
*/
void deleteUser(User user);
/**
* 查詢
* @return
*/
User queryUserbyName(String name);
}
UserDAO.Java
package com.suo.dao.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.suo.bo.User;
import com.suo.dao.UserDAO;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
@Override
public void saveUser(User user) {
// TODO Auto-generated method stub
getHibernateTemplate().save(user);
}
@Override
public void updateUser(User user) {
// TODO Auto-generated method stub
getHibernateTemplate().update(user);
}
@Override
public void deleteUser(User user) {
// TODO Auto-generated method stub
getHibernateTemplate().delete(user);
}
@Override
public User queryUserbyName(String name) {
// TODO Auto-generated method stub
String queryString="from User t where t.name=?";
// return (User)getHibernateTemplate().find(queryString,new String[]{name}).get(0);
return (User) getHibernateTemplate().find(queryString, name).get(0);
}
}
六、Service層編寫(該層用于數(shù)據(jù)的業(yè)務(wù)處理)
UserService.java
package com.suo.service;
import com.suo.bo.User;
public interface UserService {
void save(User user);
User queryUserbyName(String name);
void updateUser(User user);
void deleteUser(String name);
}
UserServiceImpl.java
package com.suo.service.impl;
import com.suo.bo.User;
import com.suo.dao.UserDAO;
import com.suo.service.UserService;
public class UserServiceImpl implements UserService{
private UserDAO userDAO;
@Override
public void save(User user) {
// TODO Auto-generated method stub
userDAO.saveUser(user);
}
@Override
public User queryUserbyName(String name) {
// TODO Auto-generated method stub
return userDAO.queryUserbyName(name);
}
@Override
public void updateUser(User user) {
// TODO Auto-generated method stub
User userdata=userDAO.queryUserbyName(user.getName());
userdata.setAddress(user.getAddress());
userdata.setEmail(user.getEmail());
userdata.setPassword(user.getPassword());
userDAO.updateUser(userdata);
}
@Override
public void deleteUser(String name) {
// TODO Auto-generated method stub
User user=userDAO.queryUserbyName(name);
userDAO.deleteUser(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
七陈症、Action編寫
package com.suo.action;
import com.opensymphony.xwork2.ActionSupport;
import com.suo.bo.User;
import com.suo.service.UserService;
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private UserService userService;
private User user;
private String name;
public String save(){
userService.save(user);
return "add_success";
}
public String queryUserbyName(){
user? =userService.queryUserbyName(name);
return "query_success";
}
public String deleteUser(){
userService.deleteUser(name);
return "delete_success";
}
public String updateUser(){
String str=user.getName();
//更改前preUser
User preuUser=userService.queryUserbyName(str);
userService.updateUser(user);
//更改后user
User afteruser=userService.queryUserbyName(str);
return "update_success";
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
八蔼水、在hibernate.properties中配置數(shù)據(jù)相關(guān)數(shù)據(jù),以及在applicationContext.xml中配置Spring管理
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
classpath:hibernate.properties
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
${mysql.database.driver_class}
${mysql.database.url}
${mysql.database.username}
${mysql.database.password}
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
org.hibernate.dialect.MySQL5Dialect
classpath:com/suo/bo
PROPAGATION_NOT_SUPPORTED
PROPAGATION_NOT_SUPPORTED
PROPAGATION_NOT_SUPPORTED
PROPAGATION_NOT_SUPPORTED
PROPAGATION_NOT_SUPPORTED
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED
九录肯、Struts.xml文件的編寫
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
/add_success.jsp
/delete_success.jsp
/update_success.jsp
/query_success.jsp
十趴腋、前端Jsp頁面的編寫
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
-->
ssh框架整合學(xué)習(xí)demo
新增
名稱:
密碼:
郵箱:
住址:?
?
查詢
名稱:
?
修改
名稱:
密碼:
郵箱:
住址:?
?
刪除
名稱:
?
query_success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'query_success.jsp' starting page
-->
查詢成功实昨!
查詢結(jié)果
名字 :
密碼:
郵箱:
住址:?
其他的前端jsp類似? 就不在寫了
源碼下載地址:點(diǎn)擊打開鏈接
十一命咐、部署運(yùn)行