idea使用maven创建java web项目

maven安装

顺序不太对,按自己的思路顺序搭建

1. 下载maven
2. 配置环境变量

idea使用maven创建java web项目_第1张图片

idea使用maven创建java web项目_第2张图片

3.配置本地仓库
在maven的安装目录中conf/settings.xml文件配置本地仓库
idea使用maven创建java web项目_第3张图片
配置IntelliJ
在IntelliJ的设置中,可以设置maven的安装目录,settings.xml文件的位置,和本地仓库的位置等信息。
idea使用maven创建java web项目_第4张图片

新建项目

创建父工程
file-maven-跳过原型选择-创建
idea使用maven创建java web项目_第5张图片
子工程:
ctrl+shift+alt+s:添加子模块
idea使用maven创建java web项目_第6张图片
原型选择:web层用war(web app),service层和dao层用jar(quickstart)
idea使用maven创建java web项目_第7张图片
idea使用maven创建java web项目_第8张图片
maven模板项目结构
每个module都需要创建五个目录
idea使用maven创建java web项目_第9张图片
idea使用maven创建java web项目_第10张图片

配置Tomcat容器

打开菜单Run -> 选择Edit Configuration
idea使用maven创建java web项目_第11张图片
点击“+”号 -> 选择“Tomcat Server” -> 选择“Local”
idea使用maven创建java web项目_第12张图片
在Tomcat中部署并运行项目

Run -> Edit Configurations,进入”Run/Debug Configurations”窗口 -> 选择刚刚建立的Tomcat容器 -> 选择Deployment -> 点击右边的“+”号 -> 选择Artifact
idea使用maven创建java web项目_第13张图片
打开Project Structure(快捷键:Ctrl+Shift+Alt+s)
idea使用maven创建java web项目_第14张图片

dao层的web.xml设置

默认的版本是2.3,更改为2.5


<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">

  
  <listener>
    
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  <context-param>
    <param-name>contextConfigLocationparam-name>
    
    <param-value>classpath:spring/applicationContext.xml,classpath:spring/applicationContext-*.xmlparam-value>
  context-param>

  
  <filter>
    <filter-name>OpenSessionInViewfilter-name>
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilterfilter-class>
  filter>

  <filter-mapping>
    <filter-name>OpenSessionInViewfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>


  
  
  <filter>
    <filter-name>struts2filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
  filter>
  <filter-mapping>
    <filter-name>struts2filter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>


  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>

web-app>

问题
Description Resource Path Location Type
Cannot change version of project facet Dynamic Web Module to 2.5. maven-ssh0120 line 1 Maven Java EE Configuration Problem
找到工程目录:
idea使用maven创建java web项目_第15张图片
修改:这里写图片描述
idea使用maven创建java web项目_第16张图片

pom.xml设置

基本配置:
每个maven都有一个pom.xml,pom.xml在工程根目录。

gropuid:项目名称
artifactid:模块名称(子项目名称)
version:版本
packaging:打包方式,jar(java工程)、war(web工程)、pom(作为父工程)
dependencies:依赖管理,配置本工程所依赖jar包(构件)
build:工程构建配置,配置插件

设置编译版本
idea使用maven创建java web项目_第17张图片
或者让这三个地方一致
Project Structure里确认两个地方:Project sdk以及project language level

Project Structure->Modules里Sources里的Language level

Preferences->Java Compiler->Per-module bytecode Version

定义父工程的dom.xml:

<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.0modelVersion>

    <groupId>cn.itcast.mavengroupId>
    <artifactId>maven-sshartifactId>
    <packaging>warpackaging>
    <version>0.0.1-SNAPSHOTversion>
    <name>maven-ssh Maven Webappname>
    <url>http://maven.apache.orgurl>
    
    <properties>
        <spring.version>4.2.4.RELEASEspring.version>
        <hibernate.version>5.0.7.Finalhibernate.version>
        <struts.version>2.3.24struts.version>
    properties>
    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-ormartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.hibernategroupId>
                <artifactId>hibernate-coreartifactId>
                <version>${hibernate.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.strutsgroupId>
                <artifactId>struts2-coreartifactId>
                <version>${struts.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.strutsgroupId>
                <artifactId>struts2-spring-pluginartifactId>
                <version>${struts.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.strutsgroupId>
                <artifactId>struts2-json-pluginartifactId>
                <version>${struts.version}version>
            dependency>
        dependencies>
    dependencyManagement>
    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-coreartifactId>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
            <scope>runtimescope>
        dependency>
        

        <dependency>
            <groupId>c3p0groupId>
            <artifactId>c3p0artifactId>
            <version>0.9.1.2version>
        dependency>
        
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-coreartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-spring-pluginartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-json-pluginartifactId>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.0version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>1.7.2version>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.9version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
    dependencies>
    <build>
        <finalName>maven-sshfinalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
        plugins>
    build>
      
  <properties>
    <argLine>-Dfile.encoding=UTF-8argLine>
  properties>
project>

定义dao的dom.xml

<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.0modelVersion>
  <parent>
    <groupId>cn.itcast.mavengroupId>
    <artifactId>maven-parentartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>maven-daoartifactId>
  <packaging>jarpackaging>
  <dependencies>
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-coreartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
        dependency>

        

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
            <scope>runtimescope>
        dependency>
        

        <dependency>
            <groupId>c3p0groupId>
            <artifactId>c3p0artifactId>
            <version>0.9.1.2version>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>1.7.2version>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.9version>
            <scope>testscope>
        dependency>
    dependencies>

project>

定义service的dom.xml

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.0modelVersion>
  <parent>
    <groupId>cn.itcast.mavengroupId>
    <artifactId>maven-parentartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>maven-serviceartifactId>
  <packaging>jarpackaging>

  <dependencies>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.9version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>cn.itcast.mavengroupId>
            <artifactId>maven-daoartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
    dependencies>

project>

定义web的dom.xml

<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.0modelVersion>
  <parent>
    <groupId>cn.itcast.mavengroupId>
    <artifactId>maven-parentartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>maven-webartifactId>
  <packaging>warpackaging>
  <name>web工程name>

   <dependencies>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.9version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>cn.itcast.mavengroupId>
            <artifactId>maven-serviceartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>


        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-coreartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-spring-pluginartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-json-pluginartifactId>
        dependency>

                
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.0version>
            <scope>providedscope>
        dependency>
                
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
    dependencies>


project>

参考:
【java】intellij idea 超详细图解从0搭建SSM框架
Intellij Idea 创建Web项目并部署servlet
使用IntelliJ IDEA 配置Maven(入门)

你可能感兴趣的:(idea)