【问题标题】:java spring URL mappingjava spring URL映射
【发布时间】:2016-05-17 18:03:37
【问题描述】:

我使用 maven web 应用程序,框架 spring。

使用 Netbeans IDE 和 Tomcat 服务器。

当我在 netbeans 中运行 web 时,浏览器中的 URL 是:

http://localhost:8080/mywebsite

使用此 URL 的网站无法读取事件 servlet 映射。

当我将 URL 更改为 http://localhost:8080/mywebsite/ 时,它运行良好。

这个案例的原因是什么?为什么我的网站没有在 URL 中自动添加字符“/”?

{更新}

config.java

公共类 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/*");
}

}

初始化器

@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
public class MyController {

//<editor-fold defaultstate="collapsed" desc="ADMIN">
@RequestMapping(value = "/", method = RequestMethod.GET)
public String login(ModelMap map) {
    return "admin/login";
}}

【问题讨论】:

  • @Abdelhak 我更新了我的问题

标签: java spring spring-mvc


【解决方案1】:

如果你打开http://localhost:8080/mywebsite,网络应用会尝试找到一些index.html文件(基于tomcat或http服务器配置)。

你的映射是@RequestMapping(value = "/", method = RequestMethod.GET),所以它会应用到http://localhost:8080/mywebsite/。如果你想使用你的控制器来处理http://localhost:8080/mywebsite,你可以尝试在你的映射值中使用*。这意味着,对于任何请求,如果未定义特定映射,则将应用默认映射。

【讨论】:

  • 我使用 spring 并覆盖 onStartup => 当我运行 http://localhost:8080/mywebsite 应用程序将归档 index.jsp 并显示正确。但是当我提交事件 => 调用 servlet 事件时,由于方向错误而失败。右边是http://localhost:8080/mywebsite/myevent 但现在是http://localhost:8080/myevent
猜你喜欢
  • 2016-01-02
  • 1970-01-01
  • 2012-10-21
  • 2012-10-22
  • 2017-01-18
  • 2016-02-16
  • 2012-04-30
  • 2011-01-18
  • 1970-01-01
相关资源
最近更新 更多