这是前端的ajax代码:$.ajax({
                        type : "post",
                        url : "login/validate",
                        dataType : "json",
                        data : login,
                        success : function (msg) {
                            if (msg.uName != userName) {
                                $(".err-msg").html("用户名或密码错误")
                                $(".error-item").css("display", "block")
                            } else {
                                $(window).attr('location','/emall/views/back/index.jsp');
                            }
                        }
                    });Controller层的代码:    @RequestMapping("/login/validate")
    @ResponseBody
    public User loginValidate(String uName, String uPassword, HttpServletRequest request) {
        User user = userService.selectByLogin(uName, uPassword);
        System.out.println("uName = " + uName);
        if (user != null) {
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            session.setAttribute("state", true);
        } else {
            user = new User();
            user.setuName("验证失败");
        }
        return user;
    }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:context="http://www.springframework.org/schema/context"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-4.3.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "  >    <!-- ①:对com.jason包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
    <context:component-scan base-package="com.jason"/>
    <!-- 静态资源访问 -->
    <!--如果webapp下你新建了文件夹,想访问里面的静态资源,那么就要在这配置一下-->
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>    <!-- Configures the @Controller programming model
    <mvc:annotation-driven />-->
    <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter"/>
            </list>
        </property>
    </bean>    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>    <mvc:annotation-driven />    <!-- 配置视图解析器,把控制器的逻辑视频映射为真正的视图 -->
    <!-- /WEB-INF/jsp/start.jsp -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/views/" />
        <property name="suffix" value=".jsp" />
    </bean>    <!-- 配置dbcp数据库连接池 -->    <!-- <context:property-placeholder location="classpath:db.properties"/> -->
    <!--数据库配置 -->
    <bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>    </bean>    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        <property name="initialSize" value="${initialSize}"/>
        <property name="maxActive" value="${maxActive}"/>
        <property name="maxIdle" value="${maxIdle}"/>
        <property name="minIdle" value="${minIdle}"/>
        <property name="maxWait" value="${maxWait}"/>
    </bean>    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>    <!-- 注解管理事务 -->
    <tx:annotation-driven/>
    <!-- 拦截器 -->
    <mvc:interceptors>
        <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    </mvc:interceptors>
    <!-- 定义无Controller的path<->view直接映射 -->
    <!-- <mvc:view-controller path="/" view-name="redirect:/" /> -->    <!-- 配置会话工厂SqlSessionFactory -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:sqlmap/*Mapper.xml"/>
        <property name="typeAliasesPackage" value="com.jason.entity" />    </bean>    <!-- 在spring容器中配置mapper的扫描器产生的动态代理对象在spring的容器中自动注册,bean的id就是mapper类名(首字母小写)-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定扫描包的路径,就是mapper接口的路径,多个包中间以 半角逗号隔开   -->
        <property name="basePackage" value="com.jason.dao"/>
        <!-- 配置sqlSessionFactoryBeanName -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
</beans>web.xml  <!-- 配置springmvc的前端控制器 指向spring-mvc.xml 程序在启动的时候就加载springmvc 可以接受所有请求 load-on-startup:表示启动容器时初始化该Servlet; -->
  <servlet>
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 可以自定义servlet.xml配置文件的位置和名称, 默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value> classpath:spring/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 将前端URL请求和后台处理方法controller建立对应关系-->
  <servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>项目路径报错:[http-nio-8080-exec-7] WARN  [org.springframework.web.servlet.PageNotFound] - No mapping found for HTTP request with URI [/emall/views/user/login/validate] in DispatcherServlet with name 'springServlet'
一开始我将login.jsp直接放在webapp目录下就没有报错正常运行,但是一放到views/user目录下就开始报这个错,不知道为什么要报这个错,求大佬解答,感激不尽

解决方案 »

  1.   

    @RequestMapping("/login/validate")改成@RequestMapping("login/validate")试试
      

  2.   

    看下spring工程启动时的日志,看下日志打印的springmvc 映射路径是啥,再看前台页面调用时的请求路径,一对比就知道差别在哪儿了!
      

  3.   

    正确的映射路径应该是http://localhost:8080/emall/register/validate
    但是它自动映射到了http://localhost:8080/emall/views/user/register/validate
    该怎么让它改过来啊
      

  4.   

    ajax路径改成 /login/validate
      

  5.   

    我刚才也发现了,是ajax的路径问题