785828852 2019-01-10 10:54 采纳率: 0%
浏览 485

SSM框架controller只要引入其他类就会报404错误

@Controller
@RequestMapping(value = "user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public String userLogin(User user){
        User u = userService.selectByUsername(user.getUsername());
        if(u!=null ){
            return "1";
        }
        System.out.println("sss");
        return "2";
    }

    /**
     * 测试
     * @return
     */
    @RequestMapping(value = "test",method = RequestMethod.POST)
    @ResponseBody
    public String test(){
        System.out.println("111");
        return "2";
    }
}
$("#login").on("click",function () {
        var u = $("#username").val();
        var p = $("#password").val();
        $.post("user/login",{"username":u,"password":p},function(res){
            if(res==1){
                alert("登录成功!");
            }else{
                alert("登录失败");
            }
        })
    })

ajax路径为login时前台会报错jquery.js:8623 POST http://localhost:8080/bookManage_war/user/login 404 (Not Found)
改为test时就能正常使用
只要加上user实体类或是service或是dao,就会找不到路径,只能简单实现返回一个值
web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
    <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- spring mvc的配置文件 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

applicationContext.xml

<context:component-scan base-package="com.wyj.service">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

    <context:annotation-config/>

    <mvc:annotation-driven />

    <!--开通静态资源的访问-->
    <mvc:default-servlet-handler />
    <!-- 读取配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 数据源 使用c3p0连接,需要jar包支持 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
        <property name="minPoolSize" value="${c3p0.minPoolSize}"/>
        <property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}"/>
        <property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/>
        <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
    </bean>

    <!-- 集成Mybatis(sqlSessionFactory )-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="typeAliasesPackage" value="com.wyj"></property>
    </bean>

    <!-- 映射帮助类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
        <property name="basePackage" value="com.wyj.dao"></property>
    </bean>

    <!-- 注册异常处理 -->
    <bean id="exceptionHandler" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView" value="error"></property>
        <property name="exceptionAttribute" value="exp"></property>
        <property name="exceptionMappings">
            <props>
                <prop key="cn.dtw.exception.MyException">myError</prop>
            </props>
        </property>
    </bean>

spring-mvc.xml

<context:component-scan base-package="com.wyj.controller">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <context:annotation-config/>

    <mvc:annotation-driven />

    <!--开通静态资源的访问-->
    <mvc:default-servlet-handler />

    <!-- 视图定位 -->
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".html" />
    </bean>
  • 写回答

4条回答

  • weixin_39198018 2019-01-10 11:15
    关注

    试试 把这个tomca删了,重新配个。再到server locations选第二项重启

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败