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

夏V风 2015-11-09 04:08:56
近日参考网上资料自己搭建了一个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
...全文
173 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
夏V风 2015-11-10
  • 打赏
  • 举报
回复
引用 4 楼 shijing266 的回复:
<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、地址匹配问题, 要么地址写错了,不存在,要么就是地址没映射到,你稍微注意点就知道了
正解啊。我去,居然被这东西搞了一天没搞好。有时候自己就是看傻眼了。怎么看都发现不了这显而易见的问题。
夏V风 2015-11-09
  • 打赏
  • 举报
回复
引用 3 楼 xiakepan 的回复:
直接访问一个静态页面,看能找到吗?看是不是发布没有包含项目名。

不行。我现在没法请求到Controller,另外静态资源都在WEB-INF下面,不能直接用路径访问到的。我怀疑不知道是不是jar有什么问题。
  • 打赏
  • 举报
回复
<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、地址匹配问题, 要么地址写错了,不存在,要么就是地址没映射到,你稍微注意点就知道了
番茄鲨鱼面 2015-11-09
  • 打赏
  • 举报
回复
直接访问一个静态页面,看能找到吗?看是不是发布没有包含项目名。
夏V风 2015-11-09
  • 打赏
  • 举报
回复
这种前期就试过了。还是不行。所以也不知道问题出在哪里。
mtian2020 2015-11-09
  • 打赏
  • 举报
回复
<servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 改成 <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> 然后, @RequestMapping(value="/index.do") 改成 @RequestMapping(value="/index")

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧