Spring项目中未找到文件问题
我试图启动并运行一个简单的Spring项目,但它似乎不起作用。没有错误。试图改变各种事情,但没有运气。下面是课程 AppInitializer.javaSpring项目中未找到文件问题,spring,spring-mvc,websphere,Spring,Spring Mvc,Websphere,我试图启动并运行一个简单的Spring项目,但它似乎不起作用。没有错误。试图改变各种事情,但没有运气。下面是课程 AppInitializer.java package com.spring.config; import java.util.Set; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; imp
package com.spring.config;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.scan("com.spring");
// Manages the lifecycle of the root application context
servletContext.addListener(new ContextLoaderListener(rootContext));
// Declare dispatcher servlet. Handles requests into the application
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
package com.spring.config;
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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.spring")
public class WebAppContextConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");
registry.addResourceHandler("/vendor/**").addResourceLocations("/vendor/**");
}
}
package com.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public String showIndex() {
return "Hello world";
}
@RequestMapping(value = "/statuscheck", method = RequestMethod.GET)
public @ResponseBody
String getStatusCheck() {
return "SUCCESS";
}
}
WebAppContextConfig.java
package com.spring.config;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.scan("com.spring");
// Manages the lifecycle of the root application context
servletContext.addListener(new ContextLoaderListener(rootContext));
// Declare dispatcher servlet. Handles requests into the application
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
package com.spring.config;
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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.spring")
public class WebAppContextConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");
registry.addResourceHandler("/vendor/**").addResourceLocations("/vendor/**");
}
}
package com.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public String showIndex() {
return "Hello world";
}
@RequestMapping(value = "/statuscheck", method = RequestMethod.GET)
public @ResponseBody
String getStatusCheck() {
return "SUCCESS";
}
}
AppConfig.java`
package com.spring.config;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
}
HelloController.java
package com.spring.config;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.scan("com.spring");
// Manages the lifecycle of the root application context
servletContext.addListener(new ContextLoaderListener(rootContext));
// Declare dispatcher servlet. Handles requests into the application
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
package com.spring.config;
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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.spring")
public class WebAppContextConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");
registry.addResourceHandler("/vendor/**").addResourceLocations("/vendor/**");
}
}
package com.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public String showIndex() {
return "Hello world";
}
@RequestMapping(value = "/statuscheck", method = RequestMethod.GET)
public @ResponseBody
String getStatusCheck() {
return "SUCCESS";
}
}
pom.xml
我将spring上下文(4.3.0-RELEASE)、SpringWebMVC(4.3.0-RELEASE)、javax.servlet-api和jstl作为依赖项
我正在使用WebSphere8.5.5.9
我尝试检查此url:
http://localhost:8080/SpringProject/statuscheck
我得到了
[WARNING ] SRVE0190E: File not found: /statuscheck
编辑-1
我在server.xml中有这个
<webApplication id="SpringProject" contextRoot = "SpringProject" location="SpringProject.war" name="SpringProject"/>
这是我的项目结构
编辑-4
我有以下项目方面
我有jdk 7
据我所知,您应该告诉spring在哪里优化配置类; 您是否尝试过以下方法:
package com.spring.config;
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.scan("com.spring");
rootContext.setConfigLocations(YOUR SPRING CFG FULL QUALIFIED CLASSES );
// Manages the lifecycle of the root application context
servletContext.addListener(new ContextLoaderListener(rootContext));
// Declare dispatcher servlet. Handles requests into the application
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
尝试查看此处和此处这可能会导致问题,请更改它
@RequestMapping(value = "/statuscheck", method = RequestMethod.GET)
public @ResponseBody
String getStatusCheck() {
return "SUCCESS";
}
到
验证server.xml中的上下文根如果您尝试-?您可以发布您的web.xml吗?您可以发布您的WAR内部结构吗?如果可能的话,请在启动期间查看websphere日志的最后部分。只是为了验证应用程序是否已成功启动。我添加了这一行:rootContext.setConfigLocations(“com.spring.config.WebAppContextConfig”);在这里,您可以找到一个使用类完成的简单示例。我使用了Tomcat8.5,所有的工作都很好。访问URL为Hi。。我下载了你的代码并在本地进行了尝试。。我有jdk 7和websphere liberty配置文件。。所以我删除了你的设置和类路径文件。请看我的编辑-4。想知道这是否与我的项目Faceals有关。如果您在Tomcat8中的server.xml中有任何内容,请与我们分享。5@javaMan当前位置这是一个maven项目。。。所以maven应该足以创造战争神器;我在server.xml中没有什么特别之处。我想知道您的AS是否支持Servlet3.X规范