[英]java spring URL mapping
我使用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";
}}
如果打開http://localhost:8080/mywebsite
,則Web應用將嘗試查找一些index.html
文件(基於tomcat或http服務器配置)。
而且您的映射是@RequestMapping(value = "/", method = RequestMethod.GET)
,因此它將應用於http://localhost:8080/mywebsite/
。 如果要使用控制器來處理http://localhost:8080/mywebsite
,則可以嘗試在映射值中使用*
。 這意味着,對於任何請求,如果沒有定義特定的映射,則將應用默認映射。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.