java - Intellij Idea Spring 404 – 未找到

标签 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";
    }
}

同时打开浏览器设置是 http://localhost:8080/在运行/调试配置.服务器.打开浏览器中。另外,运行/调试配置中的应用程序上下文为“/”。部署

最佳答案

添加到您的应用程序上下文中

   <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

关于java - Intellij Idea Spring 404 – 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53623322/

相关文章:

java - 如何使用正则表达式删除元音多于辅音的单词

java - Hibernate注解关系

java - jni native 函数重载签名

java:在另一个测试中访问对象的最佳实践

java - 在两个 SQLSessionFactory bean 之一上使用 @Primary 时出现 "java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist"

java - 为什么要使用 Mockito?

java - Spring MVC Maven 项目中的 org.springframework.beans.factory.BeanCreationException

java - Spring Batch - 如何将字符串从文件转换为日期?

spring - Spring MessageSource 属性文件中的内部引用

java - 如何在 spring mvc 4 上将表单值从 jsp 发布到 Controller 时解决 ClassCastException