配置Spring MVC时出错?

时间:2015-08-09 18:47:13

标签: hibernate maven spring-mvc tomcat7

我使用Maven和Eclipse 4.5创建了一个Spring MVC应用程序 但是,在tomcat上部署之后,我得到404.无法理解我哪里出错了?

这是我的文件 - AppConfig.java

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.pratiksanglikar.controller")
public class AppConfig {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }
}

AppInitializer.java

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(container);

        ServletRegistration.Dynamic servlet = container.addServlet(
                "dispatcher", new DispatcherServlet(ctx));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }

}

AppController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pratiksanglikar.service.UserService;

@Controller
public class AppController {

    UserService service;

    @Autowired
    MessageSource messageSource;

    /*
     * This method will be called on form submission, handling POST request for
     * saving employee in database. It also validates the user input
     */
    @RequestMapping(value =  "UserLogin" , method = RequestMethod.GET)
    public String Login() {
        return "User Logged In Successfully";
    } 
}

UserService.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pratiksanglikar.crossover.dao.UserDAO;
import com.pratiksanglikar.crossover.model.User;


    @Component
    @Transactional
    public class UserService {

        @Autowired
        private UserDAO dao;

        public User findByUsername(String userName) {
            return dao.findByUsername(userName);
        }     
    }

当我部署并点击网址时 - localhost:8080 / CO / UserLogin.jsp,我在apache tomcat console上遇到以下错误 -

INFO: Server version:        Apache Tomcat/7.0.63
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jun 30 2015 08:08:33 UTC
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.63.0
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 8
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.2
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          x86
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jdk1.7.0_80\jre
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.7.0_80-b15
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         F:\CO\apache-tomcat-7.0.63
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         F:\CO\apache-tomcat-7.0.63
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.config.file=F:\CO\apache-tomcat-7.0.63\conf\logging.properties
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=F:\CO\apache-tomcat-7.0.63\endorsed
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=F:\CO\apache-tomcat-7.0.63
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=F:\CO\apache-tomcat-7.0.63
Aug 09, 2015 11:56:40 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.io.tmpdir=F:\CO\apache-tomcat-7.0.63\temp
Aug 09, 2015 11:56:40 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: Loaded APR based Apache Tomcat Native library 1.1.33 using APR version 1.5.1.
Aug 09, 2015 11:56:40 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
Aug 09, 2015 11:56:41 PM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized (OpenSSL 1.0.1m 19 Mar 2015)
Aug 09, 2015 11:56:41 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-apr-8080"]
Aug 09, 2015 11:56:41 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-apr-8009"]
Aug 09, 2015 11:56:41 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1271 ms
Aug 09, 2015 11:56:41 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 09, 2015 11:56:41 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.63
Aug 09, 2015 11:56:41 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive F:\CO\apache-tomcat-7.0.63\webapps\CO.war
Aug 09, 2015 11:56:42 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(F:\CO\apache-tomcat-7.0.63\webapps\CO\WEB-INF\lib\javax.servlet-api-3.1.0.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/s
Aug 09, 2015 11:56:45 PM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unne
 time and JSP compilation time.
Aug 09, 2015 11:56:45 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [402] milliseconds.
Aug 09, 2015 11:56:46 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Aug 09, 2015 11:56:46 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Aug 09 23:56:46 IST 2015]; root of context hierarchy
Aug 09, 2015 11:56:46 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
INFO: Registering annotated classes: [class com.pratiksanglikar.CO.configuration.AppConfig]
Aug 09, 2015 11:56:46 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.1.3.Final
Aug 09, 2015 11:56:47 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/UserLogin],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.pratiksanglikar.CO.controller.AppController.Login()
Aug 09, 2015 11:56:47 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 1575 ms
Aug 09, 2015 11:56:47 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive F:\CO\apache-tomcat-7.0.63\webapps\CO.war has finished in 5,875 ms
Aug 09, 2015 11:56:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory F:\CO\apache-tomcat-7.0.63\webapps\docs
Aug 09, 2015 11:56:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory F:\CO\apache-tomcat-7.0.63\webapps\docs has finished in 156 ms
Aug 09, 2015 11:56:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory F:\CO\apache-tomcat-7.0.63\webapps\examples
Aug 09, 2015 11:56:48 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory F:\CO\apache-tomcat-7.0.63\webapps\examples has finished in 228 ms
Aug 09, 2015 11:56:48 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory F:\CO\apache-tomcat-7.0.63\webapps\host-manager
Aug 09, 2015 11:56:48 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory F:\CO\apache-tomcat-7.0.63\webapps\host-manager has finished in 68 ms
Aug 09, 2015 11:56:48 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory F:\CO\apache-tomcat-7.0.63\webapps\manager
Aug 09, 2015 11:56:48 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory F:\CO\apache-tomcat-7.0.63\webapps\manager has finished in 50 ms
Aug 09, 2015 11:56:48 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory F:\CO\apache-tomcat-7.0.63\webapps\ROOT
Aug 09, 2015 11:56:48 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory F:\CO\apache-tomcat-7.0.63\webapps\ROOT has finished in 48 ms
Aug 09, 2015 11:56:48 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-apr-8080"]
Aug 09, 2015 11:56:48 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-apr-8009"]
Aug 09, 2015 11:56:48 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 6544 ms
Aug 09, 2015 11:57:04 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/CO/] in DispatcherServlet with name 'dispatcher'

2 个答案:

答案 0 :(得分:0)

在AppInitializer.java中试试这个

 servlet.addMapping("/*");

而不是

 servlet.addMapping("/");

答案 1 :(得分:0)

我认为

@ComponentScan(basePackages = "com.pratiksanglikar.controller")

也应该包括你的root包,例如

@ComponentScan(basePackages = {"the.root.package.of.your.app", "com.pratiksanglikar.controller"})