您好我正在开发一个Spring MVC项目,控制器工作正常,但突然无法正常工作。当我点击控制器应该处理的URL时,继续抛出404错误。
对于我的控制器类,下面是我的控制器类的片段: 包mywebapp.controller;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import mywebapp.dao.model.interfaces.PetDao;
import mywebapp.model.Pet;
@Controller
@RequestMapping
public class PetController {
private static final Logger LOG = LoggerFactory.getLogger(PetController.class);
public static Logger getLog() {
return LOG;
}
@Autowired
@Qualifier("PetDaoJpaImpl")
private PetDao petDao;
public PetDao getPetDao() {
return petDao;
}
public void setPetDao(PetDao petDao) {
this.petDao = petDao;
}
@RequestMapping(value = "pet.htm", method = RequestMethod.GET)
public ModelAndView getAllPets() throws Exception {
getLog().info("Activating controller........");
List<Pet> petList = getPetDao().getAllPets();
getLog().info("Getting name in nameList......");
return new ModelAndView("petInfo", "petList", petList);
}
}
它应该映射一个“pet.htm”网址,但是当我点击这个网址时它会给我:
HTTP Status 404 - /MyWebapp/pet.htm
type Status report
message /MyWebapp/pet.htm
description The requested resource is not available.
Apache Tomcat/7.0.67
我的控制台中没有错误堆栈。有人有类似的问题吗?任何帮助将不胜感激。
仅供参考,这是我的应用程序上下文文件,我使用java来配置调度程序servlet:
package mywebapp.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class SpringAppContext implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(MywebappConfig.class);
// Manage the lifecycle of the root application context
servletContext.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(MywebappConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
==============================的更新 ====== ================================
MywebappConfig.java:
package mywebapp.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import mywebapp.controller.PetController;
import mywebapp.dao.model.impl.PetDaoJdbcImpl;
import mywebapp.dao.model.impl.PetDaoJpaImpl;
import mywebapp.dao.model.interfaces.PetDao;
import org.springframework.context.annotation.ComponentScan;
@Configuration
@ComponentScan(basePackages = "mywebapp")
@EnableWebMvc
@EnableTransactionManagement
public class MywebappConfig {
Properties getProperties() {
Properties properties = new Properties();
// properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
return properties;
}
@Bean(name = "getDataSource")
public DriverManagerDataSource getDataSource() {
DriverManagerDataSource datasource = new DriverManagerDataSource();
datasource.setDriverClassName("com.mysql.jdbc.Driver");
datasource.setUrl("jdbc:mysql://localhost/pet");
datasource.setUsername("admin");
datasource.setPassword("admin");
return datasource;
}
}
@Bean
public LocalContainerEntityManagerFactoryBean emf() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(getDataSource());
em.setPackagesToScan(new String[] { "mywebapp.model" });
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
em.setJpaProperties(getProperties());
return em;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public InternalResourceViewResolver getView() {
InternalResourceViewResolver view = new InternalResourceViewResolver();
view.setViewClass(JstlView.class);
view.setPrefix("/WEB-INF/jsp/");
view.setSuffix(".jsp");
return view;
}
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("i18n/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
=====================================的更新 == ================================ 服务器启动时的控制台日志:
Mar 04, 2016 1:02:51 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:MyWebapp' did not find a matching property.
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/7.0.67
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Dec 7 2015 13:07:11 UTC
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 7.0.67.0
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 7
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 6.1
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jre1.8.0_71
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_71-b15
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: C:\Users\Guanlun.Mu\Documents\workspace-sts-3.7.2.RELEASE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: C:\apache-tomcat-7.0.67
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\Guanlun.Mu\Documents\workspace-sts-3.7.2.RELEASE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\apache-tomcat-7.0.67
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\Guanlun.Mu\Documents\workspace-sts-3.7.2.RELEASE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\apache-tomcat-7.0.67\endorsed
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Mar 04, 2016 1:02:51 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_71\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files/Java/jre1.8.0_71/bin/server;C:/Program Files/Java/jre1.8.0_71/bin;C:/Program Files/Java/jre1.8.0_71/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\gradle-2.10\bin;C:\Program Files\MySQL\MySQL Server 5.7\bin;C:\windows\system32\config\systemprofile\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Java\jdk1.8.0_71\bin;C:\Program Files\nodejs\;C:\Program Files (x86)\Brackets\command;C:\Users\Guanlun.Mu\AppData\Roaming\npm;C:\sts-bundle\sts-3.7.2.RELEASE;;.
Mar 04, 2016 1:02:51 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 04, 2016 1:02:51 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 04, 2016 1:02:51 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1455 ms
Mar 04, 2016 1:02:51 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 04, 2016 1:02:51 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.67
Mar 04, 2016 1:02:52 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [165] milliseconds.
Mar 04, 2016 1:02:54 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 unneeded JARs during scanning can improve startup time and JSP compilation time.
Mar 04, 2016 1:02:54 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Mar 04, 2016 1:02:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 04, 2016 1:02:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 04, 2016 1:02:54 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3218 ms
答案 0 :(得分:0)
日志显示此
INFO: No Spring WebApplicationInitializer types detected on classpath
未部署您的Web应用程序。你可以确保你的项目编译得很好,你的战争中存在SpringAppContext
类。