首页 新闻 搜索 专区 学院

springboot+thymeleaf模版引入静态资源不对问题

-1
悬赏园豆:20 [已解决问题] 解决于 2017-11-20 09:34

我写了一个方法LoginController.java没有任何逻辑直接跳转页面,然后页面样式、js、图片都没有引用进来;下面上下图

先说下问题

页面使用了th标签引入了css/js/图片这些静态资源,

自地址栏直接访问没有问题可以看到

但是我访问http://localhost:8080/users/login(通过方法跳转到页面)出现如下问题

在请求静态资源的时候默认的给我加上了/users,导致资源请求不到,请各位大侠帮忙看看是什么原因,

目录结构:

controller代码:

package com.dfyz.controller.admin;

import com.dfyz.Message;
import com.dfyz.entity.Member;
import com.dfyz.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2017/11/16.
 */
@Controller
@RequestMapping("/users")
public class LoginController {

    @Resource(name = "memberService")
    private MemberService memberService;

    @RequestMapping("/login")
    public String login(String userName, String password, ModelMap map){
        Member member = memberService.find(1L);
//        if(member.getPassword().equals(password.trim())){
//            return Message.success("成功");
//        }
//        return Message.error("请核对用户名和密码");
        map.addAttribute("member",member);
        return "admin/index";
    }
}

页面代码:(将body中的内容删掉了)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <title>xxx专用</title>
    <meta name="description" content="估计只有你一个人用了"/>
    <meta name="keywords" content="index"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="icon" type="image/png" th:href="@{assets/i/favicon-bak.png}" />
    <link rel="apple-touch-icon-precomposed" th:href="@{assets/i/app-icon72x72@2x.png}" />
    <meta name="apple-mobile-web-app-title" content="Amaze UI" />
    <script th:src="@{assets/js/echarts.min.js}"></script>
    <link rel="stylesheet" th:href="@{assets/css/amazeui.min.css}" />
    <link rel="stylesheet" th:href="@{assets/css/amazeui.datatables.min.css}" />
    <link rel="stylesheet" th:href="@{assets/css/app.css}" />
    <script th:src="@{assets/js/jquery.min.js}"></script>

</head>

<body data-type="index">

</body>

</html>

配置文件:

# 端口
#server.port=80
# 自动扫描的包前缀
entitymanager.packagesToScan= com.dfyz
## 自动扫描的包前缀
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.maxActive=500
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.properties.hibernate.hbm2ddl.auto=update
# spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
# 静态文件请求匹配方式
spring.mvc.static-path-pattern=/**
# 修改默认的静态寻址资源目录
spring.resources.static-locations = classpath:/templates/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
#热部署生效
spring.devtools.restart.enabled=true

cbs.imagesPath=file:/E:/imagesuuuu/
Nan丶G的主页 Nan丶G | 初学一级 | 园豆:186
提问于:2017-11-18 23:20
< >
分享
最佳答案
2

从其他途径获取了错误的原因:我想从让其最后的js,css,以及图片的请求地址是localhoast:8080/xxxxx.js;  这种方式应该是属于绝对路径写的方式应该是@{/assets/css/amazeui.min.css}这种,虽然是小错误,但是也让我纠结了好长时间,希望可以帮到和我一样马虎的人-_-!

Nan丶G | 初学一级 |园豆:186 | 2017-11-20 09:34

折腾一个多小时终于好了,谢谢博主!

assansec | 园豆:200 (初学一级) | 2021-06-20 16:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册