我不熟悉Spring和Web应用程序,并且尝试学习,我在Spring5中创建了一个示例应用程序,代码可以很好地编译,并且我的测试通过了Eclipse,但是当我尝试在Tomcat下部署这场战争并尝试访问时我在tomcat日志中收到带有以下消息的404应用程序,我经历了多个线程报告了类似的问题,但是它对我不起作用,我在哪里出错?
错误
2018年7月23日23:53:01.420信息[ContainerBackgroundProcessor [StandardEngine [Catalina]]] org.apache.catalina.core.ApplicationContext.log在类路径上未检测到Spring WebApplicationInitializer类型
2018年7月23:53:01.807信息[ContainerBackgroundProcessor [StandardEngine [Catalina]]] org.apache.catalina.core.ApplicationContext.log初始化Spring FrameworkServlet'mvc-dispatcher'
我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>yess</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:persistence-beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
我的persistence-beans.xml如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- package to look for annotated classes -->
<context:component-scan base-package="com.yess.web.rest, com.yess.engine" />
<context:annotation-config />
<!-- we will manage transactions with annotations -->
<tx:annotation-driven />
<!-- data source for our database -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
<property name="url" value="jdbc:mysql://localhost/sample_db" />
<property name="username" value="root" />
<property name="password" value="test" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.yess.engine.domain.Sales</value>
<value>com.yess.engine.domain.ResponsiblePerson</value>
<value>com.yess.engine.domain.results.FinancialResults</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg type="javax.sql.DataSource" ref="dataSource" />
</bean>
</beans>
和控制器如下:
@Controller
@RequestMapping("/scorecard")
public class ScoreCardController {
public SalesResultsService salesResultService;
/**
* @param salesResultService
* the salesResultService to set
*/
@Autowired
public void setSalesResultService(SalesResultsService salesResultService) {
this.salesResultService = salesResultService;
}
@RequestMapping(value = "/matrix/{type}", method = RequestMethod.GET)
public @ResponseBody List<FinancialResults> getFinancialResults(@PathVariable(value = "type") String type,
@RequestParam(value = "fromDate") LocalDate fromDate, @RequestParam(value = "toDate") LocalDate toDate)
throws Exception {
List<FinancialResults> results = salesResultService.getSalesResultsListByDates(fromDate, toDate);
return results;
}
}