Spring mvc не находит страницу - 404. Решено (вроде бы)

213
26 мая 2018, 18:30

уже не знаю что делать. Все перепроверил.

Создал проект. подключил спринг. Добавил спринг в артефакт для деплоя (tomcat 9) (Проверял - либы выгружаются).

Настроил xml, создал контроллер.

НО! спринг ничего не находит. При заходе на главную страницу - 404. что не так? не понимаю.

Структура проекта:

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_4_0.xsd"
         version="4.0">
    <!--<welcome-file-list>
        <welcome-file>index</welcome-file>
    </welcome-file-list>-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath: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>
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:annotation-config />
    <!--<mvc:annotation-driven />-->
</beans>

dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <mvc:annotation-driven />
    <context:component-scan base-package = "com.spring.learning.controller"/>
    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
    <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name = "prefix" value = "/WEB-INF/jsp/" />
        <property name = "suffix" value = ".jsp" />
    </bean>
</beans>

BaseController:

@Controller
public class BaseController {
    @RequestMapping(value = "/")
    public String index() {
        return "index";
    }
    @RequestMapping(value = "/hello")
    public String hello() {
        return "hello";
    }
}
Answer 1

Есть два решения которые мне помогли.

Первое (как мне кажется не самое лучшее) добавить в web.xml такое:

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>/WEB-INF/views/*</url-pattern>
</servlet-mapping>

Второе (не знаю как, может после пересоздания артефакта):

В web.xml вместо:

<servlet-mapping>
   <servlet-name>dispatcher</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>

сделать так:

<servlet-mapping>
   <servlet-name>dispatcher</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>
READ ALSO
Сериализовать java-объекты в json-строку

Сериализовать java-объекты в json-строку

Хочу из контроллера вернуть json-строку List, но объекты в json сериализуются как пустые строкиВ результате получаю [{},{}]

171
Прокрутка активности вниз

Прокрутка активности вниз

На активности в TextView передается довольно большой объем текстаКогда его становится слишком много он уходит "вниз", но активность прокрутить...

188
Проблема со скроллом на смартфонах

Проблема со скроллом на смартфонах

При просмотре со смартфона тормозит прокрутка 3 и 7 экранов, в которых находится форма с десятью инпутамиСсылка на сайт http://продатьвелосипед

178
Прерывистая рамка посередине css

Прерывистая рамка посередине css

Ребята, не знаю как реализовать данную рамку

197