springboot 的部分错误


Application.properties 中
#指定端口号 server.port
=8087 #指定访问路径必须以/crud/xxx 开始 server.servlet.context-path=/crud #指定编码格式 server.tomcat.uri-encoding=UTF-8 #指定日期格式 spring.mvc.date-format=yyyy-MM-dd # 禁用缓存 spring.thymeleaf.cache=false # 国际化配置文件(包名.基础名) spring.messages.basename=i18n.login

pom 文件中需要加上自己需要的依赖:如

    <!--引入jquery-webjar-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>

        <!--引入bootstrap-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.0.0</version>
        </dependency>

静态页面不要放在静态文件夹下,会得不到模板引擎的解析需要放在

首先就是要访问我们的首页,这是可以指定的首先可以写一个方法这时有两种方法

(1)不推荐 在controller中写上一个方法

 @RequestMapping({"/","/index.html"})
    public String index(){
    //模板引擎会自动进行拼写自动加上.html
        return "login";
    }

(2)加上视图映射 viewControlle

 手动写上一个配置类使用视图映射

因为WebMvcConfigurerAdapter 在Spring5.0已被废弃所以  博主推荐的两种方式https://blog.csdn.net/lenkvin/article/details/79482205

 

1 直接实现WebMvcConfigurer (官方推荐)

@Configuration
public class WebMvcConfg implements WebMvcConfigurer {

    //todo

}

2 直接继承WebMvcConfigurationSupport

@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {

        //todo

}

使用第一种方法

 

 

 

 

 

说下默认映射的文件夹有:

classpath:/META-INF/resources

  • classpath:/resources

  • classpath:/static

  • classpath:/public

上面这几个都是静态资源的映射路径,优先级顺序为:META-INF/resources > resources > static > public

我们可以通过修改spring.mvc.static-path-pattern来修改默认的映射**

具体信息https://www.cnblogs.com/java-synchronized/p/7091723.html

 对login.html 页面的修改

th:text是对标签体中的修改   

th:text="#{login.password}

而复选框是字节数的没有标签体 所以使用  

[[#{login.remember}]]

注册国际化标准i18N

别忘了在配置文件中加上

# 国际化配置文件(包名.基础名)
spring.messages.basename=i18n.login

放行静态资源

 registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/login.html","/","/webjars/**","/user/login","/static/**");

 =====================================================================================================================================

 springboot的数据库的错误

当我使用springboot 的快速启动导入了jdbc  mybatis  出现这种错误

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-04-26 08:35:50.757 ERROR 16084 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

《https://www.jianshu.com/p/836d455663da 解释详细连接》

问题原因: Mybatis没有找到合适的加载类,其实是大部分spring - datasource - url没有加载成功,分析原因如下所示.

  1. DataSourceAutoConfiguration会自动加载.

  2. 没有配置spring - datasource - url 属性.

  3. spring - datasource - url 配置的地址格式有问题.

  4. 配置 spring - datasource - url的文件没有加载.

方案一 (解决原因1)
排除此类的autoconfig。启动以后就可以正常运行。

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})

 

方案二 (解决原因2)

在application.properties/或者application.yml文件中没有添加数据库配置信息.

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/read_data?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
方案三 (解决原因3)

在spring xml配置文件中引用了数据库地址 所以需要对:等进行转义处理.但是在application.properties/或者application.yml文件并不需要转义,错误和正确方法写在下面了.

//错误示例
spring.datasource.url = jdbc:mysql\://192.168.0.20\:1504/f_me?setUnicode=true&characterEncoding=utf8

//正确示例
spring.datasource.url = jdbc:mysql://192.168.0.20:1504/f_me?setUnicode=true&characterEncoding

方案四 (解决原因4)

yml或者properties文件没有被扫描到,需要在pom文件中<build></build>添加如下.来保证文件都能正常被扫描到并且加载成功.

<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

 




 

 

 

 

 

 

 


posted @ 2019-04-25 11:54  纳兰容若♫  阅读(238)  评论(1编辑  收藏  举报