springmvc-demo-fangneng(SpringMVC工程搭

文件目录:

导入包:

  1.  
    <groupId>org.example</groupId>
  2.  
    <artifactId>springmvc-demo-fangneng</artifactId>
  3.  
    <version>1.0-SNAPSHOT</version>
  4.  
     
  5.  
    <dependencies>
  6.  
    <dependency>
  7.  
    <groupId>junit</groupId>
  8.  
    <artifactId>junit</artifactId>
  9.  
    <version>4.13.2</version>
  10.  
    <scope>test</scope>
  11.  
    </dependency>
  12.  
    <dependency>
  13.  
    <groupId>org.springframework</groupId>
  14.  
    <artifactId>spring-webmvc</artifactId>
  15.  
    <version>5.2.13.RELEASE</version>
  16.  
    </dependency>
  17.  
    <dependency>
  18.  
    <groupId>javax.servlet</groupId>
  19.  
    <artifactId>servlet-api</artifactId>
  20.  
    <version>2.5</version>
  21.  
    </dependency>
  22.  
    <dependency>
  23.  
    <groupId>javax.servlet</groupId>
  24.  
    <artifactId>javax.servlet-api</artifactId>
  25.  
    <version>4.0.1</version>
  26.  
    <scope>provided</scope>
  27.  
    </dependency>
  28.  
    </dependencies>
  29.  
     
  30.  
    <build>
  31.  
    <resources>
  32.  
    <resource>
  33.  
    <directory>src/main/java</directory>
  34.  
    <includes>
  35.  
    <include>**/*.properties</include>
  36.  
    <include>**/*.xml</include>
  37.  
    </includes>
  38.  
    <filtering>false</filtering>
  39.  
    </resource>
  40.  
    <resource>
  41.  
    <directory>src/main/resources</directory>
  42.  
    <includes>
  43.  
    <include>**/*.properties</include>
  44.  
    <include>**/*.xml</include>
  45.  
    </includes>
  46.  
    <filtering>false</filtering>
  47.  
    </resource>
  48.  
    </resources>
  49.  
    </build>
HelloController文件:
  1.  
    @Controller
  2.  
    public class HelloController {
  3.  
    @RequestMapping("/hello")
  4.  
    public String hello(Model model){
  5.  
    // Model 封装数据
  6.  
    model.addAttribute("msg","HELLO MY FIRST SPRING MVC PROJECT");
  7.  
     
  8.  
    // 返回的字符串就是视图的名字 会被视图解析器处理
  9.  
    return "hello";
  10.  
    }
  11.  
    }

applicationContext.xml文件:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xmlns:context="http://www.springframework.org/schema/context"
  5.  
    xmlns:mvc="http://www.springframework.org/schema/mvc"
  6.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.  
    http://www.springframework.org/schema/beans/spring-beans.xsd
  8.  
    http://www.springframework.org/schema/context
  9.  
    https://www.springframework.org/schema/context/spring-context.xsd
  10.  
    http://www.springframework.org/schema/mvc
  11.  
    https://www.springframework.org/schema/mvc/spring-mvc.xsd
  12.  
    ">
  13.  
     
  14.  
    <!-- bean definitions here -->
  15.  
     
  16.  
    <!-- 1加载注解驱动 -->
  17.  
    <mvc:annotation-driven/>
  18.  
     
  19.  
    <!-- 2静态资源过滤 -->
  20.  
    <mvc:default-servlet-handler/>
  21.  
     
  22.  
    <!-- 3视图解析器 -->
  23.  
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  24.  
    <property name="prefix" value="/WEB-INF/jsp/"/>
  25.  
    <property name="suffix" value=".jsp"/>
  26.  
    </bean>
  27.  
     
  28.  
    <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
  29.  
    <context:component-scan base-package="controller"/>
  30.  
     
  31.  
    </beans>

hello.jsp文件:

  1.  
    <%--
  2.  
    Created by IntelliJ IDEA.
  3.  
    User: Lenovo
  4.  
    Date: 2021/3/31
  5.  
    Time: 14:28
  6.  
    To change this template use File | Settings | File Templates.
  7.  
    --%>
  8.  
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9.  
    <html>
  10.  
    <head>
  11.  
    <title>Title</title>
  12.  
    </head>
  13.  
    <body>
  14.  
    ${msg}
  15.  
    </body>
  16.  
    </html>

web.xml文件:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5.  
    version="4.0">
  6.  
     
  7.  
    <!-- 配置前端控制器 -->
  8.  
    <servlet>
  9.  
    <servlet-name>springmvc</servlet-name>
  10.  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  11.  
     
  12.  
    <!-- 配置初始化参数 -->
  13.  
    <init-param>
  14.  
    <param-name>contextConfigLocation</param-name>
  15.  
    <param-value>classpath:applicationContext.xml</param-value>
  16.  
    </init-param>
  17.  
     
  18.  
    <!-- 设置启动级别 -->
  19.  
    <load-on-startup>1</load-on-startup>
  20.  
     
  21.  
    </servlet>
  22.  
     
  23.  
    <!-- 设置SpringMVC拦截请求 -->
  24.  
    <servlet-mapping>
  25.  
    <servlet-name>springmvc</servlet-name>
  26.  
    <url-pattern>/</url-pattern>
  27.  
    </servlet-mapping>
  28.  
     
  29.  
    <!-- 乱码过滤 -->
  30.  
    <filter>
  31.  
    <filter-name>encodingFilter</filter-name>
  32.  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  33.  
    <init-param>
  34.  
    <param-name>encoding</param-name>
  35.  
    <param-value>utf-8</param-value>
  36.  
    </init-param>
  37.  
    </filter>
  38.  
    <filter-mapping>
  39.  
    <filter-name>encodingFilter</filter-name>
  40.  
    <url-pattern>/*</url-pattern>
  41.  
    </filter-mapping>
  42.  
    </web-app>

index.jsp文件:

  1.  
    <%--
  2.  
    Created by IntelliJ IDEA.
  3.  
    User: Lenovo
  4.  
    Date: 2021/3/31
  5.  
    Time: 14:15
  6.  
    To change this template use File | Settings | File Templates.
  7.  
    --%>
  8.  
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9.  
    <html>
  10.  
    <head>
  11.  
    <title>$Title$</title>
  12.  
    </head>
  13.  
    <body>
  14.  
    $END$
  15.  
    </body>
  16.  
    </html>

测试结果:

 

 
posted @ 2021-04-14 23:21  胖疯的小家  阅读(79)  评论(0)    收藏  举报