eclipse maven 搭建【SSH】项目



一、新建maven项目

 

eclipse maven 搭建【SSH】项目_第1张图片

 

选中maven-aechetype-webapp

eclipse maven 搭建【SSH】项目_第2张图片

group Id一般为公司域 ;Artifact Id相当于项目名称 ;version不需要更改;Package不使用默认,手动清空

Finish

项目结构如图 报错是因为没有加入servlet API 暂时不用管。

eclipse maven 搭建【SSH】项目_第3张图片

二、配置pom.xml

复制代码
  1    <properties>
  2          
  3         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  4         
  5         <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
  6         
  7         <maven.compiler.encoding>UTF-8maven.compiler.encoding>
  8    properties>
  9       <dependencies>
 10         
 11         <dependency>
 12             <groupId>log4jgroupId>
 13             <artifactId>log4jartifactId>
 14             <version>1.2.17version>
 15         dependency>
 16         
 17         <dependency>
 18             <groupId>javax.servletgroupId>
 19             <artifactId>javax.servlet-apiartifactId>
 20             <version>3.1-b09version>
 21             <scope>providedscope>
 22         dependency>
 23         
 24         <dependency>
 25             <groupId>mysqlgroupId>
 26             <artifactId>mysql-connector-javaartifactId>
 27             <version>5.1.24version>
 28         dependency>
 29         
 30         <dependency>
 31             <groupId>com.mchangegroupId>
 32             <artifactId>c3p0artifactId>
 33             <version>0.9.5-pre2version>
 34         dependency>
 35         
 36         <dependency>
 37             <groupId>junitgroupId>
 38             <artifactId>junitartifactId>
 39             <version>4.11version>
 40             <scope>testscope>
 41         dependency>
 42         
 43         <dependency>
 44             <groupId>com.alibabagroupId>
 45             <artifactId>fastjsonartifactId>
 46             <version>1.1.29version>
 47         dependency>
 48         
 49         <dependency>
 50             <groupId>net.sf.json-libgroupId>
 51             <artifactId>json-libartifactId>
 52             <version>2.4version>
 53             <classifier>jdk15classifier>
 54         dependency>
 55         
 56         <dependency>
 57             <groupId>org.apache.strutsgroupId>
 58             <artifactId>struts2-coreartifactId>
 59             <version>2.3.14version>
 60             <exclusions>
 61                 <exclusion>
 62                     <groupId>javassistgroupId>
 63                     <artifactId>javassistartifactId>
 64                 exclusion>
 65             exclusions>
 66         dependency>
 67         <dependency>
 68             <groupId>org.apache.strutsgroupId>
 69             <artifactId>struts2-spring-pluginartifactId>
 70             <version>2.3.14version>
 71         dependency>
 72         <dependency>
 73             <groupId>org.apache.strutsgroupId>
 74             <artifactId>struts2-convention-pluginartifactId>
 75             <version>2.3.14version>
 76         dependency>
 77         
 78         <dependency>
 79             <groupId>org.springframeworkgroupId>
 80             <artifactId>spring-coreartifactId>
 81             <version>3.2.2.RELEASEversion>
 82         dependency>
 83         <dependency>
 84             <groupId>org.springframeworkgroupId>
 85             <artifactId>spring-contextartifactId>
 86             <version>3.2.2.RELEASEversion>
 87         dependency>
 88         <dependency>
 89             <groupId>org.springframeworkgroupId>
 90             <artifactId>spring-context-supportartifactId>
 91             <version>3.2.2.RELEASEversion>
 92         dependency>
 93         <dependency>
 94             <groupId>org.springframeworkgroupId>
 95             <artifactId>spring-jdbcartifactId>
 96             <version>3.2.2.RELEASEversion>
 97         dependency>
 98         <dependency>
 99             <groupId>org.springframeworkgroupId>
100             <artifactId>spring-beansartifactId>
101             <version>3.2.2.RELEASEversion>
102         dependency>
103         <dependency>
104             <groupId>org.springframeworkgroupId>
105             <artifactId>spring-webartifactId>
106             <version>3.2.2.RELEASEversion>
107         dependency>
108         <dependency>
109             <groupId>org.springframeworkgroupId>
110             <artifactId>spring-expressionartifactId>
111             <version>3.2.2.RELEASEversion>
112         dependency>
113         <dependency>
114             <groupId>org.springframeworkgroupId>
115             <artifactId>spring-ormartifactId>
116             <version>3.2.2.RELEASEversion>
117         dependency>
118         
119         <dependency>
120             <groupId>org.aspectjgroupId>
121             <artifactId>aspectjweaverartifactId>
122             <version>1.7.2version>
123         dependency>
124         
125         <dependency>
126             <groupId>org.hibernategroupId>
127             <artifactId>hibernate-coreartifactId>
128             <version>4.2.0.Finalversion>
129         dependency>
130     dependencies>
复制代码

保存,Maven会自动联网去下载所有需要的jar包,只写jar的名字,不用记住,只要去Maven中央仓库去搜索,Maven中央仓库地址:http://search.maven.org/完成之后,多了很多jar,并且都放到了配置的本地仓库中,以后要再使用相同版本的jar时候,就不需要联网下载了。

 三、修改项目结构

因为构建的MSSH项目的结构并不是一个标准的Maven结构,我们需要手动新建几个Source Folder

eclipse maven 搭建【SSH】项目_第4张图片

这是标准的maven项目结构

src/main/java:存放java源文件

src/main/resources:存放项目配置文件,如spring.xml,hibernate.cfg.xml。。。

src/test/java:存放test的java文件

src/test/resources:存放test时候所需的配置文件

 

配置Source Folder的output folder

eclipse maven 搭建【SSH】项目_第5张图片

同步项目的jdk版本

eclipse maven 搭建【SSH】项目_第6张图片

eclipse maven 搭建【SSH】项目_第7张图片eclipse maven 搭建【SSH】项目_第8张图片

设置web.xml的路径

eclipse maven 搭建【SSH】项目_第9张图片

further configuration available若没有出现 可以先去掉dynamic web module的勾 apply之后再打上勾 就会出现此选项 设置路径为src/main/webapp

四、编写配置文件

web.xml

复制代码
 1 xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 3   <display-name>display-name>
 4   
 5   
 7   <context-param>
 8     <param-name>contextConfigLocationparam-name>
 9     <param-value>classpath:spring.xml,classpath:spring-hibernate.xmlparam-value>
10   context-param>
11   
12   
13   <listener>
14     <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
15   listener>
16   
17   
18   
19   <filter>
20     <filter-name>encodingFilterfilter-name>
21     <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
22     <init-param>
23       <param-name>encodingparam-name>
24       <param-value>UTF-8param-value>
25     init-param>
26   filter>
27   <filter-mapping>
28     <filter-name>encodingFilterfilter-name>
29     <url-pattern>/*url-pattern>
30   filter-mapping>
31   
32   <filter>
33     <filter-name>openSessionInViewfilter-name>
34     <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilterfilter-class>
35   filter>
36   <filter-mapping>
37     <filter-name>openSessionInViewfilter-name>
38     <url-pattern>/*url-pattern>
39   filter-mapping>
40   
41   
42   <filter>
43     <filter-name>struts2filter-name>
44     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
45   filter>
46   <filter-mapping>
47     <filter-name>struts2filter-name>
48     <url-pattern>/*url-pattern>
49   filter-mapping>
50   
51   
52   <welcome-file-list>
53     <welcome-file>login.jspwelcome-file>
54   welcome-file-list>
55 web-app>
复制代码

database.properties

复制代码
hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
url=jdbc:mysql://localhost:3306/mssh?useUnicode=true&characterEncoding=UTF-8
username=root
password=root


hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false
复制代码

spring.xml

扫描com.shi包下的文件

复制代码
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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">

    
    <context:property-placeholder location="classpath:database.properties" />

    
    <context:component-scan base-package="com.shi" />
    
beans>
复制代码

spring-hibernate.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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    ">
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        
        <property name="driverClass" value="${driverClassName}">property>
        <property name="jdbcUrl" value="${url}">property>
        <property name="user" value="${username}">property>
        <property name="password" value="${password}">property>
        <property name="maxPoolSize" value="40">property>
        <property name="minPoolSize" value="1">property>
        <property name="initialPoolSize" value="1">property>
        <property name="maxIdleTime" value="20">property>
    bean>

    
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.shi.*.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}prop>
                <prop key="hibernate.dialect">${hibernate.dialect}prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}prop>
            props>
        property>
    bean>

    
    <bean name="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>
    <tx:annotation-driven transaction-manager="txManager" />


beans>
复制代码

struts.xml

使用零配置,扫描com.shi包下的文件

复制代码
xml version="1.0" encoding="UTF-8"?>
DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    
    <constant name="struts.objectFactory" value="spring" />
    
    
    <constant name="struts.convention.package.locators" value="shi" />
    <constant name="struts.convention.package.locators.basePackage" value="com" />
    
    
    <constant name="struts.devMode" value="true" />
    <constant name="struts.configuration.xml.reload" value="true" />
    
    <constant name="struts.i18n.encoding" value="utf-8" />
    <package name="defaultPackage" namespace="/" extends="struts-default">

    package>
struts>
复制代码

 五、使用

  User
  UserDao
  UserManager
  UserAction

一、新建maven项目

 

eclipse maven 搭建【SSH】项目_第10张图片

 

选中maven-aechetype-webapp

eclipse maven 搭建【SSH】项目_第11张图片

group Id一般为公司域 ;Artifact Id相当于项目名称 ;version不需要更改;Package不使用默认,手动清空

Finish

项目结构如图 报错是因为没有加入servlet API 暂时不用管。

eclipse maven 搭建【SSH】项目_第12张图片

二、配置pom.xml

复制代码
  1    <properties>
  2          
  3         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  4         
  5         <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
  6         
  7         <maven.compiler.encoding>UTF-8maven.compiler.encoding>
  8    properties>
  9       <dependencies>
 10         
 11         <dependency>
 12             <groupId>log4jgroupId>
 13             <artifactId>log4jartifactId>
 14             <version>1.2.17version>
 15         dependency>
 16         
 17         <dependency>
 18             <groupId>javax.servletgroupId>
 19             <artifactId>javax.servlet-apiartifactId>
 20             <version>3.1-b09version>
 21             <scope>providedscope>
 22         dependency>
 23         
 24         <dependency>
 25             <groupId>mysqlgroupId>
 26             <artifactId>mysql-connector-javaartifactId>
 27             <version>5.1.24version>
 28         dependency>
 29         
 30         <dependency>
 31             <groupId>com.mchangegroupId>
 32             <artifactId>c3p0artifactId>
 33             <version>0.9.5-pre2version>
 34         dependency>
 35         
 36         <dependency>
 37             <groupId>junitgroupId>
 38             <artifactId>junitartifactId>
 39             <version>4.11version>
 40             <scope>testscope>
 41         dependency>
 42         
 43         <dependency>
 44             <groupId>com.alibabagroupId>
 45             <artifactId>fastjsonartifactId>
 46             <version>1.1.29version>
 47         dependency>
 48         
 49         <dependency>
 50             <groupId>net.sf.json-libgroupId>
 51             <artifactId>json-libartifactId>
 52             <version>2.4version>
 53             <classifier>jdk15classifier>
 54         dependency>
 55         
 56         <dependency>
 57             <groupId>org.apache.strutsgroupId>
 58             <artifactId>struts2-coreartifactId>
 59             <version>2.3.14version>
 60             <exclusions>
 61                 <exclusion>
 62                     <groupId>javassistgroupId>
 63                     <artifactId>javassistartifactId>
 64                 exclusion>
 65             exclusions>
 66         dependency>
 67         <dependency>
 68             <groupId>org.apache.strutsgroupId>
 69             <artifactId>struts2-spring-pluginartifactId>
 70             <version>2.3.14version>
 71         dependency>
 72         <dependency>
 73             <groupId>org.apache.strutsgroupId>
 74             <artifactId>struts2-convention-pluginartifactId>
 75             <version>2.3.14version>
 76         dependency>
 77         
 78         <dependency>
 79             <groupId>org.springframeworkgroupId>
 80             <artifactId>spring-coreartifactId>
 81             <version>3.2.2.RELEASEversion>
 82         dependency>
 83         <dependency>
 84             <groupId>org.springframeworkgroupId>
 85             <artifactId>spring-contextartifactId>
 86             <version>3.2.2.RELEASEversion>
 87         dependency>
 88         <dependency>
 89             <groupId>org.springframeworkgroupId>
 90             <artifactId>spring-context-supportartifactId>
 91             <version>3.2.2.RELEASEversion>
 92         dependency>
 93         <dependency>
 94             <groupId>org.springframeworkgroupId>
 95             <artifactId>spring-jdbcartifactId>
 96             <version>3.2.2.RELEASEversion>
 97         dependency>
 98         <dependency>
 99             <groupId>org.springframeworkgroupId>
100             <artifactId>spring-beansartifactId>
101             <version>3.2.2.RELEASEversion>
102         dependency>
103         <dependency>
104             <groupId>org.springframeworkgroupId>
105             <artifactId>spring-webartifactId>
106             <version>3.2.2.RELEASEversion>
107         dependency>
108         <dependency>
109             <groupId>org.springframeworkgroupId>
110             <artifactId>spring-expressionartifactId>
111             <version>3.2.2.RELEASEversion>
112         dependency>
113         <dependency>
114             <groupId>org.springframeworkgroupId>
115             <artifactId>spring-ormartifactId>
116             <version>3.2.2.RELEASEversion>
117         dependency>
118         
119         <dependency>
120             <groupId>org.aspectjgroupId>
121             <artifactId>aspectjweaverartifactId>
122             <version>1.7.2version>
123         dependency>
124         
125         <dependency>
126             <groupId>org.hibernategroupId>
127             <artifactId>hibernate-coreartifactId>
128             <version>4.2.0.Finalversion>
129         dependency>
130     dependencies>
复制代码

保存,Maven会自动联网去下载所有需要的jar包,只写jar的名字,不用记住,只要去Maven中央仓库去搜索,Maven中央仓库地址:http://search.maven.org/完成之后,多了很多jar,并且都放到了配置的本地仓库中,以后要再使用相同版本的jar时候,就不需要联网下载了。

 三、修改项目结构

因为构建的MSSH项目的结构并不是一个标准的Maven结构,我们需要手动新建几个Source Folder

eclipse maven 搭建【SSH】项目_第13张图片

这是标准的maven项目结构

src/main/java:存放java源文件

src/main/resources:存放项目配置文件,如spring.xml,hibernate.cfg.xml。。。

src/test/java:存放test的java文件

src/test/resources:存放test时候所需的配置文件

 

配置Source Folder的output folder

eclipse maven 搭建【SSH】项目_第14张图片

同步项目的jdk版本

eclipse maven 搭建【SSH】项目_第15张图片

eclipse maven 搭建【SSH】项目_第16张图片eclipse maven 搭建【SSH】项目_第17张图片

设置web.xml的路径

eclipse maven 搭建【SSH】项目_第18张图片

further configuration available若没有出现 可以先去掉dynamic web module的勾 apply之后再打上勾 就会出现此选项 设置路径为src/main/webapp

四、编写配置文件

web.xml

复制代码
 1 xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 3   <display-name>display-name>
 4   
 5   
 7   <context-param>
 8     <param-name>contextConfigLocationparam-name>
 9     <param-value>classpath:spring.xml,classpath:spring-hibernate.xmlparam-value>
10   context-param>
11   
12   
13   <listener>
14     <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
15   listener>
16   
17   
18   
19   <filter>
20     <filter-name>encodingFilterfilter-name>
21     <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
22     <init-param>
23       <param-name>encodingparam-name>
24       <param-value>UTF-8param-value>
25     init-param>
26   filter>
27   <filter-mapping>
28     <filter-name>encodingFilterfilter-name>
29     <url-pattern>/*url-pattern>
30   filter-mapping>
31   
32   <filter>
33     <filter-name>openSessionInViewfilter-name>
34     <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilterfilter-class>
35   filter>
36   <filter-mapping>
37     <filter-name>openSessionInViewfilter-name>
38     <url-pattern>/*url-pattern>
39   filter-mapping>
40   
41   
42   <filter>
43     <filter-name>struts2filter-name>
44     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
45   filter>
46   <filter-mapping>
47     <filter-name>struts2filter-name>
48     <url-pattern>/*url-pattern>
49   filter-mapping>
50   
51   
52   <welcome-file-list>
53     <welcome-file>login.jspwelcome-file>
54   welcome-file-list>
55 web-app>
复制代码

database.properties

复制代码
hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
url=jdbc:mysql://localhost:3306/mssh?useUnicode=true&characterEncoding=UTF-8
username=root
password=root


hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false
复制代码

spring.xml

扫描com.shi包下的文件

复制代码
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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">

    
    <context:property-placeholder location="classpath:database.properties" />

    
    <context:component-scan base-package="com.shi" />
    
beans>
复制代码

spring-hibernate.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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    ">
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        
        <property name="driverClass" value="${driverClassName}">property>
        <property name="jdbcUrl" value="${url}">property>
        <property name="user" value="${username}">property>
        <property name="password" value="${password}">property>
        <property name="maxPoolSize" value="40">property>
        <property name="minPoolSize" value="1">property>
        <property name="initialPoolSize" value="1">property>
        <property name="maxIdleTime" value="20">property>
    bean>

    
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.shi.*.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}prop>
                <prop key="hibernate.dialect">${hibernate.dialect}prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}prop>
            props>
        property>
    bean>

    
    <bean name="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>
    <tx:annotation-driven transaction-manager="txManager" />


beans>
复制代码

struts.xml

使用零配置,扫描com.shi包下的文件

复制代码
xml version="1.0" encoding="UTF-8"?>
DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    
    <constant name="struts.objectFactory" value="spring" />
    
    
    <constant name="struts.convention.package.locators" value="shi" />
    <constant name="struts.convention.package.locators.basePackage" value="com" />
    
    
    <constant name="struts.devMode" value="true" />
    <constant name="struts.configuration.xml.reload" value="true" />
    
    <constant name="struts.i18n.encoding" value="utf-8" />
    <package name="defaultPackage" namespace="/" extends="struts-default">

    package>
struts>
复制代码

 五、使用

  User
  UserDao
  UserManager
  UserAction

你可能感兴趣的:(eclipse maven 搭建【SSH】项目)