minamino
minamino

Reputation: 115

java spring URL mapping

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";
}}

Upvotes: 0

Views: 1905

Answers (1)

Mavlarn
Mavlarn

Reputation: 3883

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.

Upvotes: 1

Related Questions