gpt4 book ai didi

java - 无法从 IntelliJ Tomcat 中找到 Controller

转载 作者:行者123 更新时间:2023-11-28 23:15:14 24 4
gpt4 key购买 nike

我在 intelliJ 中配置 spring mvc 项目时遇到问题(我无法在此项目中使用 spring boot)。

我似乎无法击中我的 Controller :

package app.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/user")
public class UserController {

@RequestMapping(value = "/registerUser", method = RequestMethod.GET)
public String registerUser(Model m, HttpServletRequest request) {
System.out.println("HERE");
return "index";
}
}

因为我在尝试 localhost:8080/user/registerUser 时总是会收到 404 错误。

我意识到为了拥有 HttpServletRequest 类,我需要在我的 pom.xml 中导入最后一个依赖项:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.nemanjagajic</groupId>
<artifactId>Reserve</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.1.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>

或者我可以转到"file"->“项目结构”->“模块”,选择“依赖项”选项卡,然后添加库 Tomcat 9.0.6,但无论如何我都无法访问我的 Controller 。同一个项目似乎在 Eclipse 中工作,所以我猜它与在这种情况下配置服务器 aka tomcat 有关,但我很难找到在 IntelliJ 中正确配置它的方法。

如果有人想尝试,我会留下项目的 github 存储库: https://github.com/nemanjagajic/Reserve

最佳答案

  1. 请重命名 web->webapp 并移至 src->main 文件夹。
  2. 如下更新 pom.xml。

    http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

    <groupId>com.nemanjagajic</groupId>
    <artifactId>Reserve</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.6.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>4.3.6.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.4.1.Final</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.11.1.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat -->
    <dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat</artifactId>
    <version>8.5.15</version>
    <type>pom</type>
    </dependency>

    </dependencies>
    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
    <port>8080</port>
    <path>/</path>
    </configuration>
    </plugin>
    </plugins>
    </build>

  3. 如下更新 webConfig.java。

       @Bean
    public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setViewClass(JstlView.class);
    resolver.setPrefix("/");
    resolver.setSuffix(".jsp");
    return resolver;
    }

这是工作示例。

enter image description here

enter image description here

关于java - 无法从 IntelliJ Tomcat 中找到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50804436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com