u011559676
785828852
采纳率0%
2019-01-10 10:54 阅读 473

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 weixin_39198018 2019-01-10 11:15

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

    点赞 评论 复制链接分享
  • qq_34872748 L星火燎原 2019-01-10 11:46
    改成:
        if(u!=null ){
            return "1";
        }else{
                    return "2";
        }
    
    点赞 评论 复制链接分享
  • whandwho 大川里的小川人 2019-01-10 13:38

    http://localhost:8080/bookManage_war/user/login
    基本是这个路径的问题,
    你看看你的web.xml配置文件里面有没有 配置过滤那些后缀啥的

    如果后缀方面是OK的,那就是 工程目录的问题,就是路径问题,
    建议使用 postman 这个软件进行post测试。
    很有可能是工程名相关路径问题

    点赞 评论 复制链接分享
  • congge_1993 瞭望塔 2019-01-10 15:54

    @RequestBody

    点赞 评论 复制链接分享

相关推荐