I can't start my SpringMVC app to Tomcat.



  • I'm studying. Spring MVC♪ Used Tomcat 8IntelliJ IDEA 15Maven 3

    Problem: 404 in an attempt to move localhost:8080/greeting

    Project structure:

    введите сюда описание изображения

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    
    &lt;display-name&gt;Spring-MVC&lt;/display-name&gt;
    &lt;context-param&gt;
        &lt;param-name&gt;contextClass&lt;/param-name&gt;
        &lt;param-value&gt;org.springframework.web.context.support.AnnotationConfigWebApplicationContext&lt;/param-value&gt;
    &lt;/context-param&gt;
    
    &lt;servlet&gt;
        &lt;servlet-name&gt;SpringDispatcher&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;contextClass&lt;/param-name&gt;
            &lt;param-value&gt;org.springframework.web.context.support.AnnotationConfigWebApplicationContext&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
            &lt;param-value&gt;ua.andrewiscom.springtest.config&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;SpringDispatcher&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    
    &lt;session-config&gt;
        &lt;session-timeout&gt;30&lt;/session-timeout&gt;
    &lt;/session-config&gt;
    

    </web-app>

    greeting.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>Welcome</title>
    </head>
    <body>
    <h1>Hello, ${name}!</h1>
    </body>
    </html>

    HelloController.java

    package ua.andrewiscom.springtest.controllers;

    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 org.springframework.web.bind.annotation.RequestParam;

    @Controller
    public class HelloController {

    @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }
    

    }

    MvcConfig.java

    package ua.andrewiscom.springtest.config;

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

    @Configuration
    @ComponentScan(basePackages="ua.andrewiscom")
    @EnableWebMvc
    public class MvcConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/pages/");
    resolver.setSuffix(".jsp");
    return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
    

    }

    Anyway, I really don't like configuration through. XML♪ Found some of the Java Configuration Towers and actually made it the way. MvcConfig♪ Coupled and redecorated. Then I tried. https://spring.io/guides/gs/serving-web-content/ ordinary. Logis say it's good. war- Archive is coming.



  • In the mapping dispatcherServlet after the slash, you have to put: <url-pattern>/*</url-pattern>

    Next:
    if you do. context-param, why would the same parameter indicate init-param

    Well, the comments are:

    Anyway, I really don't like the XML configuration.

    You'll figure out what the pros and cons of different configurations are.

    Found a little bit of a Java Configuration and did it at MvcConfig.

    Spring has nothing better than their own. http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ - it's all concise and understandable.

    Coupled and redecorated.

    Even if you're doing the Tutorial, you need to clear every line of the code, figure out what's going on, and why does it say that or not.

    Speaking of logs, if you'd look in the logs, you might have noticed that spring's giving a warning to an unfit URL.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2