一 创建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项目要好理解一些