一、新建maven项目,配置ssh框架pom的最低支持

1、新建一个maven项目

2、添加一个web.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>limanman</display-name>
 
 
</web-app>

3、基本ssh框架pom.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.winssage.fengshu</groupId>
  <artifactId>winssage</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 
  <name>winssage</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
     <!-- 添加SSH依赖 -->  
    <!-- Struts2 -->  
    <dependency>  
        <groupId>org.apache.struts</groupId>  
        <artifactId>struts2-core</artifactId>  
        <version>2.3.1</version>  
    </dependency>  
       
    <dependency>  
        <groupId>org.apache.struts</groupId>  
        <artifactId>struts2-spring-plugin</artifactId>  
        <version>2.3.1</version>  
    </dependency>  
       
       
     <!-- 添加Hibernate依赖 -->  
    <dependency>  
        <groupId>org.hibernate</groupId>  
        <artifactId>hibernate-core</artifactId>  
        <version>3.6.5.Final</version>  
    </dependency>  
    <dependency>  
        <groupId>commons-dbcp</groupId>  
        <artifactId>commons-dbcp</artifactId>  
        <version>1.4</version>  
    </dependency>  
       
    <!-- 添加Log4J依赖 -->  
    <dependency>  
        <groupId>log4j</groupId>  
        <artifactId>log4j</artifactId>  
        <version>1.2.16</version>  
    </dependency>  
       
    <dependency>  
      <groupId>org.slf4j</groupId>  
      <artifactId>slf4j-api</artifactId>  
      <version>1.6.1</version>  
    </dependency>  
       
    <dependency>  
        <groupId>org.slf4j</groupId>  
        <artifactId>slf4j-nop</artifactId>  
        <version>1.6.4</version>  
    </dependency>  
       
    <!-- 添加javassist -->  
    <dependency>  
        <groupId>javassist</groupId>  
        <artifactId>javassist</artifactId>  
        <version>3.11.0.GA</version>  
    </dependency>  
       
    <!-- 添加Spring依赖 -->  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-core</artifactId>  
        <version>3.1.1.RELEASE</version>  
    </dependency>  
       
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-beans</artifactId>  
        <version>3.1.1.RELEASE</version>  
    </dependency>  
       
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-context</artifactId>  
        <version>3.1.1.RELEASE</version>  
    </dependency>  
       
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-jdbc</artifactId>  
        <version>3.1.1.RELEASE</version>  
    </dependency>  
       
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-orm</artifactId>  
        <version>3.1.1.RELEASE</version>  
    </dependency>  
       
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-web</artifactId>  
        <version>3.1.1.RELEASE</version>  
    </dependency>  
       
  </dependencies>
</project>

二、maven webproject 整合struts2

1、web.xml 加入中心拦截器配置

1
2
3
4
5
6
7
8
9
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
 
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2、加入log4j.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//Apache//DTD Log4j 1.2//EN"
                                    "http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd">
<log4j:configuration>
    <appender class="org.apache.log4j.ConsoleAppender" name="RootConsoleAppender">
        <param name="Threshold" value="debug" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n" />
        </layout>
    </appender>
 
    <category name="com.fengshu.limanman.winssage">
        <priority value="debug" />
    </category>
 
    <category name="org.springframework">
        <priority value="warn" />
    </category>
 
    <category name="org.hibernate">
        <priority value="warn" />
    </category>
    <category name="org.dbunit">
        <priority value="warn" />
    </category>
 
    <category name="com.mchange.v2">
        <priority value="warn" />
    </category>
    <category name="org.apache">
        <priority value="warn" />
    </category>
    <category name="org.logicalcobwebs">
        <priority value="warn" />
    </category>
    <category name="com.opensymphony">
        <priority value="warn" />
    </category>
 
    <root>
        <level value="info" />
        <appender-ref ref="RootConsoleAppender" />
    </root>
 
</log4j:configuration>

3、引入struts2.xml 根据约定由于配置原则,将此配置文件放在更目录即可

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="fengshu" namespace="/user" extends="struts-default">
 
        <!--<action name="user"
            class="com.fengshu.limanman.winssage.controller.LoginController">
            <result name="success">/pages/index.jsp</result>
        </action>-->
    </package>
</struts>

4、建一个测试jsp在webapp/pages/index.jsp

1
2
3
4
5
6
7
8
9
10
11
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>hello limanman
</body>
</html>

5、写一个controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.fengshu.limanman.controller;
 
import org.apache.log4j.Logger;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class LoginController extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private Logger log=Logger.getLogger(this.getClass());
     
    public String  login() {
        log.info("hello  i am limanman");
        return "succerss";
         
    }
 
}

6、配置struts2.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="fengshu" namespace="/" extends="struts-default">
 
        <action name="user" class="com.fengshu.limanman.controller.LoginController">
            <result name="success">/pages/index.jsp</result>
        </action>
    </package>
</struts>

7、暂时注释掉pom.xml中的struts2-spring-plugin

因为还没有整合spring,如果加入struts2-spring-plugin将会报空指针异常,整合spring后需要加入此jar包,下面将不再提及。

8、配置jetty运行环境,开发的时候我们一般使用jetty服务器,而不用tomcat

在pom.xml中加入jetty插件,并且配置根目录目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<build>
        <plugins>
            <!-- 配置加入jetty服务器,开发时我们一般使用jetty服务器 -->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <configuration>
                    <!-- 设置扫描target/classes内部文件变化时间间隔 -->
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/limanman</contextPath>
                    </webApp>
                </configuration>
            </plugin>
            <!-- compiler插件, 设定JDK版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
 
            <!-- war打包插件, 设定war包名称不带版本号 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warName>winssage</warName>
                </configuration>
            </plugin>
            <!-- eclipse插件, 设定wtp版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                    <wtpversion>2.0</wtpversion>
 
 
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 用于支持跨模块加载配置文件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>dll</nonFilteredFileExtension>
                        <nonFilteredFileExtension>so</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>
        </plugins>
    </build>

9、第一个里程碑,第一次运行项目

(1)jetty:run -Djetty.port=9080 -Dmaven.test.skip=true 跑起项目

(2)效果截图

三、将spring 整合进来

1、web.xml加入spring监听

1
2
3
4
5
6
7
8
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:META-INF/spring/ApplicationContext.xml</param-value>
    </context-param>
    <!-- Spring Listener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

2、将strut2交给spring管理

(1)修改strut2.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="fengshu" namespace="/" extends="struts-default">
 
        <action name="user" class="loginController">
            <result name="success">/pages/index.jsp</result>
        </action>
    </package>
</struts>

(2)添加spring配置文件

1
2
 <bean id="loginController" class="com.fengshu.limanman.controller.LoginController">
    </bean>

(3)运行 it is aok!

四、spring再整合hibernate

1、加入datasource

1
2
3
4
5
6
7
8
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver">
        </property>
        <property name="url" value="jdbc:mysql://localhost:3306/ddd">
        </property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

2、加入sessionFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.winssage.bslogic.core.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Mysql5Dialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

3、运行项目后报错

经检查发现是因为没有mysql驱动,也没有hibernate-anation 包

给改hibernate pom.xml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!-- 添加Hibernate依赖 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.2.6.ga</version>
            <type>jar</type>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>jta</artifactId>
                    <groupId>javax.transaction</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.2.0.ga</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>ejb3-persistence</artifactId>
            <version>1.0.0</version>
        </dependency>
         
<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.0.5</version>
</dependency>

4、再次启动

已自动更新了表,注意应该讲hibernate.hbm2ddl.auto设置为create才能自动创建表

到此hibernate已经成功添加进来

http://my.oschina.net/fengshuzi/blog/291421

基于maven的ssh框架一步一步搭建(一)的更多相关文章

  1. 基于maven的ssm框架整合

    基于maven的ssm框架整合 第一步:通过maven建立一个web项目.                第二步:pom文件导入jar包                              (1 ...

  2. 基于Maven的Spring + Spring MVC + Mybatis的环境搭建

    基于Maven的Spring + Spring MVC + Mybatis的环境搭建项目开发,先将环境先搭建起来.上次做了一个Spring + Spring MVC + Mybatis + Log4J ...

  3. Maven 整合 SSH 框架

    前面的一系列文章中,我们总结了三大框架:Struts2,Hibernate,Spring 的基本知识.本篇就姑且尝试着使用 Maven 这个项目构建工具来将这三个框架整合一起.说到这里,如果有对 Ma ...

  4. maven项目ssh框架的整合

    1.环境 eclipse版本:Eclipse Mars2 4.5jdk版本:1.8maven版本:apache-maven 3.3.9zhnegs这是主要的开发工具版本,ssh的各种jar包版本就不列 ...

  5. Maven 整合SSH框架

    1. 传递依赖冲突 1.1 传递依赖:A(项目)依赖B,B依赖C(1.1版本),B是A的直接依赖,C是A的传递依赖; A(项目)又依赖D,D依赖C(1.2版本),此时,C有两个版本,产生冲突; 1.2 ...

  6. maven学习记录三——maven整合ssh框架

    6       整合ssh框架 6.1     依赖传递 只添加了一个struts2-core依赖,发现项目中出现了很多jar, 这种情况 叫 依赖传递 6.2     依赖版本冲突的解决 1.  第 ...

  7. maven整合ssh框架笔记

    具体工程会上传文件sshpro <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:x ...

  8. 使用 IDEA和Maven 整合SSH框架

    1.创建web工程 一路next 下去就行.完成后,IDEA会自动构建maven工程. 2.创建如下项目结构 需要将 java文件夹设置为SourcesRoot目录,否则无法创建package 设置操 ...

  9. 基于Maven的SSM框架搭建

    Maven + Spring + Spring MVC + Mybatis + MySQL整合SSM框架 1.数据库准备 本文主要想实现SSM框架的搭建,并基于该框架实现简单的登录功能,那么先新建一张 ...

随机推荐

  1. 关于热插拔usb hotplug /proc/sys/kernel mdev udev b...

    转:http://www.360doc.com/content/10/0527/18/9922_29835045.shtml 这篇文章说的很好http://blog.chinaunix.net/u1/ ...

  2. TStringList的DelimitedText的空格问题

    TStringList的DelimitedText的空格问题 如果DelimitedText的字符串中有空格,TStringList遇到空格会进行自动换行,而这显然不是我们想要的结果.经测试发现D7~ ...

  3. jquery避免跟其他库冲突

    方法一: var $j=JQuery.noConflict(); $j('#msg').hide();//此处$j就代表JQuery 方法二: JQuery.noConflict(); JQuery( ...

  4. class"org.apache.commons.dbcp.BasicDataSource"not found出错的解决办法

    法1:在项目上右击build path->configure build path..->add library..->MyEclipse Libraries选中Spring 2.0 ...

  5. 二十四种设计模式:单例模式(Singleton Pattern)

    单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using S ...

  6. iOS:多线程NSThread的详细使用

    NSThread具体使用:直接继承NSObject NSThread:. 优点:NSThread 是轻量级的,使用简单 缺点:需要自己管理线程的生命周期.线程同步.线程同步对数据的加锁会有一定的系统开 ...

  7. C# 中的单精度与双精度区别

    单精度浮点数(float)与双精度浮点数(double)的区别: (1)在内存中占有的字节数不同 * 单精度浮点数在机内占4个字节 *双精度浮点数在机内占8个字节 (2)有效数字位数不同 *单精度浮点 ...

  8. 关于json对象的删除

    摘自:http://xosadan.iteye.com/blog/1100383 关于json对象的删除 一个json对象在后台产生了,但是有些数据可能无效或者不合法,所以需要在前台作些例外处理,比如 ...

  9. ubuntu 不是 识别 android 设备 解决方法

    ubuntu: 在终端输入lsusb: langu@langu:~$ lsusb Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root ...

  10. 7个高级技巧帮助你释放大量Mac OS X硬盘空间

    7个高级技巧帮助你释放大量Mac OS X硬盘空间 https://blog.csdn.net/hu434587115/article/details/72874811/