0

i collided with some problems when i was writing web app. When i use URL like this http://localhost:8080/user i have no problems and my app work correctly, but when i use URL such as http://localhost:8080/some-intermediate-node/user i have pages without any style and java-script code. Belong i show my dispatcher servlet config

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.excbooks.controller")
public class ServletConfig  extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css");
        registry.addResourceHandler("/js/**").addResourceLocations("/js");
    }
    @Bean
    public InternalResourceViewResolver setupViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix("");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

and my controller

@Controller
public class MainController {
    {
        BasicConfigurator.configure();
    }
    @RequestMapping(value = "/d/login", method = RequestMethod.GET)
    public String login(Model model){
        return "log-in.html";
    }
    @RequestMapping(value = "/d/user", method = RequestMethod.GET)
    public String userProfile(Model model){
        User user = new User();
        user.setId(new BigInteger("1"));
        user.setUsername("Sashko");
        model.addAttribute("user",user);
        return "index.jsp";
    }
}

Link to my JSP index https://drive.google.com/file/d/0B42ezhAKqwZlcUEyVkR5amNIaDg/view?usp=sharing

6
  • 2
    Share code of index.jsp please? Commented Nov 12, 2016 at 17:47
  • ok. Here drive.google.com/file/d/0B42ezhAKqwZlcUEyVkR5amNIaDg/… Commented Nov 12, 2016 at 17:59
  • Post that in the question. Commented Nov 12, 2016 at 18:04
  • @JB Nizet, It is actual jsp. Commented Nov 12, 2016 at 18:14
  • 1
    And? How does that prevent from posting its content in the question? Commented Nov 12, 2016 at 18:17

2 Answers 2

1

Within your JSP, I suspect the javacsript/css files are pulled in roughly like...

<link rel="stylesheet" href="css/my.css">
<script src="js/my.js"></script>

The issue is that the href and src attributes specify relative URLs. They correctly point to your files when the URL is http://localhost:8080/user, but when the URL is http://localhost:8080/some-intermediate-node/user the browser will look for...

respectively.

Change the href and src attributes so that they start with a forward slash (/) character.

Sign up to request clarification or add additional context in comments.

Comments

0

Change CSS and javascript href to /css/** and /js/**

For example :

<link rel="stylesheet" href="/css/bootstrap.min.css">

<script src="/js/jquery-1.12.3.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.