Cześć, zacząłem naukę Springa od książki "Spring w akcji. Z innych źródeł ogarnąłem konfigurację eclipsa z tomcatem i mavenem i teraz mam tak, że jak chcę zrobić maven + dynamic web project to tworzę mavenowski projekt i dodaje archetyp webapp. Dobry sposób ? Tak na stacku gdzieś napisali i zrobiłem. Strukturę projektu mam taką:
https://zapodaj.net/6f5b3ad97b165.png.html
Zdziwiłem się, że maven nie wytworzył standardowych czterech folderów main( java i resources ) i test (java , resources ). Było tylko src/main/resources a src/main/java dodałem ręcznie i dopiero wtedy zadziałało tworzenie nowych klas.
I teraz tak. Jak tworzę nowy JSP klikając prawym na src/java/resources i go odpalam to działa. Pojawia się on w WEB-INF. Ale jak go stworzę z poziomu WEB-INF to nie działa. Nie za bardzo rozumiem dlaczego.
I teraz zgodnie z książką mam taką javową konfigurację:
SpittrWebAppInitializer.java
package spittr.cfg;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override
protected String[] getServletMappings()
{
return new String[] { "/" };
}
@Override
protected Class<?>[] getRootConfigClasses()
{
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses()
{
return new Class<?>[] { WebConfig.class };
}
}
WebConfig.java
package spittr.cfg;
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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
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();
}
}
RootConfig.java
package spittr.cfg;
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
{
}
HomeController.java
package spittr.web;
import static org.springframework.web.bind.annotation.RequestMethod.*;
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 = GET )
public String home()
{
return "home";
}
}
I wgl książki na ścieżce
Powinienem dostać home.jsp
Umieszczam więc to home.jsp w folderze views, który tworzę w WEB-INF ale nie wczytuje go w ogóle.
Jakieś sugestie ?