idea 创建springmvc 导致的404错误

错误点一:@RequestMapping("ideademo")中的“ideademo”少了个/

@Controller
public class DemoController {
    @RequestMapping("/ideademo")
    public String ShowPage(){
        System.out.println("showpage...");
        return "showpage";
    }
}

错误点2:dispatcher-servlet.xml 中默认生成的命名空间不全<context:component-scan> 标红,需要补全

  • 1.xmlns:关于初始化bean的格式文件地址
  • 2.xmlns:xsi:辅助初始化bean
  • 3.xsi:context:关于spring上下文,包括加载资源文件
  • 4.xsi:schemaLocation:用于声明了目标名称空间的模式文档
<?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 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">

    <!-- 扫描linjie.springmvc.handler包及其子包下的所有文件,通过注解注册到IoC容器中 -->
    <context:component-scan base-package="cn.wq.spring.controller"/>
    <!-- 注意:prefix中有views文件夹,所有需要创建该文件夹 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

错误点3,web.xml未配置servlet-mapping,补上“/”的servlet-mapping

<!--不添加此配置导致404-->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
发布了53 篇原创文章 · 获赞 20 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/wqbs369/article/details/83340358