【问题标题】:New project using Spring MVC 5 in eclipse, but I am having error 404在 Eclipse 中使用 Spring MVC 5 的新项目,但出现错误 404
【发布时间】:2019-08-19 03:41:45
【问题描述】:

我是 Spring MVC 的新手,我正在尝试制作简单的 HelloWorld 程序,我正在关注本教程 https://www.javaguides.net/2018/10/spring-mvc-5-hello-world-example.html

应用配置

package main.java.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
"main.java"
})
public class AppConfig {

@Bean
public InternalResourceViewResolver resolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setViewClass(JstlView.class);
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}
}

Dispatcher Servlet 初始化器

package main.java.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ServletDispatcherInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class <?> [] getRootConfigClasses() {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected Class <?> [] getServletConfigClasses() {
    return new Class[] {
        AppConfig.class
    };
}

@Override
protected String[] getServletMappings() {
    return new String[] {
        "/"
    };
}
}

控制器

package main.java.controller;

import java.time.LocalDateTime;

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

import main.java.model.HelloWorld;

@Controller
public class JavaController {

@RequestMapping("/helloworld")
public String handler(Model model) {

    HelloWorld helloWorld = new HelloWorld();
    helloWorld.setMessage("Hello World Example Using Spring MVC 5!!!");
    helloWorld.setDateTime(LocalDateTime.now().toString());
    model.addAttribute("helloWorld", helloWorld);
    return "helloworld";
}
}

型号

package main.java.model;

public class HelloWorld {
private String message;
private String dateTime;
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}
public String getDateTime() {
    return dateTime;
}
public void setDateTime(String dateTime) {
    this.dateTime = dateTime;
}
}

JSP

...
      ${helloWorld.message}</h2>
      Server date time is : ${helloWorld.dateTime}
...

文件路径

错误

我尝试清理服务器,关闭并打开浏览器,重新发布我的项目。

我现在不知道该怎么办,请帮帮我。

我的 pom.xml

<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>SpringMVC5</groupId>
  <artifactId>SpringMVC5</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>

 <name>springmvc5-helloworld-exmaple Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
   <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>
    <dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.0.RELEASE</version>
    </dependency>

    <!-- JSTL Dependency -->
    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>javax.servlet.jsp.jstl-api</artifactId>
        <version>1.2.1</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>

    <!-- Servlet Dependency -->
    <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <version>3.1.0</version>
         <scope>provided</scope>
    </dependency>

    <!-- JSP Dependency -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>
 </dependencies>

  <build>
<sourceDirectory>src</sourceDirectory>
<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
      <release>11</release>
    </configuration>
  </plugin>
</plugins>

【问题讨论】:

  • 你怎么称呼你的网址?假设 SpringMVC5 实际部署到 webapps 下面,那么 http:&lt;webserver:port&gt;/SpringMVC5/helloworld 应该是正确的
  • @ScaryWombat 仍然是 404。
  • 你是否使用了 pom.xml 作为链接?你用什么名字?
  • @ScaryWombat 我只保留最后一部分 maven-compiler-plugin3.8.0
  • 上周我遇到了类似的麻烦,这是由于 pom.尝试使用他们的示例。

标签: java eclipse spring-mvc


【解决方案1】:

其中一个可能的问题是您的包命名错误。 maven 项目的文件夹结构如下:

src
  |_ main
  |     |_ java
  |     |     |_ your.actual.package
  |     |_ resources
  |     |_ webapp
  |_ test
        |_ java
              |_ your.actual.package

所以你的实际包在java 文件夹之后开始。这意味着您实际上拥有:

  • config 包而不是 main.java.config 用于 AppConfig

  • controller 包而不是main.java.controller 用于JavaController

  • model 包而不是 main.java.model 用于 HelloWorld

您还需要更改您的@ComponentScan。将其留空以扫描根包。或者尝试使用basePackageClasses,如果这不起作用。

之后您需要检查war 文件。解压并检查WEB-INF 文件夹是否包含所有已创建类的.class 文件,views 是否包含您的helloworld.jsp 文件。

我希望这会有所帮助。

【讨论】:

  • src/main/java 然后我添加包 com.test.config,将扫描留空,没有任何变化。您认为我的设置有问题吗?
  • 您可以尝试将@ComponentScan 更改为指向您的com.test 包。但通常很难说还有什么问题。你看过war文件吗?一切都在那里(类,jsps)吗?战争中的文件夹结构好吗? tomcat 日志中有什么?你的InternalResourceViewResolver 应该有一些东西可以帮助你找到问题。
  • war文件很好,一切都在那里,只有jsp文件出错,但结构都很好。在 tomcat 日志中只有这一行看起来很糟糕 信息:在 java.library.path 上找不到基于 APR 的 Apache Tomcat Native 库,它允许在生产环境中获得最佳性能:[C:\Program ....
【解决方案2】:

1-

您应该在 @ComponentScan 注释中列出所有包,包括配置类,而不是 main.java : @ComponentScan(basePackages = {"config","controller"})

或者

将所有包移动到父包中(例如:com.project),例如 com.project.config、com.project.controller、com.project.model 并使用它:

@ComponentScan(basePackages = {"com.project"})

2- 检查项目的 web 依赖程序集,应该是这样的:

如果您的配置不是这样,您应该通过单击添加按钮并选择 java Build Path Entries 来添加它。

然后在下一个窗口中选择 Maven Dependencies。

【讨论】:

  • 谢谢。这对我有用。所以我的问题是 jar 文件不在正确的位置?对吗?
  • 如果你在eclipse中的服务器上将项目作为动态web项目运行,如果没有配置,你应该配置部署程序集,这将指示WTP将maven依赖项发送到服务器目录。另一方面,如果您使用 maven 构建,maven 会将依赖项 jar 移动到项目的 lib 文件夹中。
【解决方案3】:

不确定问题出在哪里,但这里有一个完整的项目供您参考。 您可以从这里获得帮助:

https://github.com/imrangthub/BlogSolutionUsingSpringHibernateWithClassLevelConfig

【讨论】:

  • 感谢您的帮助,但我想知道我做错了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 2016-10-03
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
  • 2018-06-04
  • 2015-10-25
相关资源
最近更新 更多