【三】、整合SpringMVC(基本配置和thymeleaf模板选择)

本次主要集成SpringMVC,使得本框架能最基本地挂在服务器上运行。下图是经过本节配置之后的项目目录结构,由于spring boot并不会生成webapp等文件,所以需要自行配置,使用intelij的话,直接加入依赖即可:

github:https://github.com/wulongtao/web-base-master

项目目录结构

过程如下:

1、pom.xml文件的引入,主要引入spring mvc、Jackson(SpringMVC返回JSON数据用到)等,这里就不贴代码了,目前为止完整的pom.xml我在github中给出:

https://github.com/wulongtao/web-base-master/blob/master/pom.xml

2、配置web.xml文件,主要使得SpringMVC可以处理Servlet,但是这里有个问题至今都没解决,配置DispatcherServlet之后,welcome-file的时候只有jsp有效了,html无效。(最终我想实现的是欢迎页面也是html文件。。。)

<?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_3_1.xsd"
         version="3.1">

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>web-base</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>web-base</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

3、配置spring-mvc-config.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- don't handle the static resource 设置之后controller的请求都访问不了? -->
    <!--<mvc:default-servlet-handler />-->

    <!--使用注解包配置-->
    <context:component-scan base-package="com.xxh.web.controller" />
    <context:annotation-config />
    <mvc:annotation-driven />

    <!--配置默认视图解析器,这里只能解析jsp后缀的页面,不能解析html后缀的页面,优先级最低-->
    <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>

        <property name="order" value="#{T(org.springframework.core.Ordered).LOWEST_PRECEDENCE}" />
    </bean>

    <!--静态资源配置-->
    <mvc:resources location="/static/" mapping="/static/**"/>

</beans>

具体代码(包括controller测试代码)在github中全部给出

4、上面配置之后,项目可以在Tomcat等容器上跑起来了,但是有个问题是上面的模板是spring自带的模板解析器,它只能解析jsp后缀的文件,无论在suffix怎么配置后缀,都是无效的,所以这里我选择使用spring官方推荐的模板——Thymeleaf,用这个模板就就可以使用html后缀的文件了。配置步骤如下:

  • pom.xml加入依赖

    <!--thymeleaf-->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    
        <!--thymeleaf模板放宽语法检查限制,使用LEGACYHTML5代替HTML5模式-->
        <!-- https://mvnrepository.com/artifact/net.sourceforge.nekohtml/nekohtml -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.21</version>
        </dependency>
  • spring-mvc-config.xml中配置模板,这里因为有两个模板,所以需要配置order属性来指定优先级

    <!--Thymeleaf 视图解析器,优先级最高-->
    <bean id="templateResolver"
          class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/templates/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="LEGACYHTML5" />
        <property name="order" value="#{T(org.springframework.core.Ordered).HIGHEST_PRECEDENCE}" />
    </bean>
    
    <bean id="templateEngine"
          class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>
    
    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
    </bean>
    
    
    <!--配置默认视图解析器,这里只能解析jsp后缀的页面,不能解析html后缀的页面,优先级最低-->
    <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    
        <property name="order" value="#{T(org.springframework.core.Ordered).LOWEST_PRECEDENCE}" />
    </bean>

在配置的过程中也有一个细节,网上大部分的配置是使用HTML5的模板,但是我用vue-cli打包出来的项目使用这个模板解析报错,原因是HTML5模板对语法的检查太严格了,所以需要使用LEGACYHTML5模板来代替

测试代码在github中给出,本节只是简单实现了Java Web项目的搭建,后面会通过封装继续简化开发过程,并且对项目的目录结构做进一步的规则化和优化

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值