Intellij Idea Spring 404 –找不到

时间:2018-12-05 00:13:47

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

在Tomcat上启动项目时,将打开“ 404未找到”页面。 链接也是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浏览器中,打开浏览器的设置为http://localhost:8080/。另外,在运行/调试配置中,应用程序上下文为“ /”。部署

3 个答案:

答案 0 :(得分:2)

添加您的应用程序上下文

   <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

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:1)

您的调度程序servlet代码在哪里。 如果未提供,则将此行放入您的dispatcher-servlet中

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

将“ com.something”替换为您的基本控制器包。

然后在applicationContext.xml中添加一行

<mvc:annotation-driven />