1
從完整註釋切換到half-xml-half-annotation配置後,我的應用程序停止工作。每次請求的結果都是404,當我嘗試調試時,控制器方法沒有被調用。Spring MVC - 控制器未加載
的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"
version="3.1">
<servlet>
<servlet-name>spring-web</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-web-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<jsp-config>
<jsp-property-group>
<url-pattern>/*</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
</web-app>
彈簧網絡servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<!-- Scan the JavaConfig -->
<context:component-scan base-package="vn.fpt.fsoft.csms" />
</beans>
WebConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "vn.fpt.fsoft.csms")
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public final void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/WEB-INF/resources/");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver =
new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
HomeController.java
@Controller
public class HomeController {
@RequestMapping("/")
public String index() {
System.out.println("elelee");
return "index";
}
@RequestMapping("/login")
public String login() {
return "login";
}
}
Tomcat的日誌
15-Aug-2016 06:47:44.136 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
15-Aug-2016 06:47:44.871 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Initializing Spring FrameworkServlet 'spring-web'
爲什麼使用半註釋和半xml的方法? 什麼是包裝結構? –
@RavindraDevadiga因爲我發現在java配置中沒有替代jsp-config的選項,所以我不喜歡xml配置。該結構只是一個標準的Maven + Web結構。順便說一句,我試圖刪除和一切工作正常,但我有點需要它,我不知道什麼是錯的 –