N
N
Nicshek2018-10-28 13:23:05
Java
Nicshek, 2018-10-28 13:23:05

Why doesn't spring mvc work?

I'm trying to learn spring mvc, but got stuck in the wall. For any requests, even the simplest ones, as in the screenshot below, I get a 404 error, the maximum that can be loaded is the Index in the root folder. When changing the prefix and moving the jsp to another folder, nothing happens either. I am trying to open helloworld.jsp
web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <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>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

dispatcher-servlet
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context">

    <context:component-scan base-package="Controllers"/>

    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value=""/>
        <beans:property name="suffix" value=".jsp"/>
    </beans:bean>

</beans:beans>

Controller
package Controllers;

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

@Controller
public class HelloController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String hello(){
        return "helloworld";
    }
}

<?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>ru</groupId>
    <artifactId>lab2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.1.RELEASE</version>
        </dependency>
    </dependencies>

</project>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2018-10-28
@aol-nnov

Is there a helloworld template? If you want to get this string in the response, add the responsebody annotation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question