歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網

SSH搭建

日期:2017/3/1 13:51:42   编辑:關於Linux

前言

本文以Tomcat為j2ee容器,數據庫為Sqlserver2005進行說明。Struts版本為2.3.15.3,Spring版本為3.2.5,Hibernate版本為4.2.7

新建SSH

引入lib包

所需包如下圖所示:

\\

web.xml配置

"1.0"encoding="UTF-8"?>

"2.5"xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

contextConfigLocation

/WEB-INF/applicationContext.xml/WEB-INF/spring-service.xml

org.springframework.web.context.ContextLoaderListener

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

index.jsp

新建applicationContext.xml

在項目中WEB-INF下新建applicationContext.xml文件,注意此處JDBC配置使用的JNDI配置,大家可以根據具體情況進行更改,裡面內容如下:

"1.0"encoding="UTF-8"?>

"http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd"default-autowire="byName">

"managerService"class="org.ssh.service.ManagerService"/>

"managerDao"class="org.ssh.dao.ManagerDao"/>

新建struts.xml

在SRC目錄下新建struts.xml文件,注意:要想實現Spring托管Struts必須在此配置文件中加入這句代碼,具體內容如下:

"1.0"encoding="UTF-8"?>

"-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

"struts.objectFactory"value="spring"/>

"struts.devMode"value="false"/>

"basicstruts2"extends="struts-default">

"index">

/index.jsp

"hello"

class="org.apache.struts.helloworld.action.HelloWorldAction"method="execute">

"success">/HelloWorld.jsp

新建hibernate.cfg.xml

'1.0'encoding='utf-8'?>

"-//Hibernate/HibernateConfiguration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

"connection.datasource">java:comp/env/jdbc/ehrdb

"connection.pool_size">1

"dialect">org.hibernate.dialect.SQLServerDialect

"current_session_context_class">thread

"cache.provider_class">org.hibernate.cache.internal.NoCacheProvider

"show_sql">true

"hbm2ddl.auto">update

"org/ssh/model/TbManager.hbm.xml"/>

新建HibernateUtil類

package org.ssh.dao.util;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

publicclass HibernateUtil {

privatestaticfinal SessionFactory sessionFactory = buildSessionFactory();

privatestatic SessionFactory buildSessionFactory(){

try{

// Create the SessionFactory fromhibernate.cfg.xml

returnnew Configuration().configure().buildSessionFactory();

}catch(Throwable ex){

// Make sure you log the exception, as itmight be swallowed

System.err.println("Initial SessionFactory creationfailed."+ ex);

thrownew ExceptionInInitializerError(ex);

}

}

publicstatic SessionFactory getSessionFactory(){

return sessionFactory;

}

}

部署運行

將程序部署至tomcat,訪問 http://localhost:8080/SSH 運行

Demo下載地址

以上代碼均為部分核心配置,完整demo下載地址如下:

http://download.csdn.net/detail/zfz1214/6679953

Copyright © Linux教程網 All Rights Reserved