Pierwsza aplikacja w Spring

0

Robię pierwszą aplikację z użyciem Spring Framework wg. książki "Spring MVC przewodnik dla początkujących".
Wszelkie konfiguracje zrobiłem wg. tutoriala, jednak miałem problem z konfiguracją 'artifacts' do deployu na Tomcacie.
Kiedy w Artifacts wybieram Web Application : Excluded to nie mogę rozwinąć listy i wybrać From modules.. a czytałem, że tak być powinno.
Udało mi się skonfigurować tak jak na poniższym obrazku :

Artifacts

Serwer startuje, jednak mam błąd 404, że nie rozpoznano nic pod "/" .

Poniżej moje najważniejsze pliki konfiguracji.
Czy coś robię źle?

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.packt</groupId>
    <artifactId>webstore</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

HomeController.java

package com.packt.webstore.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController
{
    @RequestMapping("/")
    public String welcome(Model model)
    {
        model.addAttribute("greeting","Witaj w sklepie internetowym");
        model.addAttribute("tagline", "BL");

        return "welcome";
    }
}

welcome.jsp

<@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <link rel="stylesheet"
              href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
        <title>Witaj</title>
    </head>
    <body>
        <section>
            <div class="jumbotron">
                <div class="container">
                    <h1>${greeting}</h1>
                    <p>${tagline}</p>
                </div>
            </div>
        </section>
    </body>
</html>

DefaultServlet-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: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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <mvc:annotation-driven />
    <context:component-scan base-package="com.packt.webstore" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

web.xml

<web-app version="3.0" 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">
    <servlet>
        <servlet-name>
            DefaultServlet
        </servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>
            DefaultServlet
        </servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
0

A masz @requestmapping pod ten adres? A on sam powinien być w kontrolerze

0

@Riiuku: mam w pliku powyżej zamiesczonym HomeController.java @RequestMapping

0

Spróbowałem zrobić teraz również Javową konfigurację zamiast XML i występuje podobny błąd - tak jakby nie mogło znaleźć pliku welcome.jsp
Czy struktura plików w projekcie jest odpowiednia?
title

0

Ścieżka powinna wyglądać tak: webapp -> WEB-INF -> views. Możesz sprawdzić sobie w google jak powinna wyglądać struktura projektu https://www.google.pl/search?tbm=isch&sa=1&ei=AGNAWqz2NcfUwQLVo5fIDg&q=SPRING+STRUCTURE+PROJECT&oq=SPRING+STRUCTURE+PROJECT&gs_l=psy-ab.12...0.0.0.72387.0.0.0.0.0.0.0.0..0.0....0...1c..64.psy-ab..0.0.0....0.HT4w23zNmpA. Na końcu skonfiguruj poprawną ścieżkę do widoków w bean'ie viewResolver.

0

Strukturę folderów przerobiłem, na obecną :
title

Plik konfiguracyjny viewResolvera wygląda tak:

package com.lorenc.startproject.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
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;

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = {"com.lorenc.startproject.web"})
public class MvcConfig extends WebMvcConfigurerAdapter
{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver jspViewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();
        bean.setPrefix("/WEB-INF/views/jsp/");
        bean.setSuffix(".jsp");
        return bean;
    }

    @Bean(name = "messageSource")
    public ReloadableResourceBundleMessageSource getMessageSource() {
        ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
        resource.setBasename("classpath:messages");
        resource.setDefaultEncoding("UTF-8");
        return resource;
    }
}

Cały czas jednak status :
HTTP Status 404 - /WEB-INF/views/jsp/welcome.jsp

Co jest przyczyną?

0

a spróbuj wrzucić webapp do main

0
<?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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <mvc:annotation-driven />
    <context:component-scan base-package="com.packt.webstore" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

tu masz com.packt.webstore a masz inną strukturę ;p

No i u mnie web.xml znajduję się w webapp/WEB-INF folderze

0

@herbatek: wrzuciłem - bez zmian.
@pedegie ten poprzedni jest poprawny. Screen ze struktury katalogów rzuciłem z innego projektu, gdzie wykonałem konfigurację w Java nie XML. Występuje taki sam błąd w obu projektach.

0

Problem rozwiązany - przerzucenie folderu do webapp i podmiana katalogów pomogła w jednym i drugim projekcie :)
Mam jeszcze pytanie odnośnie frontu. Mam gotowy template bootstrapa, gdzie wrzucić te wszystkie pliki. np. do ścieżki src/main/webapp/bootstrap?

0

Z reguły pliki np. bootstrapa znajdują się w folderze resources -> src/main/webapp/resources/css/bootstrap.css

1 użytkowników online, w tym zalogowanych: 0, gości: 1