0

I using maven web application, framework spring.

Using Netbeans IDE and Tomcat server.

When I run web in netbeans, URL in browser is:

http://localhost:8080/mywebsite

With this URL website cannot read event servlet mapping.

When I change URL to http://localhost:8080/mywebsite/ then It run good.

What is reason for this case? Why my website don't auto add character "/" in URL?

{update}

config.java

public class Config extends WebMvcConfigurerAdapter {

@Bean
public UrlBasedViewResolver setupViewResolver() {
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();
    resolver.setPrefix("/WEB-INF/html/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
}

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

}

Initializer

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(Config.class);
    ctx.setServletContext(servletContext);
    ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}

controller

@Controller
public class MyController {

//<editor-fold defaultstate="collapsed" desc="ADMIN">
@RequestMapping(value = "/", method = RequestMethod.GET)
public String login(ModelMap map) {
    return "admin/login";
}}
1
  • @Abdelhak I updated in my question Commented Feb 8, 2016 at 6:33

1 Answer 1

1

If you open http://localhost:8080/mywebsite, the web app will try to find some index.html file(based on tomcat or http server configuration).

And you mapping is @RequestMapping(value = "/", method = RequestMethod.GET), so it will apply to http://localhost:8080/mywebsite/. If you want to use your controller to handle http://localhost:8080/mywebsite, you can try to use * in your mapping value. It means, for any request, if there is no specific mapping defined, and that default mapping will be applied.

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

1 Comment

I using spring and I override onStartup => When I run http://localhost:8080/mywebsite app will file index.jsp and show right. But when I submit event => call servlet event then failed because directions wrong. Right is http://localhost:8080/mywebsite/myevent but now It is http://localhost:8080/myevent

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.