Spring MVC不适用于Java配置(源服务器没有找到目标资源的当前表示。)
我尝试创建一个简单的 Spring MVC 并通过 eclipse ide 使用一个简单的控制器在 tomcat 中运行:
@Controller
public class HomeController {
@RequestMapping("/home")
public String helloWorld(Model model) {
model.addAttribute("message", "home university!");
return "home";
}
}
我的其他配置:
webinitializer:
public class WebAppInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MvcConfig.class, ServiceConfig.class, AspectConfig.class, DaoConfig.class, DataSourceConfig.class);
context.setServletContext(servletContext);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispather", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
MvcSpringJavaConfig:
public class MvcConfig {
@Configuration
@ComponentScan(basePackages = "img.imaginary.controller")
@EnableWebMvc
public class MvcConfiguration {
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
}
结构:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
</web-app>
我的pom.xml: pom.xml
当 tomcat 启动并且我尝试打开该应用程序时在浏览器中,我得到 404 状态和描述:
源服务器尚未找到目标资源的当前表示或不想公开其存在。
我尝试更改控制器和 web.xml 以及其他配置,但我总是得到这个结果
我无法弄清楚我缺少什么才能让我的简单家庭控制器正确启动
I try to create a simple Spring MVC and run in tomcat via eclipse ide with one simple controller:
@Controller
public class HomeController {
@RequestMapping("/home")
public String helloWorld(Model model) {
model.addAttribute("message", "home university!");
return "home";
}
}
my other configs:
webinitializer:
public class WebAppInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MvcConfig.class, ServiceConfig.class, AspectConfig.class, DaoConfig.class, DataSourceConfig.class);
context.setServletContext(servletContext);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispather", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
MvcSpringJavaConfig:
public class MvcConfig {
@Configuration
@ComponentScan(basePackages = "img.imaginary.controller")
@EnableWebMvc
public class MvcConfiguration {
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
}
structure:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
</web-app>
my pom.xml:
pom.xml
and when tomcat starts and I try to open the app in the browser, I get
a 404 status and a description:
The source server has not found the current representation of the target resource or does not want to disclose its existence.
I tried to change the controller and the web.xml and other configs, but I always get this result
I can't figure out what I'm missing in order for my simple home controller to start correctly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将
@Configuration
添加到MvcConfig
类。还可以参考 这个:MvcConfiguration
类必须是静态的。you should add
@Configuration
toMvcConfig
class. also by referring to this:MvcConfiguration
class must be static.