SpringMVC环境搭建

开发环境

Idea + Maven + Jdk1.8 + Jetty

新建Maven webApp

SpringMVC环境搭建_第1张图片

pom.xml添加坐标依赖

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <!-- spring web -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.4.RELEASE</version>
    </dependency>
    <!-- spring mvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.4.RELEASE</version>
    </dependency>
    <!-- web servlet -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>springmvc01</finalName>


    <plugins>
      <!-- 编译环境插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <!-- jetty插件 -->
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.4.27.v20200227</version>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <!-- 设置端口 -->
          <httpConnector>
            <port>8080</port>
          </httpConnector>
          <!-- 设置项目路径 -->
          <webAppConfig>
            <contextPath>/springmvc01</contextPath>
          </webAppConfig>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

配置web.xml

该xml文件在src/main/webapp/WEB-INF文件夹目录下

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <!-- 编码过滤 utf-8 -->
  <filter>
    <description>char encoding filter</description>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*
  
  
  
    springMvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:servlet-context.xml
    
    
    1
  
  
    springMvc
    
    
    *.do
  

SpringMVC环境搭建_第2张图片
此时servlet-context.xml 文件还未创立

servlet-context.xml 配置

新建一个resources资源文件夹放servlet-context.xml 文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       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">

    <!-- 开启扫描器 -->
    <context:component-scan base-package="com.shsxt.springmvc.controller"/>
    <!--使用默认的 Servlet 来响应静态文件-->
    <mvc:default-servlet-handler/>
    <!-- 开启注解驱动-->
    <mvc:annotation-driven/>
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <!-- 前缀:WEB-INF目录下的jsp目录下 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 后缀: 以jsp结尾的资源 -->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

SpringMVC环境搭建_第3张图片
base-package扫描路径要自己创建,新建本文件夹java并在里面创建对应的路径
SpringMVC环境搭建_第4张图片
jsp目录也要自己新建

页面控制器的编写

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello(){
        //得到模型和视图对象
        ModelAndView modelAndView = new ModelAndView();
        //设置数据模型
        modelAndView.addObject("hello","Hello SpringMVC");
        //设置视图(名称)
       modelAndView.setViewName("hello");
       //返回模型和视图
        return modelAndView;
    }
}

添加视图页面

在jsp文件夹下新建hello.jsp文件
SpringMVC环境搭建_第5张图片

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--获取数据模型--%>
    ${hello}
</body>
</html>

启动jetty服务

SpringMVC环境搭建_第6张图片
SpringMVC环境搭建_第7张图片
为什么访问路径里hello后面要加.do呢?
因为上面web.xml最后拦截请求里写了*.do表示拦截所有.do请求,换成/就表示拦截所有请求
至此SpringMVC环境搭建就成功啦

你可能感兴趣的:(spring,spring,前端)