groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
比如我创建一个项目,我一般会将groupId设置为cn.zr,cn表示域为中国,zr是我个人姓名缩写,artifactId设置为testProj,表示你这个项目的名称是testProj,依照这个设置,你的包结构最好是cn.zr.testProj打头的,如果有个StudentDao,它的全路径就是cn.zr.testProj.dao.StudentDao。
参考后创建GroupId为:com.weixin
ArtifactId为:myapp
ArtifactId即工程名
——————————————————————————————
配置pom.xml中的 依赖:
中途IDEAvim插件阻止了正常的复制粘贴等一系列问题,在File——settings——Plugins 搜索它 并关闭即可。
配置依赖 在这个网 找相应的版本就好 http://mvnrepository.com/
需要配置的依赖有(直接搜下面写的,这些都是依赖中的artifactId):
junit,struts2-core,struts2-spring-plugin,hibernate-core,spring-core,spring-beans,spring-orm,spring-web,spring-context,mysql-connector-java,c3p0(groupid变更成了com.mchange),commons-dbcp(groupid变成了org.apache.commons,artifactId变成了commons-dbcp2。。这里我用新版尝试 ),log4j(groupid变成了 org.apache.logging.log4j ,artifactId变成了 log4j-core 。。这里用新版尝试),slf4j-api,slf4j-nop,javassist。
pom.xml文件如下:
二:开始整合struts2:
在src.main下添加一个webapp的文件夹,然后在里面添加一个WEB-INF的文件夹,并在里面新增web.xml文件。
IDEA 对齐快捷键:ctrl+alt+l键,如果与qq快捷键锁定相冲突要修改。
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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.1web.xml 加入Struts2的中心拦截器配置以及Spring的监听器:
然后新建struts.xml要放置在src下,以及新建applicationContext.xml
然后新建包结构如下:
整合Struts2与Spring:
先看看启动是否可以成功:
将项目发布到tomcat启动时,遇到了将前台资源index.jsp中内容改变在网页中访问却没有实时更新,只能重新启动tomcat服务器才更新,原因是:
在tomcat 配置中Server下 On frame deactivation的选项应改成 Update classes and resources 才可以。
已经启动成功。
开始整合:1.在action,service,dao层分别创建LoginAction,LoginService,LoginDao类。
2.在applicationContext.xml中配置
<bean id ="loginService" class="com.weixin.service.LoginService">
<property name="loginDao" ref="loginDao"></property>
</bean>
<bean id ="loginDao" class="com.weixin.dao.LoginDao">
</bean>
并且要在 LoginService类中 加入LoginDao类,并加入set方法
public class LoginService {
private LoginDao loginDao;
public void setLoginDao(LoginDao loginDao) {
this.loginDao = loginDao;
}
}
将Action交给Spring管理:在applicationContext.xml中对Action进行配置,然后在LoginAction类中加入LoginService类,并加入set方法,其中让LoginAction继承SupportAction并实现ModelDriven接口,并新建一个User类:
public class LoginAction extends ActionSupport implements ModelDriven<User> {
private LoginService loginService;
private User user = new User();
public User getModel() {
return null;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public String login(){
System.out.println("已经成功执行loginAction");
loginService.save();
return NONE;
}
}
并在User类中定义uid(Integer),uname(String),upswd(String),并提供相应的get与set方法。
如果要使用通配符来写Action配置,这里要注意2.5版本以后 要在里面加入action里面加入allowed-methods这个参数,参数里面填写通配的名称。
struts.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<package name="login" extends="struts-default" namespace="/">
<action name="login_*" class="loginAction" method="{1}">
<allowed-methods>login</allowed-methods>
</action>
</package>
在jsp页面中加入struts2的标签:<%@ taglib prefix="s" uri="/struts-tags" %>
index.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<s:form action="login_login" method="POST" namespace="/">
<table border="1" width="400">
<tr>
<td>用户名称</td>
<td><input type="text"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text"></td>
</tr>
<tr colspan="2"><input type="submit" value="注册"></tr>
</table>
</s:form>
</body>
</html>
发现整合成功了:
中间可能会产生问题,找不到Action配置之类的,可能原因是因为:
struts.xml文件放置路径错误。一定要将此文件放置在src目录下。编译成功后,要确认是否编译到classes目录中;
我们可以在生成的out文件的classes中查找是否有struts.xml,如果没有就是因为这个问题:
接下来我们来整合Spring与Hibernate:
1.我们先新建一个数据库,
下面这句是建表以及字段语句:
create table user (uid int(11),primary key not null auto_increment,uname varchar(32),upswd varchar(32));
将hibernate连接数据库的信息 交给Spring来管理
建好数据库与表和字段以后,对ApplicationContext.xml中要进行Hibernate相关的配置
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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<!--引入外部文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置Hibernate的相关属性-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--注入连接池-->
<property name="dataSource" ref="dataSource"></property>
<!--配置Hibernate属性-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--加载Hibernate映射文件-->
<property name="mappingResources">
<list>
<value>User.hbm.xml</value>
</list>
</property>
</bean>
<bean id ="loginAction" class="com.weixin.action.LoginAction">
<property name="loginService" ref="loginService"></property>
</bean>
<bean id ="loginService" class="com.weixin.service.LoginService">
<property name="loginDao" ref="loginDao"></property>
</bean>
<bean id ="loginDao" class="com.weixin.dao.LoginDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--开启注解事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
对建好了的数据,通过IDEA反向生成实体映射以及映射xml文件,首先要用IDEA连接数据库,然后在Persistence视图里面对数据库的表进行映射,其中要注意如果要生成hbm.xml文件必须勾上OK上面的这个勾,下面一共三个勾。
这些完成了以后,在dao包中建立一个IBaseDao接口,并建一个它的实现类BaseDao的基类,其中BaseDao中要注入SessionFactory(提供set与get方法),然后在bean id=loginDao中加入sessionFactory,如
<property name="sessionFactory" ref="sessionFactory"/>
注意要将所有的Dao都继承这个BaseDao基类,这样都会有了sessionFactory。
然后要配置事物管理器
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--开启注解事务--> <tx:annotation-driven transaction-manager="transactionManager"/>接着要在业务层添加注解才行:
@Transactional
user表中数据清除用 delete from user where 1=1; 可以删除所有的记录。
目前已经完成了ssh的环境搭建。
下面是项目文件的压缩包以及数据库表结构信息,将它发布到github上面:
未解决问题:1.log4j
2.包分级显示