在eclipse下手动配置ssh的环境,Spring,Struts2,Hibernate
2014-03-19 11:28
736 查看
昨天想配个ssh的环境,然后一看都是在myclipse 下配置的,大多数都是在工程里面引用不是自己自己加包配置。
所以自己试了一次,最大的感触就是千万别忘了一些小的配置文件,不然不报错,真是难找错误。。。。。下面是我配的过程,和我对一下东西的理解,希望对大家有帮助
![](https://oscdn.geek-share.com/Uploads/Images/Content/201707/61c17ed2eb67211a84e137429ac3f7ad.gif)
目录
1、配置Struts.2
1.1、最基本的jar包...2
1.2建立RegisterAction.java.2
1.3、配置web.xml2
1.4配置struts.xml4
2配置Spring.4
2.1、所需的jar包...4
2.2配置beans.xml4
2.2.1首先构建/src下jdbc.properties.4
2.2.2在beans.xml中配置...4
在web.xml去配置监听spring.7
3、配置Hibeinate.7
1、所需要的jar包...7
4、建立model8
5建立dao,daoimpl9
6、构建UserManager和UserManagerImpl10
7、引入Struts2-Spring-Plugin,构建Action.11
8通过junit进行测试...14
![](https://oscdn.geek-share.com/Uploads/Images/Content/201403/cee9ac90bb08643d959e5b53ba1d8062)
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.mycompany.myapp.actions</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--... -->
</web-app>
![](https://oscdn.geek-share.com/Uploads/Images/Content/201403/79ba10eb28627944f000d88da65a57d6)
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=*****
其中classpath是引用src目录下的文件写法。
(2)获取多个配置文件时,配置就需要使用locations
<!--获取properties的资源文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"
value="${jdbc.driverClassName}"/>
<propertyname="url"value="${jdbc.url}"/>
<propertyname="username"value="${jdbc.username}"/>
<propertyname="password"value="${jdbc.password}"/>
</bean>
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<!-- 为了将model交给Spring管理,然后给Hibernate使用
-->
<propertyname="packagesToScan">
<list>
<value>org.lei.model</value>
</list>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<propkey="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
hibernate3.HibernateTemplate">
<propertyname="sessionFactory"ref="ssionFactory"></property>
</bean>
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>
<aop:config>
<aop:pointcutid="bussinessService"
expression="execution(public* org.lei.service.*.*(..))"/>
<aop:advisorpointcut-ref="bussinessService"
advice-ref="txAdvice"/>
</aop:config>
<tx:adviceid="txAdvice"transaction-manager="txManager">
<tx:attributes>
<tx:methodname="exists"read-only="true"/>
<tx:methodname="add*"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
![](https://oscdn.geek-share.com/Uploads/Images/Content/201403/20288693abde7a328dc323081a2dc22e)
*
*/
package org.lei.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
*@author renlei
*@date 2014-3-18 下午9:49:46
*/
@Entity
public class User {
privateint id ;
privateString username;
privateString password;
表示主键自增
@Id
@GeneratedValue
publicint getId() {
returnid;
}
publicvoid setId(int id) {
this.id= id;
}
publicString getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username= username;
}
publicString getPassword() {
returnpassword;
}
publicvoid setPassword(String password) {
this.password= password;
}
}
*
*/
package org.lei.dao.impl;
import javax.annotation.Resource;
import org.lei.dao.UserDao;
import org.lei.model.User;
importorg.springframework.orm.hibernate3.HibernateTemplate;
importorg.springframework.stereotype.Component;
/**
*@author renlei
*@date 2014-3-19 上午10:19:30
*/
//(把普通userDao实例化到spring容器中,相当于配置文件中的<beanid="" class=""/>)
@Component("userDao")
public class UserDaoImpl implementsUserDao{
privateHibernateTemplate hibernateTemplate;
publicHibernateTemplate getHibernateTemplate() {
returnhibernateTemplate;
}
/*将hibernateTemplate通过spring进行注入*/
@Resource
publicvoid setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate= hibernateTemplate;
}
/*(non-Javadoc)
* @seeorg.lei.dao.UserDao#save(org.lei.model.User)
*/
@Override
publicboolean save(User user) {
//TODO Auto-generated method stub
hibernateTemplate.save(user);
returntrue;
}
}
*
*/
package org.lei.service.impl;
import javax.annotation.Resource;
import org.lei.dao.UserDao;
import org.lei.model.User;
import org.lei.srvice.UserManager;
importorg.springframework.stereotype.Component;
/**
*@author renlei
*@date 2014-3-18 下午9:54:10
*/
/*(把普通userManagerImpl实例化到spring容器中,相当于配置文件中的<beanid="" class=""/>)*/
@Component("userManager")
public class UserManagerImpl implementsUserManager{
private UserDao userDao;
publicUserDao getUserDao() {
returnuserDao;
}
/*将userDao进行注入(即userDaoImple的实例)*/
@Resource(name="userDao")
publicvoid setUserDao(UserDao userDao) {
this.userDao= userDao;
}
/*(non-Javadoc)
* @seeorg.lei.srvice.UserManage#add(org.lei.model.User)
*/
@Override
publicboolean add(User user) {
userDao.save(user);
returntrue;
}
}
*
*/
package org.lei.action;
import javax.annotation.Resource;
import org.lei.model.User;
import org.lei.srvice.UserManager;
importorg.springframework.context.annotation.Scope;
importorg.springframework.stereotype.Component;
importcom.opensymphony.xwork2.ActionSupport;
/**
*@author renlei
*@date 2014-3-18 下午7:14:08
*/
/*声明componet和scope,但貌似这两个不起作用,去了也没有问题,整个action好像
都交给了插件在管理*/
@Component("registerAction")
@Scope("prototype")
public class RegisterAction extendsActionSupport{
/**
*
*/
privateUserManager userManager;
publicUserManager getUserManager() {
returnuserManager;
}
/*将userManager进行注入,这里是由Struts-Spring-plugin的插件注入的,不加@resource也可以
* 加了貌似没有起作用,默认去找一个userManager
* */
@Resource
publicvoid setUserManage(UserManager userManager) {
this.userManager= userManager;
}
privatestatic final long serialVersionUID = 1L;
privateString username;
privateString password;
publicString getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username= username;
}
publicString getPassword() {
returnpassword;
}
publicvoid setPassword(String password) {
this.password= password;
}
@Override
publicString execute() throws Exception {
//TODO Auto-generated method stub
Useruser = new User();
user.setUsername(username);
user.setPassword(password);
if(userManager.add(user));
returnSUCCESS;
}
}
*
*/
package org.lei.action;
import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
/**
*@author renlei
*@date 2014-3-19 上午10:49:43
*/
public class RegisterActionTest {
/**
* @throws java.lang.Exception
*/
@Before
publicvoid setUp() throws Exception {
}
/**
* Test method for {@linkorg.lei.action.RegisterAction#execute()}.
*/
@Test
publicvoid testExecute() {
ApplicationContextctx = new ClassPathXmlApplicationContext("beans.xml");
RegisterActionua = (RegisterAction)ctx.getBean("registerAction");
try {
ua.execute();
}catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
//Assert.assertTrue(ua.getUsers().size()> 0);
}
}
下面是我当时配置好的工程的下载地址
ssh工程下载
所以自己试了一次,最大的感触就是千万别忘了一些小的配置文件,不然不报错,真是难找错误。。。。。下面是我配的过程,和我对一下东西的理解,希望对大家有帮助
![](https://oscdn.geek-share.com/Uploads/Images/Content/201707/61c17ed2eb67211a84e137429ac3f7ad.gif)
目录
1、配置Struts.2
1.1、最基本的jar包...2
1.2建立RegisterAction.java.2
1.3、配置web.xml2
1.4配置struts.xml4
2配置Spring.4
2.1、所需的jar包...4
2.2配置beans.xml4
2.2.1首先构建/src下jdbc.properties.4
2.2.2在beans.xml中配置...4
在web.xml去配置监听spring.7
3、配置Hibeinate.7
1、所需要的jar包...7
4、建立model8
5建立dao,daoimpl9
6、构建UserManager和UserManagerImpl10
7、引入Struts2-Spring-Plugin,构建Action.11
8通过junit进行测试...14
1、配置Struts
1.1、最基本的jar包
1.2建立RegisterAction.java
1.3、配置web.xml
Struts版本不一样,所使用的filter是不一样的Changed Filter Structure in Struts >= 2.1.3 To split up the the dispatcher phases, FilterDispatcher is deprecated since Struts 2.1.3. If working with older versions, you need to use ... <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class> ... See SiteMesh Plugin for an example on when to use seperate Filters for prepare and execution phase |
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.mycompany.myapp.actions</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--... -->
</web-app>
1.4配置struts.xml
2配置Spring
2.1、所需的jar包
2.2配置beans.xml
2.2.1首先构建/src下jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=*****
2.2.2在beans.xml中配置
在使用Spring配置获取properties文件时,在网上查到相关的资料, (1)获取一个配置文件 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>file:./mes.properties</value> </property> </bean>
其中classpath是引用src目录下的文件写法。
(2)获取多个配置文件时,配置就需要使用locations
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:/resources/jdbc.properties</value> <value>classpath:/resources/config.properties</value> </property> </bean>
<!--获取properties的资源文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
构建dataSource
<beanid="dataSource"destroy-method="close"class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"
value="${jdbc.driverClassName}"/>
<propertyname="url"value="${jdbc.url}"/>
<propertyname="username"value="${jdbc.username}"/>
<propertyname="password"value="${jdbc.password}"/>
</bean>
构建sessionFactory基于annotation
<beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<!-- 为了将model交给Spring管理,然后给Hibernate使用
-->
<propertyname="packagesToScan">
<list>
<value>org.lei.model</value>
</list>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<propkey="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
构建hibernateTemplate,将sessionFactory注入
<beanid="hibernateTemplate"class="org.springframework.orm.hibernate3.HibernateTemplate">
<propertyname="sessionFactory"ref="ssionFactory"></property>
</bean>
<!-- 构建HibernateTransactionManager-->
<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>
<aop:config>
<aop:pointcutid="bussinessService"
expression="execution(public* org.lei.service.*.*(..))"/>
<aop:advisorpointcut-ref="bussinessService"
advice-ref="txAdvice"/>
</aop:config>
<tx:adviceid="txAdvice"transaction-manager="txManager">
<tx:attributes>
<tx:methodname="exists"read-only="true"/>
<tx:methodname="add*"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
在web.xml去配置监听spring
<!-- 去监听Spring --><listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
3、配置Hibeinate
1、所需要的jar包
4、建立model
/***
*/
package org.lei.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
*@author renlei
*@date 2014-3-18 下午9:49:46
*/
@Entity
public class User {
privateint id ;
privateString username;
privateString password;
表示主键自增
@Id
@GeneratedValue
publicint getId() {
returnid;
}
publicvoid setId(int id) {
this.id= id;
}
publicString getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username= username;
}
publicString getPassword() {
returnpassword;
}
publicvoid setPassword(String password) {
this.password= password;
}
}
5建立dao,daoimpl
/***
*/
package org.lei.dao.impl;
import javax.annotation.Resource;
import org.lei.dao.UserDao;
import org.lei.model.User;
importorg.springframework.orm.hibernate3.HibernateTemplate;
importorg.springframework.stereotype.Component;
/**
*@author renlei
*@date 2014-3-19 上午10:19:30
*/
//(把普通userDao实例化到spring容器中,相当于配置文件中的<beanid="" class=""/>)
@Component("userDao")
public class UserDaoImpl implementsUserDao{
privateHibernateTemplate hibernateTemplate;
publicHibernateTemplate getHibernateTemplate() {
returnhibernateTemplate;
}
/*将hibernateTemplate通过spring进行注入*/
@Resource
publicvoid setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate= hibernateTemplate;
}
/*(non-Javadoc)
* @seeorg.lei.dao.UserDao#save(org.lei.model.User)
*/
@Override
publicboolean save(User user) {
//TODO Auto-generated method stub
hibernateTemplate.save(user);
returntrue;
}
}
6、构建UserManager和UserManagerImpl
/***
*/
package org.lei.service.impl;
import javax.annotation.Resource;
import org.lei.dao.UserDao;
import org.lei.model.User;
import org.lei.srvice.UserManager;
importorg.springframework.stereotype.Component;
/**
*@author renlei
*@date 2014-3-18 下午9:54:10
*/
/*(把普通userManagerImpl实例化到spring容器中,相当于配置文件中的<beanid="" class=""/>)*/
@Component("userManager")
public class UserManagerImpl implementsUserManager{
private UserDao userDao;
publicUserDao getUserDao() {
returnuserDao;
}
/*将userDao进行注入(即userDaoImple的实例)*/
@Resource(name="userDao")
publicvoid setUserDao(UserDao userDao) {
this.userDao= userDao;
}
/*(non-Javadoc)
* @seeorg.lei.srvice.UserManage#add(org.lei.model.User)
*/
@Override
publicboolean add(User user) {
userDao.save(user);
returntrue;
}
}
7、引入Struts2-Spring-Plugin,构建Action
/***
*/
package org.lei.action;
import javax.annotation.Resource;
import org.lei.model.User;
import org.lei.srvice.UserManager;
importorg.springframework.context.annotation.Scope;
importorg.springframework.stereotype.Component;
importcom.opensymphony.xwork2.ActionSupport;
/**
*@author renlei
*@date 2014-3-18 下午7:14:08
*/
/*声明componet和scope,但貌似这两个不起作用,去了也没有问题,整个action好像
都交给了插件在管理*/
@Component("registerAction")
@Scope("prototype")
public class RegisterAction extendsActionSupport{
/**
*
*/
privateUserManager userManager;
publicUserManager getUserManager() {
returnuserManager;
}
/*将userManager进行注入,这里是由Struts-Spring-plugin的插件注入的,不加@resource也可以
* 加了貌似没有起作用,默认去找一个userManager
* */
@Resource
publicvoid setUserManage(UserManager userManager) {
this.userManager= userManager;
}
privatestatic final long serialVersionUID = 1L;
privateString username;
privateString password;
publicString getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username= username;
}
publicString getPassword() {
returnpassword;
}
publicvoid setPassword(String password) {
this.password= password;
}
@Override
publicString execute() throws Exception {
//TODO Auto-generated method stub
Useruser = new User();
user.setUsername(username);
user.setPassword(password);
if(userManager.add(user));
returnSUCCESS;
}
}
8通过junit进行测试
/***
*/
package org.lei.action;
import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
/**
*@author renlei
*@date 2014-3-19 上午10:49:43
*/
public class RegisterActionTest {
/**
* @throws java.lang.Exception
*/
@Before
publicvoid setUp() throws Exception {
}
/**
* Test method for {@linkorg.lei.action.RegisterAction#execute()}.
*/
@Test
publicvoid testExecute() {
ApplicationContextctx = new ClassPathXmlApplicationContext("beans.xml");
RegisterActionua = (RegisterAction)ctx.getBean("registerAction");
try {
ua.execute();
}catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
//Assert.assertTrue(ua.getUsers().size()> 0);
}
}
下面是我当时配置好的工程的下载地址
ssh工程下载
相关文章推荐
- SSH开发环境的配置:web.xml、Spring集成hibernate和Spring集成Struts2
- Struts2,Hibernate,Spring 环境配置
- SSH(Struts2 + Hibernate + Spring)环境的搭建
- 【Java EE (Struts2 + Spring + Hibernate)开发】:开发环境配置(待续)
- 图解SSH(struts2,spring,hibernate)框架配置
- Struts2——SSH (Spring+Struts+Hibernate)框架搭建之配置文件序列一
- Eclipse 搭建struts2 spring3 hibernate3环境实战 待完善
- Spring整合Struts2和Hibernate+Maven(二)之SSH的配置文件
- eclipse配置Struts2、Hibernate3、Spring2.5范例
- eclipse配置Struts2、Hibernate3、Spring2.5范例
- Java Eclipse(mac)环境使用maven 加载spring,hibernate 及java的一些配置
- eclipse ssh插件 (struts2 spring3 hibernate4)
- eclipse配置使spring hibernate struts2 的xml节点自动提示
- Spring 3.1.0 + Hibernate 3.0 Eclipse IDE 环境的搭建
- 01-struts2之配置环境(Eclipse )
- SSH:Struts2与Spring整合核心配置及Jar包引用
- 非IDE方式手动配置struts2环境之hello_world
- struts2,hibernate4,spring3配置时问题汇总及解决办法
- struts2,hibernate4,spring3配置时问题汇总及解决办法
- Maven+Mybatis+Spring+eclipse环境配置