简体   繁体   English

java spring URL映射

[英]java spring URL mapping

I using maven web application, framework spring. 我使用Maven Web应用程序,框架为Spring。

Using Netbeans IDE and Tomcat server. 使用Netbeans IDE和Tomcat服务器。

When I run web in netbeans, URL in browser is: 当我在netbeans中运行Web时,浏览器中的URL为:

http://localhost:8080/mywebsite http:// localhost:8080 / mywebsite

With this URL website cannot read event servlet mapping. 使用此URL,网站无法读取事件servlet映射。

When I change URL to http://localhost:8080/mywebsite/ then It run good. 当我将URL更改为http:// localhost:8080 / mywebsite /时,它运行良好。

What is reason for this case? 发生这种情况的原因是什么? Why my website don't auto add character "/" in URL? 为什么我的网站没有在URL中自动添加字符“ /”?

{update} {更新}

config.java config.java

public class Config extends WebMvcConfigurerAdapter { 公共类Config扩展了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";
}}

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). 如果打开http://localhost:8080/mywebsite ,则Web应用将尝试查找一些index.html文件(基于tomcat或http服务器配置)。

And you mapping is @RequestMapping(value = "/", method = RequestMethod.GET) , so it will apply to http://localhost:8080/mywebsite/ . 而且您的映射是@RequestMapping(value = "/", method = RequestMethod.GET) ,因此它将应用于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. 如果要使用控制器来处理http://localhost:8080/mywebsite ,则可以尝试在映射值中使用* It means, for any request, if there is no specific mapping defined, and that default mapping will be applied. 这意味着,对于任何请求,如果没有定义特定的映射,则将应用默认映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM