spring MVC的环境配置

spring MVC的环境配置

注 : 可以使用java web进行操作,也可以使用maven项目进行配置

使用maven项目的配置方式

注 : 使用的是ideal工具开发的,基于jdk1.8,使用的jetty服务器

1. 创建maven的webapp项目

2. 进入项目改一下pom.xml中的一些系统配置

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>// 把版本改成1.8的
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>// 把依赖的版本改成4.12
      <scope>test</scope>
    </dependency>

3. 然后再在pom.xml添加mvc相关的依赖

注 : 依赖可以百度mvn搜索spring依赖

**3.1 spring-context **

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>

3.2 spring-core

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>

3.3 spring-beans

<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>

3.4 spring-web

<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>

3.5 spring-webmvc

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>

3.6 javax.servlet-api

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

4.在pom.xml中添加插件

4.1 编译环境插件

<!-- 编译环境插件 -->
      <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>

4.2 jetty插件

<!-- jetty插件 -->
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.4.27.v20200227</version>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <!-- 设置端⼝ -->
          <httpConnector>
            <port>8081</port>
          </httpConnector>
          <!-- 设置项⽬路径 -->
          <webAppConfig>
            <contextPath>/springmvc002</contextPath>
          </webAppConfig>
        </configuration>
      </plugin>

5.再设置前端控制器---->web.xml

<?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>/*</url-pattern>
  </filter-mapping>
  <!-- servlet请求分发器 -->
  <servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!-- 表示启动容器时初始化该Servlet -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <!-- 这是拦截请求, "/"代表拦截所有请求,"*.do"拦截所有.do请求 -->
    <url-pattern>/</url-pattern>
    <!--    <url-pattern>*.do</url-pattern>-->
  </servlet-mapping>
</web-app>

6. 编辑springmvc.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>

7. 创建Controller控制器----->用来测试环境

// 使用注解标识是controller层
@Controller
public class UserController {
    
    

    @RequestMapping("/hello")// 地址映射器--->当访问hello的时候会找注解下的方法
    public ModelAndView hello(){
    
    
        // 准备模型和视图
        ModelAndView modelAndView = new ModelAndView();
        // 设置视图名称----->这里的名字可以随便定,只要保持创建的jsp文件名要是一致的,否则是不能够访问的
        modelAndView.setViewName("Hello");
        // 设置数据模型---->attributeName是可以随便定义的     attributeValue是Object的,所以是可以写所有的内容
        modelAndView.addObject("msg","真难,spring");
        // 返回视图和模型
        return modelAndView;
    }
    
}

8. 创建视图解析器---->要根据web.xml配置文件中的路径去编写配置文件的位置

<%--
  Created by IntelliJ IDEA.
  User: 13390
  Date: 2020/9/13
  Time: 14:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--获取数据模型--%>
<%--这里的msg是Controller中attributeName--%>
${msg}
</body>
</html>

9. 启动项目---->编辑jetty插件进行启动

10. 打开浏览器测试

//localhost:8081/springmvc002/hello

// localhost本机的地址,或者127.0.0.1	
// 8081 是自己在pom.xml中jetty插件中定义的端口号
// springmvc002  是pom.xml中<contextPath>/springmvc002</contextPath>自己设置的项目的路径位置
// hello 要看两个方面 
// 第一个是在web.xml 中设置的拦截是什么样子的
<servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <!-- 这是拦截请求, "/"代表拦截所有请求,"*.do"拦截所有.do请求 -->
    <url-pattern>/</url-pattern>
    <!--    <url-pattern>*.do</url-pattern>-->
  </servlet-mapping>
// 第二这个hello是controller控制器中@RequestMapping中设置的访问路径

猜你喜欢

转载自blog.csdn.net/weixin_45561352/article/details/108563790