解决springmvc中添加了静态资源访问路径之后就访问不到Controller路径的问题

访问不到Controller,也访问不到controller路径。
在这里插入图片描述

Controller代码:

/**
 * Created by 李柏霖
 * 2020/10/19 17:35
 */

package com.lbl.controller;


import com.lbl.domain.Employee;
import com.lbl.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    
    

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping(path = "/list",method = {
    
    RequestMethod.GET,RequestMethod.POST})
    public String list(Model model){
    
    
        System.out.println("employee....list.....run");
        //显示所有的person数据
        List<Employee> employeeList = employeeService.findAll();
        System.out.println(employeeList);
        model.addAttribute("employeeList",employeeList);
        return "list";
    }
}

原因是我在spingmvc的配置文件中加了过滤静态资源的配置,当我删除这段,又可以正常访问.

在这里插入图片描述

springmvc.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--只扫@Controller注解-->
    <context:component-scan base-package="com.lbl">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--过滤静态资源   .js .css png-->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/images/" mapping="/images/**" />
    <mvc:resources location="/js/" mapping="/js/**" />

    
    <context:annotation-config></context:annotation-config>
</beans>

这个问题,困扰了我两个小时.后面终于让我发现了问题所在.

原来是我springmvc中的配置写错了.

在这里插入图片描述

修改配置为<mvc:annotation-driven/>

springmvc.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--只扫@Controller注解-->
    <context:component-scan base-package="com.lbl">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--过滤静态资源   .js .css png-->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/images/" mapping="/images/**" />
    <mvc:resources location="/js/" mapping="/js/**" />

    <mvc:annotation-driven></mvc:annotation-driven>

</beans>

修改了配置之后 ,再运行.

在这里插入图片描述

可以访问到,问题解决!!!

猜你喜欢

转载自blog.csdn.net/qq_37924905/article/details/109167041