Witam.
Próbuję wystartować z pierwszym projektem springa MVC z wykorzystaniem adnotacji już jakiś czas i wszystko to jak krew w piach...
Czytam sobie książkę "Spring w akcji", ale mało tu jest o wstępnej konfiguracji projektu i za bardzo nie wiem jak to zrobić. Próbowałem stworzyć zwykły projekt mavena, dodałem to co było w książce + zależności mavenowe, ale nie mogę wystartować projektu z tomcata, w ogóle nie pokazuje mi się zapisana konfiguracja. Z tomcatem wszystko ok, ponieważ do testów wystartowałem inny projekt oparty na xml'owej konfiguracji.
W książce mam do dodania kilka klas
package spittr.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class } ;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
package spittr.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages = { "spittr" }, excludeFilters = {
@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class RootConfig {
}
package spittr.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.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver= new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
package spittr.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@RequestMapping(value="/", method=RequestMethod.GET)
public String home() {
return "home";
}
}
i jeden widok:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false"%>
<html>
<head>
<title>Spittr</title>
<link rel="stylesheet"
type="text/css"
href="<c:url value='/resources/style.css' />" >
</head>
<body>
<h1>Witamy w serwisie</h1>
<a href="<c:url value='/spittles' />">Spittle</a>
<a href="<c:url value='/spitter/register' />">Rejestracja</a>
</body>
</html>
Dodaje także załącznik z moim obecnym projektem w mavenie.
Nie wiem dlaczego tomcat nie chce tego połknąć, proszę o pomoc.
Pozdrawiam.
- spitter.rar (8 KB) - ściągnięć: 89
Shalom