我正在试验我的Spring MVC应用程序的context.xml文件中的path属性。目前我将其设置为path=""
,一切正常,但结果是网址只显示localhost:8080/
。
我想要完成的是将上下文路径更改为我的项目名称,以便URL实际反映应用程序名称,并且不仅指向根目录(localhost:8080 / nameOfProject)。我更改了context.xml以包含以下内容:
<Context path="/MDHIS_WebClient"/>
通过此更改,只有登录页面有效,而任何其他页面都有
404错误
我正在使用Maven进行构建,项目完全由注释驱动,没有web.xml
。
修改
这是我的ApplicationInitializer类:
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";
}
}
您是如何设法在网址中填充网络应用的名称的?
谢谢!
答案 0 :(得分:0)
您做得正确。
这是您可以尝试的。
希望有帮助。
答案 1 :(得分:0)
您可以尝试将此块添加到您的配置代码中吗?我假设您应用中的其他页面是.html
@Bean
public ViewResolver htmlViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/yourPathToOtherPages/");
resolver.setSuffix(".html");
return resolver;
}
答案 2 :(得分:0)
最后找到了解决方案,在任何config类中声明一个像下面这样的bean都可以解决问题,并将上下文添加到URL中:
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer()
{
return factory -> factory.setContextPath("/LEM2");
}
请注意,这些类是Spring Boot的一部分,因此您需要使用它才能起作用。