使用maven搭建的ssm框架能够访问jsp页面但是就是无法访问controller层,代码如下

controller层是这样的
@Controller
@RequestMapping("web_User")
public class UserController {
private static final Logger logger = Logger.getLogger(UserController.class);

@Resource
private UserService userService;
/**
* 获取具体用户信息
* */
@RequestMapping(value = { "getUserById_test" }, method = { RequestMethod.POST })
public @ResponseBody
Map getVideoById(@RequestParam long id) {
User user = userService.finduserById(id);
Map map = new HashMap();
map.put("result", user);
return map;
}
}
applicationcontext是这样的
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">

<mvc:annotation-driven />

<context:annotation-config />
<mvc:default-servlet-handler />
<context:component-scan base-package="com.gp.demo.*" /> 
<!-- 启用缓存注解功能 -->
 <cache:annotation-driven cache-manager="cacheManager"/>  
 <!-- 缓存管理器工厂加载ehcache的配置 -->
 <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
    <property name="configLocation" value="classpath:ehcache.xml"/>  
</bean>  
<!-- 声明缓存管理器 -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">  
    <property name="cacheManager" ref="cacheManagerFactory"/>  
</bean> 

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:jdbc.properties</value>
    </property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>com.gp.demo.domain</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
    <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="${jdbc.initialSize}" />
</bean>

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

<!-- 连接事务的注解配置 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 配置事务的传播特性 hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="Find*" propagation="REQUIRED" read-only="true" />
        <tx:method name="is*" propagation="REQUIRED" read-only="true" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="send*" propagation="REQUIRED" />
        <tx:method name="accept*" propagation="REQUIRED" />
        <tx:method name="approve*" propagation="REQUIRED" />
        <tx:method name="count*" propagation="REQUIRED" />
        <tx:method name="sort*" propagation="REQUIRED" />
        <tx:method name="set*" propagation="REQUIRED" />
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="create*" propagation="REQUIRED" />
        <tx:method name="insert*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="merge*" propagation="REQUIRED" />
        <tx:method name="del*" propagation="REQUIRED" />
        <tx:method name="remove*" propagation="REQUIRED" />
        <tx:method name="put*" propagation="REQUIRED" />
        <tx:method name="use*" propagation="REQUIRED" />
        <tx:method name="register*" propagation="REQUIRED" />
        <tx:method name="search*" propagation="REQUIRED" />
        <tx:method name="get*" propagation="REQUIRED" />
        <tx:method name="cancel*" propagation="REQUIRED" />
        <tx:method name="login*" propagation="REQUIRED" read-only="true" />
        <tx:method name="count*" propagation="REQUIRED" read-only="true" />
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />
        <tx:method name="list*" propagation="REQUIRED" read-only="true" />
        <!-- <tx:method name="*" read-only="true" /> -->
    </tx:attributes>
</tx:advice>

<!-- 那些类的哪些方法参与事务 -->
<aop:config>
    <aop:pointcut id="allManagerMethod"
        expression="execution(public * com.gp.demo.service.*.*(..))" />
    <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
</aop:config>

<mvc:annotation-driven />
<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<bean id="conversionService"
    class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />

<bean 
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
    class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="order" value="0" />
    <property name="cacheSeconds" value="0" />
    <property name="webBindingInitializer">
        <bean
            class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">

        </bean>
    </property>
    <property name="messageConverters">
        <list>
            <bean
                class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
            <bean
                class="org.springframework.http.converter.StringHttpMessageConverter" />
            <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
            <bean
                class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
            <bean
                class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
        </list>
    </property>
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

 <!-- 定时邮件 -->

<!-- end 定时邮件 -->



<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 设置上传文件的最大尺寸为100MB -->
        <property name="maxUploadSize">
            <value>104857600</value>
        </property>
</bean>

<!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->
<!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->
<bean id="exceptionResolver"
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 -->
            <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>
        </props>
    </property>
</bean>
<!-- <bean id="lunxunqidong" class="com.wiwoworld.oa.web.NoticeController" scope="singleton" init-method="searchMailnotice"></bean> -->

4个回答

springmvc的拦截器配置了吗

这是我一个小项目的springmvc-conig.xml代码 你可以看看

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<!-- 自动扫描该包,SpringMVC会将包下用了@controller注解的类注册为Spring的controller -->
<context:component-scan base-package="top.dreamyy.pattern.controller"/>
<!-- 设置默认配置方案 -->
<mvc:annotation-driven/>
<!-- 使用默认的Servlet来响应静态文件 -->
<mvc:default-servlet-handler/>

<!-- 定义Spring MVC的拦截器 -->
<mvc:interceptors>
    <mvc:interceptor>
        <!-- 拦截所有请求 -->
        <mvc:mapping path="/**"/>
        <!-- 自定义判断用户权限的拦截类 -->  
        <bean class="top.dreamyy.pattern.interceptor.AuthorizedInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>


<!-- 视图解析器  -->
 <bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <!-- 前缀 -->
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    <!-- 后缀 -->
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

 <bean id="multipartResolver"  
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    <!-- 上传文件大小上限,单位为字节(10MB) -->
    <property name="maxUploadSize">  
        <value>10485760</value>  
    </property>  
    <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
    <property name="defaultEncoding">
        <value>UTF-8</value>
    </property>
</bean>

m0_37795698
m0_37795698 谢谢你的回答,不过,加了拦截器也不管用,还是报404,我只是再写一个demo还没到实际应用所以就还没具体的拦截规则
2 年多之前 回复

你这样很不好看,还不如把你项目上窜给你解决。每个人的配置都有一定的区别

那是不是action的路径对不上找不到相应的controller才404,你看看控制台语句

m0_37795698
m0_37795698 我导入了之前的项目,还是一样的问题?是不是myeclipse的配置有问题呢?
2 年多之前 回复
m0_37795698
m0_37795698 控制台没有输出,只是报404,我怀疑连拦截器都没进去,不过能访问到jsp页面。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问