我嘗試創建一個簡單的 Spring MVC 并使用一個簡單的控制器通過 eclipse ide 在 tomcat 中運行:
@Controller
public class HomeController {
@RequestMapping("/home")
public String helloWorld(Model model) {
model.addAttribute("message", "home university!");
return "home";
}
}
我的其他配置:
網路初始化器:
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;
}
}
}
結構體:

網頁.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 和其他配置,但我總是得到這個結果
我無法弄清楚我缺少什么才能讓我的簡單家庭控制器正確啟動
uj5u.com熱心網友回復:
你應該添加@Configuration到MvcConfig課堂上。也可以參考這個:
任何嵌套的配置類都必須宣告為靜態的。
MvcConfiguration類必須是靜態的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/456725.html
