S2SH项目搭建

4 篇文章 0 订阅
4 篇文章 0 订阅

struts2、spring、hibernate集成搭建WEB项目

开发工具:myeclipse8.6.0、jdk6.0、apache-tomcat-6.0.18

操作系统:win7 32 位

框架:struts2.2.1、spring2.5.5、hibernate3

 

MyEclipse配置

 

使用myeclipse进行搭建

首先安装好jdk6.0或以上版本,有一个tomcat服务器

打开myeclipse配置tomcat服务器及jdk

 

1.配置Tomcat

 

WindowèPreferencesèMyEclipseèServersèTomcatèTomcat6.x

 

 

 在右边的Tomcat home directory中选择tomcat的安装目录或者免安装版tomcat的存放目录

2.配置jdk

WindowèPreferencesèMyEclipseèServersèTomcatèTomcat6.x

选择tomcat6.x下的JDK

在右边中 Tomcat JDK name 项,点击右边的 AddA 按钮,点击 Directory 按钮,选择 jdk 的安装目录(我这边报了一个 JRE 已经存在,是因为我之前已经加过该 jdk ,此处只为演示如何添加),然后点击 Finish ,点击 OK 完成。

 

3.创建一个web项目

右键==New ==Web Project

 

填写Project Name ,点击Finish即可

 

 

Struts2搭建步骤

 

1.导入struts2所需要的jar

 

导入这8jar

 

2.struts2的核心过滤器配置到web.xml文件中

 

Web.xml文件中增加如下代码

 

<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>
3.编写struts.xml配置文件

 

src目录下创建一个struts.xml文件,此处为了方便后续配置文件的管理,专门创建一个目录resources存放配置文件,步骤如下

1)右键项目==New ==Folder Folder name中填写resources(名字可以任意起),点击Finish

2)修改.classpath文件,增加如下语句

<classpathentry kind="src" path="resources"/>(这里path后要填写(1)中创建的Folder name

然后在resources目录下创建一个struts.xml文件

 

 

3struts.xml中增加如下内容(struts.xml的配置大家可以参考网上更详细的资料)

 

<!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.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="true" />
	<constant name="struts.action.extension" value="html" />

	<package name="default" namespace="/" extends="struts-default">
	
		<action name="login" class="baseAction" method="execute">
			<result name="success">WEB-INF/login.jsp</result>
		</action>
				
	</package>
	
</struts>
 

 

4)增加相应的login.jsp文件及BaseAction.java文件

WEB-INF目录下添加一个login.jsp文件,内容就写如下一句话即可,仅用于测试

<body>

    Welcome. <br>

</body>

 

 

添加BaseAction.java文件

 

创建包com.hqb.qa.action,然后在包里创建BaseAction.java文件

 

 

文件内容(此处也可以不继承ActionSupport

 

package com.hqb.qa.action;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport{
	
	public String execute (){
		System.out.println("=============");
		return "success" ;
	}

}

 此时基本已经成功,将项目使用tomcat服务器运行

 

 

最后点击Finish,点击OK即可

 

 

 

地址栏中输入http://localhost:8080/sgw/login.action即可查看刚才我们添加的login.jsp文件

由于struts2默认的访问后缀为.action,可以通过struts.action.extension参数进行配置,该参数可以配置在struts.xml文件中

Struts.xml增加如下代码

<constant name="struts.action.extension" value="html" />

然后访问路径:http://localhost:8080/sgw/login.html

也可以配置到struts.properties文件中(推荐用这种方法)

Resources目录下增加struts.properties文件,内容为

struts.action.extension=html

 

Spring集成

1.导入Spring的核心jar包及struts2spring集成所需的jar

spring.jar

 

struts2-spring-plugin-2.1.2.jar

 

2.修改web.xml

 

Web.xml中加入如下代码

<listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3. 编写spring的配置文件

由于spring默认的配置文件是applicationcontext.xml并且是放在src根目录下的,为了方便后期管理,在web.xml中增加如下代码

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationcontext-*.xml</param-value>
</context-param>

这样spring即可读取项目中所有以applicationcontext-开头的xml文件

resources目录下增加applicationcontext-action.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:jee="http://www.springframework.org/schema/jee"
	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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<bean id="baseAction" class="com.hqb.qa.action.BaseAction"  scope="prototype">
	</bean>
	
</beans>

 4.修改struts配置文件

<action name="login" class="baseAction" method="execute">
	<result name="success">WEB-INF/login.jsp</result>
</action>

 使得struts所使用的action托管给spring管理

然后访问路径:http://localhost:8080/sgw/login.html

发现依然可以访问login.jsp页面

 

Hibernate集成

1.导入相关jar

hibernate3.jar

dom4j-1.6.1.jar

commons-collections-3.2.jar

hibernate-jpa-2.0-api-1.0.0.Final.jar

jta-1.1.jar

slf4j-api-1.6.1.jar

2.编写Model层、Dao层、Dao层实现和Service

(1)Model

 

(2)Dao层、Dao层实现

 

(3)Service层、service层实现

 

(4)配置spring管理DaoService

resources目录下增加applicationcontext-service.xmlapplicationcontext-dao.xml文件,里边增加相应的bean配置,其实可以配置在一个文件中,只是为了方便后期管理,将ActionDaoService分别配置在不同的配置文件中。

(5)数据库中增加User对应的表

3.配置proxool数据库连接池

(1)导入proxooljar

proxool-0.9.0RC3_.jar

ojdbc6.jar (数据库使用的是oracle,所以要引入oracle的驱动包)

(2)编写proxool配置文件

resources目录下增加proxool-conf.xml文件

(3)web.xml中配置proxool连接池(使用listener方式)

4.配置spring声明式事物

(1)配置DataSource

 

(2)配置hibernate sessionFactory

 

(3)配置事物管理器

 

(4)配置事物拦截器

 

(5)配置代理类,管理事物

 

测试:

(1)创建一个user.jspuser_create.jsp

user.jsp里边包括一个用户信息的提交表单

user_create.jsp显示新增后的用户信息

 

(2)创建UserAction.java

 

包括两个对象(包括对应的gettersetter方法)

public class UserAction extends BaseAction{
        private User 	user ;
	
	private IUserService userService ;

//对应的getter、setter发方法

        public String init (){
		return "init" ;
	}

	public String create (){
		try {
			userService.create(user) ;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "create" ;
	}
}

 

(3)spring中配置,管理当前Action

 

 

(4)配置struts.xml

<action name="*user" class="userAction" method="{1}">
	<result name="init">user.jsp</result>
	<result name="create">user_create.jsp</result>
	<result name="update">user_update.jsp</result>
	<result name="delete">user_delete.jsp</result>
</action>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值