java - 没有为 spring boot 元素加载静态 css

标签 java css spring

问题:正如您从图片中看到的,浏览器无法加载 css 文件。 我也在 STS 控制台中得到这个异常:

[2m2018-02-10 19:00:57.770[0;39m [33m 警告[0;39m [35m16292[0;39m [2m---[0;39m [2m[nio-8080-exec-3 ][0;39m [36m.w.s.m.s.DefaultHandlerExceptionResolver[0;39m [2m:[0;39m 处理程序执行引起的已解决异常:org.springframework.web.bind.UnsatisfiedServletRequestParameterException:实际请求参数不满足参数条件“取消”:

the code was working fine until I completed the included Controller

有人知道为什么会这样吗? 来自浏览器(firefox)的错误

][1]

!DOCTYPE html>

    <html xmlns:th="http://www.thymeleaf.org">
    <head lang="en" th:fragment="head">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!-- jQuery library -->
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7 /js/bootstrap.min.js"></script>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <link th:href="@{/css/style2.css}" rel="stylesheet" media="screen" />
    <link th:href="@{/css/login.css}" rel="stylesheet" media="screen"/>
    <link rel="stylesheet" type="text/css"  media="screen" th:href="@{/css/login.css}"/>
</head>
<body>

</body>
</html>

Controller

package com.intelbs.controllers;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.intelbs.dao.Customer;
import com.intelbs.dao.Employee;
import com.intelbs.dao.Expense;
import com.intelbs.dao.Note;
import com.intelbs.services.ExpenseService;

@Controller
public class ExpenseController {

    @Autowired
    private ExpenseService expService;

    @RequestMapping(value = "/monthlyExpenses", method = RequestMethod.GET)
    public String listMonthlyExpenses(Model model, String month, String year) {
        model.addAttribute("monthlyExpenses", expService.monthExpense(month, year));
        return "monthlyexpensesform";

    }

    @RequestMapping(value="new_expense", method = RequestMethod.GET)
    public String diplayAddExpenseForm( Model model) {
        System.out.println("inside  display");
        model.addAttribute("expense", new Expense());
        return "addexpenseform";

    }

    @RequestMapping(params= "save", method=  RequestMethod.POST)

    public String processAddExpenseForm(@Valid Expense expense, BindingResult br,
                                        Model model, RedirectAttributes ra) {
        System.out.println("inside processform");
        if(br.hasErrors()) {
            System.out.println("inside has er");    
            return"addexpenseform";

        }

        model.addAttribute("expense", expense);

        expService.saveExpense(expense);

        ra.addFlashAttribute("expense", expense);

        return "redirect:/customers";
    }

    @RequestMapping(params = "cancel",method= {RequestMethod.GET, RequestMethod.POST})
    public String cancelAddExpenseForm() {

        return "redirect:/customers";
    }
}

  [Browser error]: /image/xO4l5.png

最佳答案

我认为您在 @RequestMapping 中错误地使用了 param 而不是 value

其实param是用来缩小匹配某个URL的请求

参见 doc

A sequence of "myParam=myValue" style expressions, with a request only mapped if each such parameter is found to have the given value.

像这样重构你的代码。

@RequestMapping(value="/new_expense", method = RequestMethod.GET)
    public String diplayAddExpenseForm( Model model) {
        System.out.println("inside  display");
        model.addAttribute("expense", new Expense());
        return "addexpenseform";

    }

    @RequestMapping(value= "/save", method=  RequestMethod.POST)
    public String processAddExpenseForm(@Valid Expense expense, BindingResult br,
                                        Model model, RedirectAttributes ra) {
        System.out.println("inside processform");
        if(br.hasErrors()) {
            System.out.println("inside has er");    
            return"addexpenseform";

        }

        model.addAttribute("expense", expense);

        expService.saveExpense(expense);

        ra.addFlashAttribute("expense", expense);

        return "redirect:/customers";
    }

    @RequestMapping(value= "/cancel",method= {RequestMethod.GET, RequestMethod.POST})
    public String cancelAddExpenseForm() {

        return "redirect:/customers";
    }

关于java - 没有为 spring boot 元素加载静态 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48727938/

相关文章:

java - 列表排序难题

iphone - 如何修复移动设备(Wordpress 主题)的 "responsive-design mode"问题?

Javascript 输入类型 ="color"表单验证

java - WebLogic - 在 WebLogic Server 中部署 Spring Boot war

java - 如何在 jboss 6 上的 HornetMQ 中设置主题

java - 给定字符串中的FirstNonRepeatingCharacter在java中使用时间复杂度O(n)

java - 如何在 JEditorPane 中单击 <a> 标签时更改页面?

javascript - Bootstrap 不适用于 Iframe 中的 IE 6

java - CSRF守卫 : How to hide CSRF token from URL

java - 什么是依赖注入(inject)和 Spring 框架?