2017-03-15 70 views
0

樣式表在我的項目中不起作用。我在我的項目中使用Spring Boot,Thymeleaf,Config。 我的結構Structure百里香css Spring MVC註釋

我的login.html

<!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> 
 
    <title>Login</title> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"/> 
 
    <link th:href="@{/static/login.css}" href="../static/login.css" rel="stylesheet" type="text/css"/> 
 

 
</head> 
 
<body> 
 

 
<div class="container"> 
 

 
    <form class="form-signin" action="#" method="post"> 
 
     <h2 class="form-signin-heading">Please sign in</h2> 
 
     <input class="form-control" placeholder="Email address"> 
 
     <input type="password" class="form-control" placeholder="Password"> 
 
     <br> 
 
     <button class="btn btn-primary btn-lg btn-block" type="submit">Sign in</button> 
 
    </form> 
 

 
</div> 
 

 

 
</body> 
 
</html>

我的樣式表Login.css

body { 
 
    padding-top: 40px; 
 
    padding-bottom: 40px; 
 
    background-color: #f5f5f5; 
 
} 
 

 
.form-signin { 
 
    max-width: 400px; 
 
    padding: 19px 29px 29px; 
 
    margin: 0 auto 20px; 
 
    background-color: #fff; 
 
    border: 1px solid #e5e5e5; 
 
    -webkit-border-radius: 5px; 
 
    -moz-border-radius: 5px; 
 
    border-radius: 5px; 
 
    -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05); 
 
    -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05); 
 
    box-shadow: 0 1px 2px rgba(0,0,0,.05); 
 
} 
 
.form-signin .form-signin-heading, 
 
.form-signin .checkbox { 
 
    margin-bottom: 10px; 
 
} 
 
.form-signin input[type="text"], 
 
.form-signin input[type="password"] { 
 
    font-size: 16px; 
 
    height: auto; 
 
    margin-bottom: 15px; 
 
    padding: 7px 9px; 
 
}

我的控制器

@Controller public class UserController {

private UserServiceImpl userService; 

@RequestMapping(value = "/", method = RequestMethod.GET) 
public String index() { 

    return "login"; 
} 

}`

我的配置文件

@Configuration @EnableWebMvc //mvc:annotation-driven @ComponentScan({ "controller" }) public class SpringWebConfig extends WebMvcConfigurerAdapter{

@Bean 
public SpringResourceTemplateResolver templateResolver(){ 
    // SpringResourceTemplateResolver automatically integrates with Spring's own 
    // resource resolution infrastructure, which is highly recommended. 
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); 
    templateResolver.setOrder(1); 

    templateResolver.setPrefix("classpath:/templates/"); 
    templateResolver.setSuffix(".html"); 
    // HTML is the default value, added here for the sake of clarity. 
    templateResolver.setTemplateMode(TemplateMode.HTML); 
    // Template cache is true by default. Set to false if you want 
    // templates to be automatically updated when modified. 

    templateResolver.setCacheable(true); 
    return templateResolver; 
} 

@Bean 
public SpringTemplateEngine templateEngine(){ 
    // SpringTemplateEngine automatically applies SpringStandardDialect and 
    // enables Spring's own MessageSource message resolution mechanisms. 
    SpringTemplateEngine templateEngine = new SpringTemplateEngine(); 
    templateEngine.setTemplateResolver(templateResolver()); 
    // Enabling the SpringEL compiler with Spring 4.2.4 or newer can 
    // speed up execution in most scenarios, but might be incompatible 
    // with specific cases when expressions in one template are reused 
    // across different data types, so this flag is "false" by default 
    // for safer backwards compatibility. 
    templateEngine.setEnableSpringELCompiler(true); 
    return templateEngine; 
} 

@Bean 
public ThymeleafViewResolver viewResolver(){ 
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); 
    viewResolver.setTemplateEngine(templateEngine()); 
    return viewResolver; 
} 


@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
} 

}` Wrong result 的結果是錯誤的。 Login.css不起作用。錯誤在哪裏?

+0

什麼是login.css的生成路徑還檢查控制檯錯誤 – SAP

+0

WARN 5764 --- [nio-8080-exec-7] osweb.servlet.PageNotFound:在DispatcherServle中找不到具有URI [/static/login.css]的HTTP請求的映射t名稱爲'dispatcherServlet' –

回答

0

我想你應該更新@RequestMapping(路徑的控制器 =「/」 ...

雖然這個註釋的不是很直觀,但不正確的設置可能會導致不加載CSS的。 我類似的問題和一切似乎都是正確的(HTML,Thymeleaf代碼,項目結構 - 我讀了很多以前的帖子),但CSS不在。