Issue
I have test code in Java/Spring MVC. I want to run it and expect my Get request to look something like this:
localhost:9090/hello
but I see
localhost:9090/spring-mvc-app1/hello
I called the project spring-mvc-app1
, but I don’t understand why the name appears where logically it shouldn’t. Development environment - Eclipse.
The code itself:
package john.project.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ControllerOrder {
@GetMapping("/hello")
public String helloPage() {
return "order/hello";
}
@GetMapping("/goodbye")
public String goodbyePage() {
return "order/goodbye";
}
}
Config: SpringConfig.java:
package john.project.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;
@Configuration
@ComponentScan("john.project")
@EnableWebMvc
public class SpringConfig implements WebMvcConfigurer {
private final ApplicationContext applicationContext;
@Autowired
public SpringConfig(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
registry.viewResolver(resolver);
}
}
SpringMvcDispatcherServletInitializer.java:
package john.project.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
Solution
Logically there might be several applications on web server, and at deploy time each one by default takes a project name as Web Application Context. You can configer it in the project properties in Eclipse.
When you deploy your web application to tomcat it will trying to create an application context specified during deployment after processing the
web.xml
. If you have errors in the web application it might not started and context is not created. You need to resolve those errors and redeploy your application. You might have wrong configuration, that use a deprecated API, wrong library versions and inconsistent dependencies, other server problems. It's difficult to tell you what happened and why your application isn't started. But providing full stacktrace, project configuration might help to further troubleshoot the problem. Below is the solution to the other problem which is in your project code, that may be not exist or already resolved but not shown in your code posted. This helps to start the web application in the browser at context/spring-mvc-app1
after this context is registered with the webapp.
If you want to change Web Application Context path then you can read this article: How to change Web Application Context in Eclipse
If you run or debug a web project in Eclipse, the project name will be the default context root. For example, a project named “springmvc”, the default context root will be
http://localhost:8080/springmvc
Note, The context root tell you the URL of a deployed web application.
http://localhost:8080/{context_root}
How to update the web project context root in the Eclipse IDE.
- Eclipse – Web Project Settings
- Right click on the project, select Properties, Web Project Settings, update the context root here.
- Remove your web app from the server and add it back. The context root should be updated.
- If step 2 is failing, delete the server, create a new server and add back the web app.
New context root :
http://localhost:8080/abc
- Eclipse – Web Modules
- Double click on the Eclipse server plugin.
- Clicks on the Modules tab.
- Update the Path.
- Done. Restart the server.
New context root :
http://localhost:8080/abc
Answered By - Roman C
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.