我正在使用MVC配置将项目从Spring 2.5更新到5 我尝试使基于Java的配置出现一些问题,这是我的代码:
Web.xml
<web-app
xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Publisher Portal</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.netsol.publisher.config.AppConfig
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>1000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>publisher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.netsol.publisher.config.SpringWebMvcConfig</param-value>
</init-param>
<description>Spring MVC Dispatcher Servlet</description>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>publisher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
我的AppConfig.java
@Configuration
@ComponentScan(excludeFilters=@Filter(org.springframework.stereotype.Controller.class)})
public class AppConfig {
private static final Logger LOG = LoggerFactory.getLogger(AppConfig.class);
@Autowired
private Environment env;
}
还有我的SpringWebMvcConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.netsol.publisher.controller" })
public class SpringWebMvcConfig implements WebMvcConfigurer {
private static final int CACHE_PERIOD = 31556926; // one year
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/jsp/", ".jsp");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//this will map uri to jsp view directly without a controller
registry.addViewController("/hi").setViewName("hello");
registry.addViewController("/").setViewName("home.do");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
// Serving static files using the Servlet container's default Servlet.
configurer.enable();
}
@Override
public void addFormatters(FormatterRegistry formatterRegistry) {
// add your custom formatters
}
@Bean
PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource("publisher-portal.properties"));
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping () {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Integer.MAX_VALUE - 2);
Properties urlProperties = new Properties();
urlProperties.put("/home", "LoginController");
mapping.setMappings(urlProperties);
return mapping;
}
}
我的控制器:
@Controller
@RequestMapping(value = "/home")
public class LoginController extends BaseController {
static final String sccsId = "%TC-INFO%";
@Autowired
private PublisherFacade publisherFacade;
private static Log log = LogFactory.getLog(LoginController.class);
@RequestMapping(value = "/index.do", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView showLoginPage(HttpServletRequest request, HttpServletResponse response)
throws Exception {
return getLoginView(request, Constants.USER_LOGIN_PAGE);
}
问题是
INFO: Initializing Spring FrameworkServlet 'publisher'
- FrameworkServlet 'publisher': initialization started
- Refreshing WebApplicationContext for namespace 'publisher-servlet': startup date [Wed Sep 19 16:12:08 ART 2018]; parent: Root WebApplicationContext
- No annotated classes found for specified class/package [com.netsol.publisher.config.SpringWebMvcConfig]
- JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
- Looking for @ControllerAdvice: WebApplicationContext for namespace 'publisher-servlet': startup date [Wed Sep 19 16:12:08 ART 2018]; parent: Root WebApplicationContext
- FrameworkServlet 'publisher': initialization completed in 1146 ms
sep 19, 2018 4:12:10 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet [PendingOrderCheckerServlet] as unavailable
sep 19, 2018 4:12:10 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet [PendingOrderCheckerServlet] in web application [/publisher-portal] threw load() exception
java.lang.ClassNotFoundException: com.netsol.publisher.batch.PendingOrderCheckerServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1309)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1137)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:546)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:527)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:150)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1044)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:983)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4978)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5290)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1420)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1410)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
sep 19, 2018 4:12:10 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
sep 19, 2018 4:12:10 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
sep 19, 2018 4:12:10 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 31382 ms
- No mapping found for HTTP request with URI [/publisher-portal/home/index.do] in DispatcherServlet with name 'publisher'
- No mapping found for HTTP request with URI [/publisher-portal/home/index.do] in DispatcherServlet with name 'publisher'
我想知道我的配置是否正常以及为什么找不到控制器的映射