Java GET没有映射
我通常使用xml配置,但现在只想使用java配置,但我的代码有点问题,我不知道要更改什么,因为一切似乎都很好 在glass fish上运行spring应用程序后,我遇到此错误:Java GET没有映射,java,spring,Java,Spring,我通常使用xml配置,但现在只想使用java配置,但我的代码有点问题,我不知道要更改什么,因为一切似乎都很好 在glass fish上运行spring应用程序后,我遇到此错误: [glassfish 5.0][WARNING][[org.springframework.web.servlet.PageNotFound][tid:_ThreadID=30 _ThreadName=http-listener-1(2)][timeMillis:1619543716623][levelValue:900
[glassfish 5.0][WARNING][[org.springframework.web.servlet.PageNotFound][tid:_ThreadID=30 _ThreadName=http-listener-1(2)][timeMillis:1619543716623][levelValue:900][[GET/spring\u project-1.0-SNAPSHOT/home/showHome/]无映射。
这是我的控制器:
@Controller
@RequestMapping("/home")
public class HomeController {
@RequestMapping("/showHome")
public String showHome() {
return "home";
}
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.config")
public class AppConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
public class AppIntializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(AppConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
我的视图解析器:
@Controller
@RequestMapping("/home")
public class HomeController {
@RequestMapping("/showHome")
public String showHome() {
return "home";
}
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.config")
public class AppConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
public class AppIntializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(AppConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
}
和WebAppInitializer:
@Controller
@RequestMapping("/home")
public class HomeController {
@RequestMapping("/showHome")
public String showHome() {
return "home";
}
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.config")
public class AppConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
public class AppIntializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(AppConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
您需要在此行中指定HTTP方法
@RequestMapping("/showHome")
或者只是使用
@GetMapping
此外,请阅读下面的页面,它可能会很有用
从错误中可以看出,您正在尝试访问
GET/spring\u project-1.0-SNAPSHOT/home/showHome/
。尝试检查您正在访问的路径,我认为它应该是/home/showHome/
仍然不起作用。当我使用web.xml
/home/showHome/
!=<代码>/home/showHome