SSH框架搭配和配置

6 篇文章 0 订阅
5 篇文章 0 订阅

ssh框架是一个集成框架,下面我用的是: spring4.2  +  hibernate 5.3 +struts2

第一步  ssh框架的 搭配需导入 spring,struts2,hibernate 相关jar包。

可直接下载以下包导入到项目的WEB-INF的lib下:

链接:https://pan.baidu.com/s/1PI7YjgwZEQtyxPhXSVsnlw 
提取码:w3y9 


第二步   配置web.xml文件(无需修改,直接用)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list>
  <!-- spring配置文件 -->
  <listener>
  		<listener-class>
  			org.springframework.web.context.ContextLoaderListener
  		</listener-class>
  </listener>
  <context-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 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>
  <!-- 配置osiv模式,针对hibernate的延迟加载 -->
  <filter>
  	<filter-name>OpenSessionInViewFilter</filter-name>
  	<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>OpenSessionInViewFilter</filter-name>
  	<url-pattern>*.action</url-pattern>
  </filter-mapping>
</web-app>

第三步  在src下建立 applicationContext.xml (dataSource中数据库配置需根据自身修改,映射文件则以项目为根据修改、添加,事务配置据自身开发情况修改)

<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans 
						   http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
						   http://www.springframework.org/schema/context 
						   http://www.springframework.org/schema/context/spring-context.xsd 
						   http://www.springframework.org/schema/aop 
						   http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
						   http://www.springframework.org/schema/tx 
						   http://www.springframework.org/schema/tx/spring-tx.xsd" 
						   xmlns:tx="http://www.springframework.org/schema/tx" 
						   xmlns:aop="http://www.springframework.org/schema/aop" 
						   xmlns:context="http://www.springframework.org/schema/context" 
						   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
						   xmlns="http://www.springframework.org/schema/beans">
    	
    <!--数据源配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:MySQL://localhost:3306/homework?serverTimezone=UTC"/>
        <property name="user" value="root"/>
        <property name="password" value="123456"/>
        <property name="initialPoolSize" value="3"/>
        <property name="maxPoolSize" value="20"/>
        <property name="minPoolSize" value="1"/>
        <property name="checkoutTimeout" value="2000"/>
        <property name="maxIdleTime" value="60"/>
    </bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--hibernate常用配置-->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
            </props>
        </property>
        <!--映射文件配置-->
        <property name="mappingResources">
            <list>
                <value>com/test/model/name.hbm.xml</value>
            </list>
        </property>
    </bean>
    
    <!--事务配置-->
    <!--配置事务管理器类-->
    <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <!--配置事务增强-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
            <tx:method name="get*" propagation="NEVER"/>
            <tx:method name="set*" propagation="NEVER"/>
        </tx:attributes>
    </tx:advice>
   
    <!--aop配置-->
    <aop:config  proxy-target-class="true">
        <aop:pointcut id="pt" expression="execution(* com.test.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
     
</beans>    

第四步  在src下配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
    <constant name="struts.configuration.xml.reload" value="true"/>
    <constant name="struts.serve.static.browserCache" value="false"></constant>
    
    <package name="default" namespace="/user" extends="struts-default">
	   
	    
	</package>
</struts>

最后,用个测试演示下ssh

测试用例功能:在页面输入姓名,添加姓名到数据库

一、建包 com.test.model,建类Name

Name.java

package com.test.model;

import java.io.Serializable;

public class Name implements Serializable{
	public Name() {
		super();
	}
	public Name(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

		private int id;
		private String name;
		
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
		@Override
		public String toString() {
			return "Name [id=" + id + ", name=" + name + "]";
		}
}

在com.test.model下再建  name.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.test.model">
    
    <class name="Name" table="name">
    	<id name="id" column="id">
    		<generator class="increment"></generator>
    	</id>
    	<property name="name" column="name"></property>
    </class>


</hibernate-mapping>

二、建包com.test.dao,建立接口类NameDao,实现类 NameDaoImpl

接口类NameDao

package com.test.dao;

import com.test.model.Name;

public interface NameDao {
		public int save(Name name);
}

实现类 NameDaoImpl

package com.test.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.test.model.Name;

public class NameDaoImpl implements NameDao{
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	@Override
	public int save(Name name) {
		Session session=sessionFactory.getCurrentSession();
		session.save(name);
		return 1;
	}
		
}

三、建包com.test.service,建实现类NameserviceImpl

实现类NameserviceImpl

package com.test.service;

import com.test.dao.NameDaoImpl;
import com.test.model.Name;

public class NameserviceImpl {
	private NameDaoImpl nameDaoImpl;
	
	public NameDaoImpl getNameDaoImpl() {
		return nameDaoImpl;
	}

	public void setNameDaoImpl(NameDaoImpl nameDaoImpl) {
		this.nameDaoImpl = nameDaoImpl;
	}

	
	public int save(Name name) {
		return nameDaoImpl.save(name);
	}
		
}

四、建包com.test.action,建类NameAction

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;
import com.test.model.Name;
import com.test.service.NameserviceImpl;

public class NameAction extends ActionSupport{
		private NameserviceImpl nameserviceImpl;
		private String name;

		public NameserviceImpl getNameserviceImpl() {
			return nameserviceImpl;
		}

		public void setNameserviceImpl(NameserviceImpl nameserviceImpl) {
			this.nameserviceImpl = nameserviceImpl;
		}
		
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String add() throws Exception{
			Name aName=new Name();
			aName.setName(name);
			nameserviceImpl.save(aName);
			return "add";
		}
}

五,在applicationContext.xml添加配置

<!-- 配置Dao层 -->
    <bean id="nameDaoImpl" class="com.test.dao.NameDaoImpl">
    	<property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
<!-- 配置service层 -->
    <bean id="nameserviceImpl" class="com.test.service.NameserviceImpl">
    	<property name="nameDaoImpl" ref="nameDaoImpl"></property>
    </bean>
 <bean id="nameAction" class="com.test.action.NameAction" scope="prototype">
    	<property name="nameserviceImpl" ref="nameserviceImpl"></property>
    </bean>

六、在struts.xml添加配置(执行完后跳去ok.jsp)

<action name="addName" class="nameAction" method="add">
	    	<result name="add">
	    		/ok.jsp
	    	</result>
	    </action>

七、建立一个jsp,add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
		<form action="user/addName">
			姓名:<input type="text" name="name">
			<input type="submit">
		</form>
</body>
</html>

最后,运行tomcat就好了。(ps,name.hbm.xml在之前的applicationContext.xml以配置映射)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值