I can't start my SpringMVC app to Tomcat.
-
I'm studying. Spring MVC♪ Used Tomcat 8♪ IntelliJ IDEA 15♪ Maven 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">
<display-name>Spring-MVC</display-name> <context-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </context-param> <servlet> <servlet-name>SpringDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>ua.andrewiscom.springtest.config</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringDispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config>
</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 indicateinit-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.