昨天想配个ssh的环境,然后一看都是在myclipse 下配置的,大多数都是在工程里面引用不是自己自己加包配置。
所以自己试了一次,最大的感触就是千万别忘了一些小的配置文件,不然不报错,真是难找错误。。。。。下面是我配的过程,和我对一下东西的理解,希望对大家有帮助
目录
2.2.1首先构建/src下jdbc.properties.4
6、构建UserManager和UserManagerImpl10
7、引入Struts2-Spring-Plugin,构建Action.11
1、配置Struts
1.1、最基本的jar包
1.2建立RegisterAction.java
1.3、配置web.xml
Struts版本不一样,所使用的filter是不一样的
Changed Filter Structure in 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.Driver
jdbc.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);
}
}
下面是我当时配置好的工程的下载地址