1

Been developing web apps for a few years now and i've never seen this one. I'm working on a spring boot web app that will have several controllers. The views are under src\main\webapp\WEB-INF\views, the CSS is under src\main\resources\static\css and the images are under src\main\resources\static\css\images.

In my home.jsp page, i include the CSS file like so :

<link rel="stylesheet" type="text/css" href="/css/LEM2.css"/>

This view is called with the following code in the controller :

@Controller
@RequestMapping("/")
public class MainController
{
    @Autowired
    private UserService userService;

    @RequestMapping(value = {"/", "/home"}, method = RequestMethod.GET)
    private String main(HttpServletRequest request) 
    {
        if (request.getSession().getAttribute("loggedInUser") != null)
        {
            return "home";
        }
        else
        {
            return "login";
        }
    }
}

This all works perfectly fine and my home page displays nicely. The issue arises when using another controller. The code including the CSS in the view is EXACTLY the same but the controller is built like this :

@Controller
@RequestMapping("/equipmentTypes")
public class EquipmentTypeController
{
    @Autowired
    private MachineTypeService machineTypeService;

    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    private String list(ModelMap model) 
    {
        List<MachineType> equipmentTypes = machineTypeService.getAll();
        model.addAttribute("equipmentTypes", equipmentTypes);

        return "equipmentTypes";
    }
}

The browser is throwing tons of errors because it's erroneously looking for the CSS and images with the following URL : http://localhost:8080/equipmentTypes/css/LEM2.css

The previous web apps i worked on were just using Spring MVC and not Spring Boot. I came across a few differences between the 2 already and found myself browsing stack overflow a lot to understand them! I figure this might be one of those but I can't find the answer. I have always built my controllers like this as far as i remember, hoping someone will have encountered this before.

Martin
  • 1,977
  • 5
  • 30
  • 67

0 Answers0