簡體   English   中英

Spring MVC。 雄貓 HTTP狀態404 –找不到

[英]Spring MVC. Tomcat. HTTP Status 404 – Not Found

我需要配置不帶web.xml的Spring Mvc。 僅使用Java配置

我的WebConfig

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "controllers.web")
@Import({PersistenceJPAConfig.class})
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

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

我的WebInitializer

public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(WebConfig.class);
    servletContext.addListener(new ContextLoaderListener(ctx));
    ctx.setServletContext(servletContext);
    Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    servlet.setLoadOnStartup(1);
    servlet.addMapping("/");
}
}

我的控制器

@Controller
@RequestMapping("/")

public class MainController {

@Autowired
BusRepository busRepository;

@RequestMapping(method = RequestMethod.GET)
public String index(ModelMap model) {
    StringBuilder sb = new StringBuilder("<br>");
    busRepository.findAll().forEach(it->sb.append(it.toString()).append("<br>"));
    model.put("msg", sb.toString());
    return "index";
}
}

我的index.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Spring MVC</title>
</head>
<body>
    <h4>Spring MVC</h4>
    <span class="blue">${msg}</span>
</body>
</html>

我的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.intexsoft.training.manager</groupId>
    <artifactId>managers</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>managers</name>
    <url>http://localhost:8080/managers</url>
    <build>
        <plugins>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>view.Runner</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>hibernate.cfg.xml</directory>
            </resource>
        </resources>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.11.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</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.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.11.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.11.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>
</project>

我通過電源外殼檢查了服務器的工作情況(正常工作)。 當我通過應用程序啟動服務器時,出現“ HTTP Status 404 – Not Found”,我嘗試了其他URL。 戰爭文件已組裝。 我怎么解決這個問題?

在此處輸入圖片說明 提前致謝

您應該在@RequestMapping指定value

更改此:

viewResolver.setPrefix(“ / WEB-INF / view / jsp”);

收件人:(在jsp之后添加了正斜杠)

viewResolver.setPrefix(“ / WEB-INF / view / jsp /”);

我認為您需要為@IndexMapping的索引方法添加值,並確保您的jsp頁面位於此目錄WEB-INF / view / jsp中 ,然后可以使用http:// localhost:8080 /來獲取頁面。 經理/索引

@Controller
@RequestMapping("/")

public class MainController {

@Autowired
BusRepository busRepository;

    @RequestMapping(value="index",method = RequestMethod.GET)
    public String index(ModelMap model) {
        StringBuilder sb = new StringBuilder("<br>");
        busRepository.findAll().forEach(it->sb.append(it.toString()).append("<br>"));
        model.put("msg", sb.toString());
        return "index";
    }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM