q1993a01z05 2017-11-18 15:23 采纳率: 0%
浏览 15087
已结题

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

我写了一个方法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/

  • 写回答

3条回答

  • 默默a20 2017-11-19 15:03
    关注

    因为你的controller 类有"/user",然后你的html页面引用的静态文件是基于html 路径的,就是/user/*.css,
    你把引用的静态文件用全路径写出来,就是${basePath}/css/amazeui.datatables.min.css
    你去后台set个basePath 大概 request.setAttribute("basePath", "http://"+cloudAddress+":"+port+contextPath);

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题