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.配置數(shù)據(jù)源 -->
<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>
<!-- 讀數(shù)據(jù)遠大于寫數(shù)據(jù)時啟用 -->
<!-- <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
可以正常顯示。。。
這個問題網(wǎng)上搜了兩天,沒發(fā)現(xiàn)哪里錯了,心累,各位大大求解QAQ
歡迎選擇我的課程,讓我們一起見證您的進步~~
Because the jkljklew.html page does not exist.
@RestController public class TestController {
@RequestMapping("/test/{test}") public String test(@PathVariable String test) { return test; } }
Add a log here and you will understand
Can you post the directory structure of your project and take a look?
According to your error situation, it seems that index.html is in the root directory of the release directory, without using DispatcherServlet, so it can be accessed. Can you see if the above error is due to no handler? It should be that the Controller has not been scanned. Come on.
I gave it a try, and I went through a big hole myself, and finally came out.
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-servlet.xml</param-value>
</init-param>
我的配置文件,但是目錄結構是我把 spring-servlet.xml 放在了 conf 目錄下,
導致配置文件沒被掃描到,所以404了。(記得看過一句說是 classpath*:
會去 classpath 下搜尋所有匹配的文件,我理解成只要寫文件名,
spring會自動去classes目錄以及子目錄搜文件,今天出錯后又去好好看了下,
才發(fā)現(xiàn)理解錯了,具體百度 classpath classpath* 就有好多)
If this is not your fault, take out the project and have a look. This little information cannot solve the error
By the way, there’s another good thing: BeanPostProcessor
You can find it out on Baidu. Can be used to detect whether your bean is loaded by the spring IoC container