繁体   English   中英

如何在 IntelliJ 中配置 TomCat 及其工件以显示与自动生成“index.jsp”不同的 jsp(通过 SpringMVC)?

[英]How do I configure TomCat and its Artifacts in IntelliJ to display a different jsp (through SpringMVC) than the autogen "index.jsp"?

我一直在 Udemy 上这个 Spring 课程,讲师使用 Eclipse JEE,因为它是免费的。 然而,我最近获得了 IntelliJ IDEA Ultimate Edition,自然我更愿意使用它。 我开始了一个新的 Spring MVC 项目,我发现配置 Tomcat 有一个额外的步骤,即添加一个 Eclipse JEE 似乎并不关心的工件(我觉得这对于找到解决方案可能很重要)。 我不知道那是什么,所以我只使用了 Web Application Exploded 工件。 使用的 URL 是http://localhost:8080/SpringMVCDemo_war_exploded/ 当我运行 Tomcat 服务器时,这个 URL 会自动显示 index.jsp。 这是我的问题。 我希望它显示不同的 jsp 文件。 即在web/WEB-INF/view/找到的 main-menu.jsp。 让我再解释一下。

我有一个用@Controller注释的HomeController类和一个将“main-menu”作为String返回的方法,它通过dispatcher-servlet.xml ,Spring 将预先添加WEB-INF/view/并附加.jsp 理论上这应该显示在WEB-INF/view/找到的 main-menu.jsp 页面。 但是,默认情况下始终显示index.jsp ,我什至无法获得导航到main-menu.jsp的链接才能工作。 我觉得我的HomeController中用于 Exploded Artifact 的 URL 和@RequestMapping("/")注释的方法有一些不一致,该方法返回“主菜单”。

该程序在 Eclipse JEE 中运行良好。 我的预感是我需要使用 The Tomcat Server 和 Web Application Exploded artifact 进行更多配置,但我对 Artifacts 一无所知,IntelliJ 文档解释了 Artifacts,就像您已经知道 Artifact 是什么一样。 有没有人在 IntelliJ IDEA 专业版中使用过 Spring MVC? 如何正确配置服务器以响应我的@RequestMapping 如果可能的话,我想保留@RequestMapping以便更容易在课程中学习。 以下是项目结构和所有有问题的文件

项目结构图

<?xml version="1.0" encoding="UTF-8"?>
   <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web- 
   app_3_1.xsd"
         id="WebApp_ID" version="3.1">

    <display-name>DisplayName</display-name>

    <absolute-ordering/>

    <!-- Spring MVC Configs -->

    <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

调度程序-servlet.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/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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="com.luv2code.springdemo" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

主控制器.java

package com.luv2code.springdemo.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

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

从项目中删除index.jsp文件。 它优先于请求映射。

没有任何会被视为默认页面的文件( index.htmlindex.jspdefault.html等)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM