奇葩问题:HTTP Status 404

昨天搭建了个Spring mvc + hibernate 的框架。

发现不管怎么样,页面都是无法正常跳转,都是 HTTP Status 404

研究了一下,发现该配置的都配置上去了。但是还是不行。

先贴一下代码,大家能不能看出来有什么问题。

1.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/aop
     	 http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
     	 http://www.springframework.org/schema/beans 
     	 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
     	 http://www.springframework.org/schema/context
     	 http://www.springframework.org/schema/context/spring-context-4.2.xsd
     	 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
     	 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p">
  	
  	
  	
	<!-- 加载数据库属性配置文件 -->
	<context:property-placeholder location="classpath:config/hibernate/db.properties"/>
	<!-- 数据库连接池c3p0配置 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
		destroy-method="close">
		<property name="jdbcUrl" value="${db.url}"></property> 
		<property name="driverClass" value="${db.driverClassName}"></property>
		<property name="user" value="${db.username}"></property>
		<property name="password" value="${db.password}"></property>
		<property name="maxPoolSize" value="40"></property> 
		<property name="minPoolSize" value="1"></property> 
		<property name="initialPoolSize" value="1"></property> 
		<property name="maxIdleTime" value="20"></property>
	</bean>
	
	<!-- 配置自动扫描的包 -->
	<context:component-scan base-package="com.jiang.*" />
	
	<!-- session工厂 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource"></ref>
		</property>
		<property value="classpath:config/hibernate/hibernate.cfg.xml"
			name="configLocation"></property>
		<!-- 自动扫描注解方式配置的hibernate类文件 -->
		<property name="packagesToScan">
			<list>
				<value>com.jiang.*</value>
			</list>
		</property>
	</bean>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<tx:annotation-driven/>

	<!-- 配置事务通知属性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 定义事务传播属性 -->
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="new*" propagation="REQUIRED" />
			<tx:method name="set*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="change*" propagation="REQUIRED" />
			<tx:method name="get*" propagation="REQUIRED" read-only="true" />
			<tx:method name="find*" propagation="REQUIRED" read-only="true" />
			<tx:method name="load*" propagation="REQUIRED" read-only="true" />
			<tx:method name="*" propagation="REQUIRED" read-only="true" />
		</tx:attributes>
	</tx:advice>
	

	<!-- 配置事务切点, 并把切点和事务属性关联起来 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.jiang.service.*.*(..))"
			id="txPointcut" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
	</aop:config>

</beans>

2.springmvc-servlet.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
	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-4.2.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/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">


	
	<!-- 定义注解驱动Controller方法处理适配器 -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/html; charset=UTF-8</value>
							<value>application/json;charset=UTF-8</value>
						</list>
					</property>
				</bean>
				<bean
					class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/html; charset=UTF-8</value>
							<value>application/json;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>

	<!-- 默认的注解映射的支持 -->
	<mvc:annotation-driven />

	<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
	</bean>
</beans>

3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="starter" version="2.4">
	<display-name>
	</display-name>
	<welcome-file-list>
    	<welcome-file>index.jsp</welcome-file>
  	</welcome-file-list>
	<!-- 字符串过滤器 -->
	<filter>   
    	<filter-name>CharacterEncodingFilter</filter-name>   
    	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
   		<init-param>   
			<param-name>encoding</param-name>   
			<param-value>utf-8</param-value>   
		</init-param>   
    	<init-param>
      		<param-name>forceEncoding</param-name>
      		<param-value>true</param-value>
    	</init-param>
	</filter>   
 	<filter-mapping>   
     		<filter-name>CharacterEncodingFilter</filter-name>   
     		<url-pattern>/*</url-pattern>   
	</filter-mapping>  	
	
	<listener>
    		<listener-class>
			org.springframework.web.context.ContextLoaderListener
        	</listener-class>
  	</listener>
  	
	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
				classpath*:config/spring/*applicationContext.xml
        	</param-value>
  	</context-param>
  	 	
         <!-- Spring MVC配置 -->
	<servlet>
		<servlet-name>springmvc4</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				classpath*:config/spring/*servlet.xml
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
    		<servlet-name>springmvc4</servlet-name>
    		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	
	<listener>
      		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <!-- hibernate 配置 -->
    <filter>
    	<filter-name>openSessionInViewFilter</filter-name>
    	<filter-class>
			org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
        </filter-class>
    	<init-param>
    	<param-name>singleSession</param-name>
     	<param-value>true</param-value>
    	</init-param>
  	</filter>
  	
  	<filter-mapping>
    	<filter-name>openSessionInViewFilter</filter-name>
    	<url-pattern>*.action</url-pattern>
  	</filter-mapping>	
  	
</web-app>


加上了log4j,发现执行action跳转的时候也是没有任何反应。

觉得@controller 这个根本没有启动。经研究发现了这个问题的原因:

        应用里边启动了2个容器,一个容器是spring root,还有个是springmvc。  springmvc 中无法获得spring root 中注册的bean。所以得不到@controller 这个注解对应的bean,执行action 无效了。  springroot 可以获得 springmvc 中注册的bean,所以整个应用可以正常加载,不会出现 @Component,@Service 之类的找不到的情况。


以下是解决方法。

方法1:

<!-- 配置自动扫描的包 -->
	<context:component-scan base-package="com.jiang.*" />

把代码放到 springmvc-servlet.xml 里边就能正常运行了。


方法2

applicationContext.xml 中不扫描@Controller 这个注解

改在springmvc-servlet.xml 中扫描.

参考代码:

applicationContext.xml 加入

<!-- 配置扫描的包 -->
	<context:component-scan base-package="com.jiang.*">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

springmvc-servlet.xml 加入

<!-- 配置扫描<span style="font-family: Arial, Helvetica, sans-serif;">的</span><span style="font-family: Arial, Helvetica, sans-serif;">包 --></span>
	<context:component-scan base-package="com.jiang.*">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值