SpringMvc学习---HelloWord

国际知名程序---HelloWorld

首先建立maven工程,导入依赖

pom.xml文件中

在里面要指定项目的打包方式为 war ,否则在后面配置tomcat时,会无法将项目部署在服务器上

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion><groupId>org.example</groupId>
    <artifactId>helloMvc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
​
​
    <dependencies>
        <!-- SpringMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency><!-- 日志 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency><!-- ServletAPI -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency><!-- Spring5和Thymeleaf整合包 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.12.RELEASE</version>
        </dependency>
    </dependencies></project>
 

 

在main文件夹下

创建webapp目录,【如果没有蓝色小圆点,一样打开项目结构,进去点底下的Web resource ....那一部分里面的一个点 - 号删掉,再重新加一个。注意路径指向就好了】打开项目结构,进入 Moudles 中,展开项目结构点击web,点击加号,添加一个WEB-INF目录和web.xml文件。注意IDEA给出的默认路径,要让那个路径指向所创建的webapp目录下。

在web.xml文件中

使用扩展配置方式配置,所谓扩展配置就是不使用默认配置方式进行配置前端控制器。在其中规定SpringMvc的配置文件路径。不让配置文件都在WEB-INF目录下。

关于默认配置和细节问题看底下注释

<?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">
    <!--配置前端控制器,设置由 DispatcherServlet 进行统一处理所有请求-->
​
​
    <!--
    使用扩展方式的好处是可以看见SpringMvc配置文件的路径和加如何加载SpringMvc配置文件的过程
    使用类似于JavaWeb 的配置servlet的方式叫默认配置方式,他不能看见这些,而且还不能规定前端控制器的初始化时间
    默认配置方式的SpringMvc配置文件在WEB-INF目录下,名称默认是 <servlet-name>-servlet.xml
    假如现在使用了默认配置方式,那么配置文件的名称是: SpringMvc-servlet.xml
    --><!--这叫扩展配置方式-->
    <servlet>
        <servlet-name>SpringMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--
        通过设置Servlet初始化参数, contextConfigLocation【上下文配置路径】 = classpath【对应位置】 : 【名称】
        从而告诉服务器 SpringMvc 其他配置文件在在哪里
        设置这个标签的目的就是不让所有的配置文件都在WEB-INF中
        -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:SpringMvc.xml</param-value>
        </init-param>
        <!--
        由于Servlet会在第一次被访问的时候初始化内容, DispatcherServlet 也会整个项目第一被访问时在这时进行初始化,会让第一次访问很拥挤
        所以设置 load-on-startup 标签值为1,让 DispatcherServlet 在服务器启动时就完成初始化工作,减少后面的步骤
        -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMvc</servlet-name>
        <!--
          /   可以匹配.js、.html、.css、.login 这类的请求但是不能匹配.jsp请求
          因为jsp本质上是一个servlet,这类请求一般都是由指定的servlet进行处理,
          如果它被DispatcherServlet拦截处理了就不会找到那个特定的servlet从而导致jsp页面无法显示
          /*   能拦截.jsp请求
          -->
        <url-pattern>/</url-pattern>
    </servlet-mapping></web-app>
 

 

在main->java->resources文件夹下创建SpringMvc配置文件

在SpringMvc.xml文件中

添加命名空间context,开启组件扫描,添加视图解析器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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"><!--开启组件扫描-->
    <context:component-scan base-package="com.xlw.SpringMvc"></context:component-scan><!-- 配置Thymeleaf视图解析器 -->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"><!-- 视图前缀 指定解析请求的文件前缀,是一个目录 -->
                        <property name="prefix" value="/WEB-INF/templates/"/><!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean></beans>
 

 

其他

在WEB-INF目录下建立一个文件夹,名字要跟视图解析器里面的视图前缀中WEB-INF后面跟的名字一样。可以先创建这个目录然后cv过去。最后建立一个hello.html文件在里面

要用到 thymeleaf ,所以要把

xmlns:th="http://www.thymeleaf.org

 

加在html标签里面,以后还会创建很多 html 页面,为了方便进入 setting->Editor->FileandCode....里面,给html模板的html 标签里加这句,以后创建html页面就会自动加上了

一般不能直接访问WEB-INF目录下的文件但是可以通过请求转发进行访问。现在用SpringMvc进行访问

 

在控制器Controller中

控制器是一个类

添加注解,把类加入SprinIOC容器里,创建一个方法负责处理访问一个请求

@RequestMapping( value = " 负责处理的请求 "),当服务器接收到请求传给DisPatcherServlet,DispatcherServlet通过Spring配置文件找到控制器,跟里面的添加了@RequestMapping注解里面的解析路径进行匹配,匹配一致的就会得到返回值。得到返回值通过视图解析器添加视图前缀和视图后缀得到完整的资源路径,通过thymeleaf渲染,拿到资源转发到目标资源文件所在,最后浏览器显示

@Controller
public class HelloMvc {
​
//    将请求和方法创建映射关系通过 value 属性设置当浏览器请求某一地址【value】时,由下面这个方法处理请求,并指向访问资源或别的
    @RequestMapping("/")
    public String helloIndex(){
//        返回视图名称
        return "index";
    }
​
}

 

 

Controller处理不同请求

在index.html文件中

这里使用了thymeleaf的语法

如果a标签中 href 属性直接写为 "/target",在浏览器解析时,会直接在webapp目录下直接匹配,他不能访问WEB-INF目录,找不到文件。就报错

使用 th:href 属性当检测到参数是绝对路径时,就会自动添加上下文路径,从上下文路径访问

相当于在jsp页面的base标签里,location = ${pageContext.request.contextPath},由自己添加工程的上下文路径。而这个就是在html页面里自动添加,省去你加上下文路径,自动帮你配置好

<a th:href="@{/target}">去别的地方看看</a>

 

 

建立一个target.html文件在同一目录下

在Controller类中

@RequestMapping("/target")
public String getTarget(){
    return "target";
}
 

 

posted @ 2021-11-16 23:32  优质水  阅读(46)  评论(0)    收藏  举报