springmvc中controller路由出现404

[java]springmvc中controller路由出现404

problem

[java]springmvc中controller路由出现404

reason

可能原因有很多

  • idea配置不对
  • 编译配置不对
  • xml配置
  • jsp位置

solution

  1. 核对idea配置
    mac: idea->File -> Project Structure
    Snipaste_2023-12-27_11-16-25
    最重要的是 Artifacts,默认配置不对需要手动添加lib包
    Snipaste_2023-12-27_11-19-06

  2. 核对编译配置
    mac: idea -》Run -》Edit Configurations -》 点绿色的+号,选择 tomcat–>local -》出现弹窗

弹窗配置server

  • name: tomcat8
  • application server: 选择tomcat 的安装目录
  • URL: 改为 http://localhost:8080/ 默认是 http://localhost:8080/spring02_mvc_war_exploded/

弹窗配置Deployment

  • 点击 + 号
  • 选择: Artifact…
  • 选择完成后,中间区域出现 spring02-mvc:war exploded
  • Application context: / 默认是 /spring02_mvc_war_exploded
    Snipaste_2023-12-27_11-06-32
  1. xml配置
<!-- web/WEB-INF/web.xml 重要! 否则controller新的路由会出现404  
    修改 url-pattern 标签 修改前: <url-pattern>*.form</url-pattern>  修改后: <url-pattern>/</url-pattern>-->
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <!-- <url-pattern>*.form</url-pattern>-->
    <url-pattern>/</url-pattern>
</servlet-mapping>
  1. jsp配置
  • 配置文件: web/WEB-INF/dispatcher-servlet.xml
  • JSP 根据配置存放jsp文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    // <!--支持注解,例如@Controller,对应src下controller的包名-->
    <context:component-scan base-package="com.ah" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        // <!-- 配置JSP页面的位置:比如controller中return "hi" 对应文件位置是 /web/template/hi.jsp -->
        <property name="prefix">
            <value>/template/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

Snipaste_2023-12-27_11-39-47

相关推荐

  1. history解决刷新出现404的问题

    2023-12-28 14:58:05       62 阅读
  2. Flask蓝图找不到地址,访问404

    2023-12-28 14:58:05       39 阅读
  3. Vue3-40-- 动态

    2023-12-28 14:58:05       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 14:58:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 14:58:05       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 14:58:05       82 阅读
  4. Python语言-面向对象

    2023-12-28 14:58:05       91 阅读

热门阅读

  1. Bash快捷键

    2023-12-28 14:58:05       59 阅读
  2. docker 容器中 bash: vi: command not found

    2023-12-28 14:58:05       62 阅读
  3. ES-搜索

    ES-搜索

    2023-12-28 14:58:05      64 阅读
  4. redis如何批量删除key

    2023-12-28 14:58:05       53 阅读
  5. GPT Zero 是什么?

    2023-12-28 14:58:05       62 阅读
  6. ElasticSearch 常用运维命令收集

    2023-12-28 14:58:05       44 阅读
  7. Nestjs集成redis

    2023-12-28 14:58:05       40 阅读
  8. 向ES索引里面添加一个字段并更新旧文档数据

    2023-12-28 14:58:05       65 阅读
  9. Rust code: demo of message system

    2023-12-28 14:58:05       47 阅读
  10. 文件&IO

    文件&IO

    2023-12-28 14:58:05      51 阅读
  11. Flutter 官方状态管理 Provider基本使用

    2023-12-28 14:58:05       58 阅读
  12. 我的创作纪念日:感恩、感谢、感激!

    2023-12-28 14:58:05       64 阅读
  13. centos 安装 vsCode

    2023-12-28 14:58:05       54 阅读
  14. 激活函数:神经网络的生命之花

    2023-12-28 14:58:05       50 阅读
  15. 力扣:135. 分发糖果(贪心)

    2023-12-28 14:58:05       58 阅读
  16. Microsoft Edge 浏览器可能遇到的问题,和解决方法

    2023-12-28 14:58:05       88 阅读