【问题标题】:Intellij Idea Spring 404 – Not FoundIntellij Idea Spring 404 – 未找到
【发布时间】:2019-05-06 11:49:10
【问题描述】:

在 Tomcat 上启动项目时,404 not found 页面正在打开。 另外链接是http://localhost:8080。但是如果输入http://localhost:8080/web/WEB-INF/views/main-menu.jsp,我的索引(主菜单)页面正在打开。 我想在项目开始时打开主页。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
     version="4.0">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

applicationContext.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/cache"
   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.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>

</bean>

HomeController.java

package com.demo.springdemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

    @RequestMapping("/")
    public String showPage(){
        return "main-menu";
    }
}

在 Run/Debug Configurations.Server.Open Browser 中打开浏览器设置也是 http://localhost:8080/。此外,应用程序上下文在 Run/Debug Configurations.Deployment 中为“/”

【问题讨论】:

    标签: java spring spring-mvc spring-boot model-view-controller


    【解决方案1】:

    添加您的应用程序上下文

       <mvc:annotation-driven />
        <context:component-scan base-package="com.something"/>
    

    在 web.xml 中添加这些行

     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          /WEB-INF/applicationContext.xml
        </param-value>
      </context-param>
    

    https://github.com/Purushottam10/Demospring/tree/master/src/main/webapp/WEB-INF

    【讨论】:

      【解决方案2】:

      您的调度程序-servlet 代码在哪里。 如果没有给出,请将此行放入您的 dispatcher-servlet 中

      <context:component-scan base-package="com.something"/>
      

      用你的控制器基础包替换“com.something”。

      并在你的 applicationContext.xml 中添加一行

      <mvc:annotation-driven />
      

      【讨论】:

        【解决方案3】:

        在 applicationContext.xml 文件中添加一行:

        <mvc:annotation-driven />
        <context:component-scan base-package="com.demo.springdemo" /> 
        

        这是告诉扫描和使用 @Controller ,@RestController 等注释。

        【讨论】:

          猜你喜欢
          • 2018-08-08
          • 1970-01-01
          • 2020-12-30
          • 2015-01-03
          • 1970-01-01
          • 2017-12-04
          • 1970-01-01
          • 2019-09-03
          • 1970-01-01
          相关资源
          最近更新 更多