IDEA+maven+springMVC第一次启动

第一次启动遇到了不少问题,记录一下

方法1:通过在pom.xml中引入spring依赖引入springMVC

这应该是标准方法
pom.xml:

    <!--spring相关包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.1.RELEASE</version>
    </dependency>

然后自己创建spring配置文件。springMVC和spring可以用一个配置文件。
不用springboot最大的问题是Tomcat的问题,需要自己手动在IDE中配置外部的Tomcat。

方法2:启动一个全新的maven项目,右键添加框架web和springMVC

不推荐这种方法,但用这种方法出现一些的错误排除很有价值,记录一下

两个配置文件applicationContext.xml和dispatcher-servlet.xml区别

添加框架后,会出现两个配置文件applicationContext.xml和dispatcher-servlet.xml,其中
ApplicationContext.xml 是spring 全局配置文件,用来控制spring 特性的
dispatcher-servlet.xml 是spring mvc里面的控制器、拦截uri转发view
使用applicationContext.xml文件时是需要在web.xml中添加listener的:
web.xml:

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

用方法2会遇到的一些报错:

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener 异常

https://blog.csdn.net/cflys/article/details/74853402
原因在于输出的WEB-INF中没有lib目录,无法引入项目引用的jar包。
在IDEA的Project Structure窗口,在Output Layout标签中找到Available Elements,右键Put into Output Root
这是由于lib包是通过右键添加框架产生的外部lib,如果是通过maven的pom.xml添加包的就不用配置这一项。
总结:如果没通过maven,手动引入了外部lib包,需要在Project Structure中添加输出

通用问题:
1、IDEA中Tomcat控制台乱码问题以及IDEA编码设置UTF-8
https://blog.csdn.net/nan_cheung/article/details/79337273
打开idea安装目录-bin
用记事本打开idea.exe.vmoptions和idea64.exe.vmoptions文件
这两个文件的最后加上-Dfile.encoding=UTF-8

2、org.xml.sax.SAXParseException: schema_reference.4: 无法读取方案文档
spring配置文件的方案文档之前是手动添加(错误)的,换成下面正确的url
http://www.springframework.org/schema/context/spring-context-4.0.xsd

3、Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
这个问题主要是由于tomcat无法读取spring配置文件导致的。
解决方法:
首先要了解maven的打包方式,在resource文件夹中放spring的配置文件,此文件会被自动打包到target/maven-springmvc/WEB-INF/classes文件夹下,此时在web.cml中应用

    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value> //这时不会变红,就是正确的了
    </init-param>

即可

Tomcat下的Web应用有两个预置的classpath : WEB-INF/classes 和WEB-INF/lib启动项目,项目就会加载这两个目录里的数据。这是war包的规范.要改变预置的classpath比较麻烦,在Tomcat的配置文件里没有发现类似的配置,要实现自己的classloader才能达到目的。
启动Tomcat时,spring按照param-name属性来读取自己的配置文件。

打包过程:WEB-INF目录下的文件会原封不动地打包入生成文件的WEB-INF目录。

最终成功运行的文件内容:

改动两个文件:web.xml,spring配置文件。再写一个业务逻辑文件

业务逻辑文件HelloController.java

package com.demo.hello;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/hello")
public class HelloController{
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public String printHello() {
        return "hello world!";
    }
}

applicationContext.xml和dispatcher-servlet.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:context="http://www.springframework.org/schema/context"
       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-4.0.xsd">

    <context:component-scan base-package="com.demo.hello" />
    
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

项目结构图:在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值