当前位置: 代码迷 >> Java Web开发 >> spring MVC注解没法请求,请大神指教
  详细解决方案

spring MVC注解没法请求,请大神指教

热度:76   发布时间:2016-04-13 22:11:31.0
spring MVC注解无法请求,请大神指教。
近日参考网上资料自己搭建了一个springMVC和hibernate的框架,好不容易解决各种问题之后,运行之后,请求Controller居然404,无法请求到,麻烦大家帮忙看看错误在哪。看了半天没发现哪错了。配置和代码结构如下:
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" id="WebApp_ID" version="3.1">
  <display-name>web</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/spring_*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/config/springMVC_*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

spring_applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd">
 <!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->  
    <context:component-scan base-package="com.web">
      <context:exclude-filter type="regex" expression="com.web.controller"/>
    </context:component-scan> 
    <!-- 引入jdbc配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/config/jdbc.properties" />
</bean> 
    <!-- 选择hibernate还是mybatis -->
<import resource="config_hibernate.xml"/>
<!-- <import resource="config_mybatis.xml"/> -->

    <!-- dataSource 配置 -->  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
        <property name="driverClassName" value="${jdbc.driverClassName}" />       
        <property name="url" value="${jdbc.url}" />  
        <property name="username" value="${jdbc.username}" />  
        <property name="password" value="${jdbc.password}" />  
        <!-- 配置初始化大小、最小、最大 -->  
        <property name="initialSize" value="1" />  
        <property name="minIdle" value="1" />  
        <property name="maxActive" value="20" />  
  
        <!-- 配置获取连接等待超时的时间 -->  
        <property name="maxWait" value="60000" />  
  
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
        <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
        <property name="minEvictableIdleTimeMillis" value="300000" />  
        <property name="validationQuery" value="SELECT 'X' from dual" />  
        <property name="testWhileIdle" value="true" />  
        <property name="testOnBorrow" value="false" />  
        <property name="testOnReturn" value="false" />  
    </bean>  
</beans>

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"
xsi:schemaLocation=" 
           http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/mvc 
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" >
<!-- 约定优于配置,约定优于配置 -->
<!-- 使注解生效 -->
<mvc:annotation-driven />
<!-- 扫描所有的controller -->
<context:component-scan base-package="com.web.controller" />

<!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd -->
<mvc:resources mapping="/img/**" location="/view/img/"/>
<mvc:resources mapping="/js/**" location="/view/js/"/>
<mvc:resources mapping="/css/**" location="/view/css/"/>

<!-- InternalResourceViewResolver默认的就是JstlView所以这里就不用配置viewClass了 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/html/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 启用基于注解的处理器映射,添加拦截器,类级别的处理器映射 -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <list>
                <bean class="com.fsj.spring.util.MyHandlerInterceptor"/>
            </list>
        </property>
</bean> -->

<!-- 
配置一个基于注解的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射,
有人说该bean要放在context:component-scan前面,要不然不起作用,但我试的放后面也可以啊。
-->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="cacheSeconds" value="0" />
    <property name="webBindingInitializer">
        <bean class="com.fsj.spring.util.MyWebBinding" />
    </property>
</bean> -->

</beans> 

TestController.java
package com.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.web.bean.AuthTerminal;
import com.web.service.TestService;

@Controller
@RequestMapping(value="/Test")
public class TestController {

@Resource
private TestService service;

@RequestMapping(value="/index.do")  
    public void index() {  
AuthTerminal au=new AuthTerminal();
au.setValidFlag(true);
List<AuthTerminal> list=service.getAllUser();
for(AuthTerminal a:list){
System.out.println("ID="+a.getAuthTerminalId()+",Type="+a.getAuthTerminalType());
}
    }  
}

我的访问路径是http://localhost:8080/web/Test/index.do
------解决思路----------------------
<param-value>/WEB-INF/config/springMVC_*.xml</param-value>

你web.xml里面配置的mvc的配置文件名称跟你的实际文件名称不一样,改成<param-value>/WEB-INF/config/springMvc_*.xml</param-value>

其实404 有很多原因:
1、Controller初始化的时候,没被加载,这说明要么配置文件没扫描到,要么配置文件没加载(项目初始化的时候,你可以看到日志有打印控制器信息的,如果没打印,就是没加载

2、地址匹配问题, 要么地址写错了,不存在,要么就是地址没映射到,你稍微注意点就知道了
 
  相关解决方案