IDEA 創建maven構建的spring mvc 項目 過程

一 創建java web項目

 

二. 添加spring mvc 框架支持

 

web.xml 內容如下;

 

 

再次運行,報如下錯誤

[2019-06-16 02:58:12,486] Artifact springmvc:war exploded: Artifact is being deployed, please wait...
16-Jun-2019 02:58:12.690 警告 [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]. Default version will be used.
16-Jun-2019 02:58:12.791 嚴重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file
16-Jun-2019 02:58:12.793 嚴重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors

 

編輯web.xml文件

再次運行,報如下錯誤

[2019-06-16 03:01:57,633] Artifact springmvc:war exploded: Artifact is being deployed, please wait...
16-Jun-2019 03:01:57.925 嚴重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file
16-Jun-2019 03:01:57.927 嚴重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors
[2019-06-16 03:01:57,939] Artifact springmvc:war exploded: Error during artifact deployment. See server log for details.

修改方法

 

 

注意右下角的apply與ok要點的

修改後再次運行

成功運行

接下來我們定義一個controller

建包 src.com.springmvc.controller 如下

 

在 src.com.springmvc.controller包下建類 indexController 如下

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
public class indexController {

    @RequestMapping("myIndex")
    public ModelAndView myIndex(){

        return new ModelAndView("myindex");
    }

}

 

在WEB-INF下建 jsp文件夾,並建文件myindex.jsp 如下

編輯 dispatcher-servlet.xml文件爲如下內容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 啓用spring mvc 註解 -->
    <context:annotation-config />

    <!-- 設置使用註解的類所在的jar包 -->
    <context:component-scan base-package="com.springmvc.controller"></context:component-scan>


    <!-- 對轉向頁面的路徑解析。prefix:前綴, suffix:後綴 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

</beans>

修改web.xml文件

重新啓動項目 並訪問myIndex 如下

spring mvc配置成功

 

三.改爲maven構建的項目,添加maven框架支持,把lib下面的包引用方式改爲 maven的pom.xml方式管理

 

 

添加maven支持後項目目錄會改變爲如下層級

根據lib下引用包的列表,依次在pom.xml文件中找對應的包引用,注意版本要一致。並把lib下面的包刪除

pom.xml引用部分內容

<dependencies>
<dependency>
    <groupId>aopalliance</groupId>
    <artifactId>aopalliance</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument-tomcat</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
</dependencies>

 

 

重啓項目

報錯

[2019-06-16 03:30:24,372] Artifact springmvc:war exploded: Artifact is being deployed, please wait...
16-Jun-2019 03:30:24.665 嚴重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file
16-Jun-2019 03:30:24.666 嚴重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors
[2019-06-16 03:30:24,695] Artifact springmvc:war exploded: Error during artifact deployment. See server log for details.

原因:改爲pom.xml中引用後,包沒有被髮布到lib下面

修改方法

 

重新啓動項目 並訪問myIndex 截圖如下

此時,jar包已通過pom.xml文件管理,maven框架添加成功

 

IDEA 下創建spring mvc項目並用maven構建 成功

先建java web項目,再添加spring mvc 和 maven的支持,感覺比先建maven項目要好理解一些

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章