关于SSH框架的一些整合要点

        昨天做了SSH框架的整合,但是也是感觉生疏了许多,下面介绍一下SSH整合的具体过程和常见问题的注意事项!

1:首先做struts部分的测试,在src目录导入struts.xml文件,然后在web.xml中添加核心过滤器StrutsPrepareAndExecuteFilter


    在项目中导入相应的Struts,spring,hibernate的jar包

    编写一个测试页面index.jsp

      

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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>
    	 <h1>welcome login page</h1>
    	 <form action="user/login.action" method="post">
    	                     用户名:<input type="text" name="user.username"/>
    	                     密    码:<input type="password" name="user.password"/>
    	            <input type="submit" value="提交"/>
    	 </form>
    	 
  </body>
</html>
在地址栏中键入一下url,出现如下界面说明struts配置成功!

2:spring和hibernate的整合:

    a:建立jdbc.properties文件


    b: 在WEB-INF下创建spring-hibernate.xml文件

  /WEB-INF/spring-hibernate.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        <!-- 开启spring注解功能 -->
        <context:annotation-config/>
        <tx:annotation-driven/>
        <context:property-placeholder location="classpath:jdbc.properties"/>
        <context:component-scan base-package="com.li.action"/>
        <context:component-scan base-package="com.li.model"/>
        <context:component-scan base-package="com.li.service"/>
        <context:component-scan base-package="com.li.dao"/>
        <!-- 使用dbcp2的数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        		<property name="driverClassName" value="${jdbc.driverClass}"/>
        		<property name="url" value="${jdbc.url}"/>
        		<property name="username" value="${jdbc.username}"/>
        		<property name="password" value="${jdbc.password}"/>
        </bean>
        <!-- sessionFactory的声明 -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
             <property name="dataSource" ref="dataSource"/>
             <property name="packagesToScan">
                    <list>
                         <value>com.li.model</value>
                    </list>
             </property>
             <property name="hibernateProperties">
             		<value>
                		hibernate.dialect=org.hibernate.dialect.MySQLDialect
            		</value>
             		
             </property>
        </bean>
        <bean id="userDAO" class="com.li.dao.UserDAO">
        		<property name="sf" ref="sessionFactory"></property>
        </bean>
        <bean id="userService" class="com.li.service.UserService">
        		<property name="userDAO" ref="userDAO"></property>
        </bean>

</beans>
上面标红的一行的配置文件一定要放好位置哦,如果放到web-inf下会报错的要注意!!

 c:创建User实体类对应数据库中的表tb_user

   

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="tb_user")
public class User {
	private int id;
	private String username;
	private String password;
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
	
}

d:建立UserDAO类 利用hibernate实现对底层数据库的操作

 import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import javax.annotation.Resource;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

//数据库访问层
import com.li.model.User;
public class UserDAO {

private SessionFactory sf;
public SessionFactory getSf() {
return sf;
}

         //使用Spring的自动注入功能
@Resource(name="sessionFactory")
public void setSf(SessionFactory sf) {
this.sf = sf;
}

       //获取用户列表信息
public List<User> getUsers() {
// TODO Auto-generated method stub
//打开hibernate的session
List<User> users = new ArrayList<User>();  
       Session session = sf.openSession();
       Transaction tc = (Transaction) session.beginTransaction();  
       List list = session.createQuery("From User").list();  
       for (Iterator iterator = list.iterator(); iterator.hasNext();) {  
           User u = (User) iterator.next();  
           users.add(u);  
       }  
       try {  
           tc.commit();  
       } catch (Exception e) {  
           e.printStackTrace();  
       }  
       session.close();  
       return users;
 
}
 
 
}

e:建立UserService

import javax.annotation.Resource;


import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


import com.li.dao.UserDAO;
import com.li.model.User;
public class UserService {
private UserDAO userDAO;
public UserDAO getUserDAO() {
return userDAO;
}
@Resource(name="userDAO")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}


//获得用户列表
public List<User> getUsers(){
return userDAO.getUsers();
}



}
f:建立Action,showUserAction

import java.util.List;


import javax.annotation.Resource;


import org.springframework.stereotype.Controller;


import com.li.model.User;
import com.li.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class ShowUsersAction extends ActionSupport{

        //存储接收的User列表信息
private List<User> users;

        //用户服务业务类
private UserService userService;
public void setUsers(List<User> users) {

this.users = users;
}
public List<User> getUsers(){
return users;
}

public UserService getUserService() {
return userService;
}
@Resource(name="userService")
public void setUserService(UserService userService) {
this.userService = userService;
}

        //演示用
public String listUsers(){
System.out.println("userService is " + userService);
this.users = userService.getUsers();
System.out.println("users is " + this.getUsers());
if(this.getUsers() != null){
return SUCCESS;
}else{
return ERROR;
}

}
}
g:在Struts.xml做如下配置:


h:当然最重要的是struts2-spring-plugins.jar包的导入,没有导入它会出现User is not mapped等等的错误信息

i:在web.xml作如下配置:

好了大功告成SSH整合完毕!文章有不足和错误之处欢迎指正。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值