Spring MVC。 Tomcat的。 HTTP状态404 - 未找到

时间:2017-12-06 15:05:36

标签: java maven spring-mvc tomcat

我需要在没有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>

我检查了服务器通过电源shell工作(它的工作原理)。 当我通过应用程序启动服务器时,我得到了#34; HTTP状态404 - 未找到&#34; 我尝试了其他网址。 战争文件被组装。 我该如何解决这个问题?

enter image description here 提前谢谢

3 个答案:

答案 0 :(得分:0)

您应在value中指定@RequestMapping

答案 1 :(得分:0)

改变这个:

  

viewResolver.setPrefix(&#34; / WEB-INF /视图/ JSP&#34);

To :(在jsp之后添加正斜杠)

  

viewResolver.setPrefix(&#34; / WEB-INF /视图/ JSP /&#34);

答案 2 :(得分:0)

我认为您需要为索引方法的@RequestMapping添加值,并确保您的jsp页面位于此目录 WEB-INF / view / jsp 中,然后您就可以获得页面使用 http://localhost:8080/managers/index

@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";
    }
    }