码迷,mamicode.com
首页 > 其他好文 > 详细

SSH(一):环境搭建

时间:2015-02-16 13:07:22      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

      SSH为struts+spring+hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架。大多数公司都在使用。

     集成SSH框架的系统从职责上分为三层:表示层业务逻辑层数据持久层,以帮助开发人员在短期内搭建结构清晰、可复用性好、维护方便的Web应用程序。其中使用Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转;利用Hibernate框架对持久层提供支持,处理请求数据并返回结果;Spring则是做管理,管理struts和hibernate。

     不管是什么项目,当然,前提是一个新的项目,我们想要使用SSH框架进行开发,需得做以下五件事。其中前三个是必须要做的,后两个则是为了快速且清晰开发,方便管理而可选加入的。

     主要步骤:

一、新建web工程

二、搭建框架环境

三、整合SSH

四、资源分类

五、配置日志

 

一、新建web工程

     通过不同的IDE新建一个web工程,这里需要注意的是养成一个很好的习惯,新建完项目后首先去设置器编码格式为UTF-8,从一定程度上避免乱码问题。

二、搭建框架环境

1.struts2


     Struts作为系统的整体基础架构,负责MVC的分离。那么它到底是如何实现MVC分离的呢?

     M(模型):这个一般不由Struts来做V(视图):视图也不算struts的强项,但是struts提供优秀的标签来支持视图的展示,利用标签,可以将数据合理的展示给用户;C(控制器):struts 的重要功能,提供struts的过滤器,拦截用户的请求,查找struts配置文件,为其匹配一个对应的Action,这个Action负责调用模型,获得数据,然后对数据做部分处理,接着Action再将处理后的数据,为其选择一个视图进行输出。

     知道原理了,再来看看如何配置struts吧。


(1)所需jar包

技术分享


(2)配置文件

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>

	<!-- 配置为开放模式:配置文件改了以后不用重新启动 -->
	<constant name="struts.devMode" value="true" />

	<!-- 扩展名配置为action -->
	<constant name="struts.action.extension" value="action" />

	<!-- 把主题配置为simple -->
	<constant name="struts.ui.theme" value="simple" />


	<package name="default" namespace="/" extends="struts-default">

		<!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->
		<action name="test" class="cn.itcast.oa.PersonAction">
			<result name="success">/test.jsp</result>
		</action>

	</package>

</struts>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="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">

	<!-- 配置struts2 :注意配置struts2的filter标签要放到所有filter标签的最下面,否则会有问题。-->
	<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>

</web-app>

     struts配置完成了之后,我们可以对其先进行一下测试。personAction类中代码如下:

package cn.itcast.oa;

import com.opensymphony.xwork2.ActionSupport;


public classpersonAction extends ActionSupport {


    @Override
 
    public String execute() throws Exception {

        System.out.println("调到action了");
   
        return"success";

    }

}

     通过在浏览器地址栏直接访问action:http://localhost:8080/ItcastOA/test.action,我们可看到浏览器的成功页面:

技术分享

     控制台也打印出相应信息:

 技术分享


2. Hibernate


     说完struts的配置,接下来说说Hibernate吧。大家都知道,Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得程序员可以随心所欲的使用对象编程的思维来操纵数据库。它的配置也很简单:


(1)所需jar包

 技术分享


(2)配置文件

hibernate.cfg.xml

*.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

	<session-factory>
		<!--1. 数据库连接信息-->
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
	</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/oa0909
		</property>
		<property name="connection.username">admin</property>
		<property name="connection.password">123456</property>

		<!--2. 其他配置-->
		<property name="myeclipse.connection.profile">mysql</property>
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">update</property>

		<!--3. 导入映射文件-->
		<!--	<mapping resource="Person.hbm.xml" />-->
		<mapping resource="cn/itcast/oa/domain/Role.hbm.xml" />
		<mapping resource="cn/itcast/oa/domain/User.hbm.xml" />
	</session-factory>

</hibernate-configuration>

     关于Hibernate的应用,和使用c3p0配置数据库连接池等在此不做过多介绍,后面会有专门的文章进行讲解。


3. Spring


     Struts和Hibernate都已经搭建完成了,现在就剩下起到管理作用的Spring。因为spring基于IoC(Inversion of Control,反向控制)和AOP构架的多层j2ee系统的框架,但它不强迫你必须在每一层中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;采用IoC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现事务管理(TranscationManagment)。关于Spring的配置如下:


(1)所需jar包

技术分享


(2)配置文件

applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<!-- 自动扫描与装配bean -->
	<context:component-scan base-package="cn.itcast.oa"></context:component-scan>

</beans>

     在这里,我们采用注解配置Bean的方式。在applicationContext.xml中加入自动扫描后,我们还需在那些我们想交给Spring管理的类上加上相应的注解,如@Component,@Service,@Controller,@Repository。

package cn.itcast.oa.view.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class PersonAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		System.out.println("调到action了");
		return "success";
	}
}

     对Spring的测试,内容如下:

package cn.itcast.oa.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.oa.view.action.PersonAction;


public class PersonActionTest {

	private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	@Test
	public void testBean(){
		PersonAction personAction= (PersonAction) ac.getBean("personAction");
		System.out.println(personAction);
	}
	
}


运行该测试类后,结果为:

技术分享

技术分享

   

    至此,三个环境全部搭建完成了,但是现在他们还是彼此独立的存在,如何将他们整合起来,更好的进行开发呢?下篇再继续~

SSH(一):环境搭建

标签:

原文地址:http://blog.csdn.net/sunliduan/article/details/43851187

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!