在 context.xml 中设置上下文路径时,除了第一页之外的所有页面都出现 404 错误
404 error on all pages except first page when setting context path in context.xml
我正在试验 Spring MVC 应用的 context.xml 文件中的路径属性。目前我将它设置为 path=""
并且一切正常,但它导致 URL 只显示 localhost:8080/
。
我想要完成的是将上下文路径更改为我的项目名称,以便 URL 实际上反映应用程序名称而不仅仅是指向根目录 (localhost:8080/项目名称)。我将 context.xml 更改为包含以下内容:
<Context path="/MDHIS_WebClient"/>
进行此更改后,只有登录页面有效,任何其他页面都会显示
404 error
我正在使用 Maven 进行构建,项目完全由注释驱动,没有 web.xml
。
编辑
这是我的 ApplicationInitializer class :
public class ApplicationInitializer extends WebMvcConfigurerAdapter implements WebApplicationInitializer
{
@Override
public void onStartup(ServletContext container) throws ServletException
{
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ApplicationConfiguration.class);
ctx.setServletContext(container);
ContextLoaderListener contextLoaderListener = new ContextLoaderListener(ctx);
ctx.getServletContext().addListener(contextLoaderListener);
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
ctx.refresh();
if (ctx.getEnvironment().getProperty("dbReset").compareTo("ON") == 0)
{
((DatabaseLoader)ctx.getBean("databaseLoader")).loadData();
}
LineCounter.countLines();
}
@Override
public void addViewControllers(ViewControllerRegistry registry)
{
registry.addViewController("/").setViewName("home");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
如何让它在后续页面的正确应用程序名称下搜索页面?谢谢
另一个编辑
我开始认为此更改也需要反映在我的应用程序的链接和控制器中才能正常工作。这是我的菜单当前调用我的通信状态页面的方式:
<a href="/communications/list">Communications</a>
控制器是这样设置来拦截调用的:
@Controller
@RequestMapping("/communications")
public class CommunicationController
{
@Autowired
private ConnectionService connectionService;
@RequestMapping(value = {"/list"}, method = RequestMethod.GET)
public String viewInterfaceCommunications(ModelMap model)
{
List<Connection> connections = connectionService.findAll();
model.addAttribute("connections", connections);
return "interfaceConnections";
}
}
你们如何设法在 URL 中填充网络应用程序的名称?
谢谢!
你做得对。
这是您可以尝试的方法。
- 清理并重新构建项目。
- 转到 "apache folder/work/Catalina/localhost" 并删除所有内容,然后重新部署项目。
希望有所帮助。
您可以尝试将此块添加到您的配置代码中吗?我假设您应用中的其他页面是 .html
@Bean
public ViewResolver htmlViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/yourPathToOtherPages/");
resolver.setSuffix(".html");
return resolver;
}
终于找到了解决方案,在任何配置中声明如下所示的 bean class 解决了问题并将上下文添加到 URL :
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer()
{
return factory -> factory.setContextPath("/LEM2");
}
请注意,这些 class 是 Spring 启动的一部分,因此您需要使用它才能正常工作。
我正在试验 Spring MVC 应用的 context.xml 文件中的路径属性。目前我将它设置为 path=""
并且一切正常,但它导致 URL 只显示 localhost:8080/
。
我想要完成的是将上下文路径更改为我的项目名称,以便 URL 实际上反映应用程序名称而不仅仅是指向根目录 (localhost:8080/项目名称)。我将 context.xml 更改为包含以下内容:
<Context path="/MDHIS_WebClient"/>
进行此更改后,只有登录页面有效,任何其他页面都会显示
404 error
我正在使用 Maven 进行构建,项目完全由注释驱动,没有 web.xml
。
编辑
这是我的 ApplicationInitializer class :
public class ApplicationInitializer extends WebMvcConfigurerAdapter implements WebApplicationInitializer
{
@Override
public void onStartup(ServletContext container) throws ServletException
{
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ApplicationConfiguration.class);
ctx.setServletContext(container);
ContextLoaderListener contextLoaderListener = new ContextLoaderListener(ctx);
ctx.getServletContext().addListener(contextLoaderListener);
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
ctx.refresh();
if (ctx.getEnvironment().getProperty("dbReset").compareTo("ON") == 0)
{
((DatabaseLoader)ctx.getBean("databaseLoader")).loadData();
}
LineCounter.countLines();
}
@Override
public void addViewControllers(ViewControllerRegistry registry)
{
registry.addViewController("/").setViewName("home");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
如何让它在后续页面的正确应用程序名称下搜索页面?谢谢
另一个编辑
我开始认为此更改也需要反映在我的应用程序的链接和控制器中才能正常工作。这是我的菜单当前调用我的通信状态页面的方式:
<a href="/communications/list">Communications</a>
控制器是这样设置来拦截调用的:
@Controller
@RequestMapping("/communications")
public class CommunicationController
{
@Autowired
private ConnectionService connectionService;
@RequestMapping(value = {"/list"}, method = RequestMethod.GET)
public String viewInterfaceCommunications(ModelMap model)
{
List<Connection> connections = connectionService.findAll();
model.addAttribute("connections", connections);
return "interfaceConnections";
}
}
你们如何设法在 URL 中填充网络应用程序的名称?
谢谢!
你做得对。
这是您可以尝试的方法。
- 清理并重新构建项目。
- 转到 "apache folder/work/Catalina/localhost" 并删除所有内容,然后重新部署项目。
希望有所帮助。
您可以尝试将此块添加到您的配置代码中吗?我假设您应用中的其他页面是 .html
@Bean
public ViewResolver htmlViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/yourPathToOtherPages/");
resolver.setSuffix(".html");
return resolver;
}
终于找到了解决方案,在任何配置中声明如下所示的 bean class 解决了问题并将上下文添加到 URL :
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer()
{
return factory -> factory.setContextPath("/LEM2");
}
请注意,这些 class 是 Spring 启动的一部分,因此您需要使用它才能正常工作。