RT,代码如下:
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:p="http://www.springframework.org/schema/p"
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-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 自动扫包 -->
<context:component-scan base-package="cn.ziav.ssshp.*">
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.RestController" />
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<!-- 自动匹配AOP切面 -->
<aop:aspectj-autoproxy />
<!-- 1.配置数据源 -->
<context:property-placeholder location="classpath*:db.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:user="${user}" p:password="${password}" p:driverClass="${driver}"
p:jdbcUrl="${url}" p:initialPoolSize="${initPoolSize}" p:maxPoolSize="${maxPoolSize}"
p:maxIdleTime="300" p:maxIdleTimeExcessConnections="30"></bean>
<!-- 2.配置JPA的EntityManagerFactory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:generateDdl="true" p:showSql="true"></bean>
</property>
<!-- 配置实体类所在的包 -->
<property name="packagesToScan" value="cn.ziav.ssshp.*"></property>
<property name="jpaProperties">
<props>
<!-- 二级缓存相关 -->
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory
</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<!-- 读数据远大于写数据时启用 -->
<!-- <prop key="hibernate.cache.use_minimal_puts">true</prop> -->
<!-- <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop> -->
<!-- hibernate 基本属性 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- 3.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!-- 5.配置 SpringData -->
<!-- 加入 jpa 的命名空间 -->
<!-- base-package: 扫描 Repository Bean 所在的 package -->
<jpa:repositories base-package="cn.ziav.ssshp.*"></jpa:repositories>
</beans>
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
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-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="cn.ziav.ssshp.*" />
<!-- 开启mvc -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<!-- 配置试图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".html" />
<!-- 支持注解式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>ssshp</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
TestController.java
@RestController
public class TestController {
@RequestMapping("/test/{test}")
public String test(@PathVariable String test) {
return test;
}
}
请求地址为:
http://localhost:8081/ssshp/test/jkljkle...
返回结果:
HTTP ERROR 404
Problem accessing /ssshp/test/jkljklew. Reason:
Not Found Powered by Jetty:// 9.3.8.v20160314
但是请求地址改成
http://localhost:8081/ssshp/index.html
可以正常显示。。。
这个问题网上搜了两天,没发现哪里错了,心累,各位大大求解QAQ
Parce que la page jkljklew.html n'existe pas.
Ajoutez un journal ici pour comprendre
Pouvez-vous publier la structure des répertoires de votre projet ?
Selon votre situation d'erreur, il semble que index.html se trouve dans le répertoire racine du répertoire de version et que DispatcherServlet n'est pas utilisé, il est donc accessible. Pouvez-vous voir si l'erreur ci-dessus est due à aucun gestionnaire ? Il se peut que le contrôleur n'ait pas été scanné.
Je l'ai essayé, et j'ai moi-même traversé un gros trou et j'en suis finalement ressorti.
Si ce n'est pas de votre faute, retirez le projet et jetez un œil. Cette petite information ne peut pas résoudre l'erreur
Au fait, il y a une autre bonne chose : BeanPostProcessor
Vous pouvez le découvrir sur Baidu. Peut être utilisé pour détecter si votre bean est chargé par le conteneur Spring IoC