Search code examples
web-applicationsspring-mvctomcat7jboss7.xurl-mapping

No xml configuration weird url mapping web application


This is my first post so go easy on me. I'm trying to set up a java web application using spring web mvc 3.1.3 on JBoss AS 7 and or TomCat 7. But something weird is happening in the URL mapping. I am following a bunch of tutorials but I am basically trying to do part one of this one. There is a very simple jsp in my WEB-INF/views folder which I would like to get rendered and returned when calling http://localhost:8080/hello/welcome. But this doesn't happen. There is something wrong with the url mapping. When I use something like /test/* and I call http://localhost:8080/hello/test/welcome it works as expected. When I use /* http://localhost:8080/hello/welcome returns a non rendered jsp. And when I use what I would in my noob opinion should be the default I get a 404 resource not found. I tested this all on a JBoss 7.1.1 and a Tomcat 7.0.33 server running within eclipse or deployed using a war file. I am at my wits end. Every google result page is full of purple links I still haven't found what I'm looking for.

Anybody who could help? I understand some info might be missing but please ask.

edit: I forgot I use maven to build my application. This is done using the m2e wtp plugin for eclipse. And I am running eclipse juno.

This is my Initializer (very basic).

Public class Initializer implements WebApplicationInitializer 
{   
public void onStartup(ServletContext servletContext) throws ServletException {
 AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
 root.register(WebappConfig.class);

 ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(root));
 servlet.addMapping("/");
 servlet.setLoadOnStartup(1);
}}

This is my WebappConfig:

@Configuration
@ComponentScan("nl.hello")
@EnableWebMvc
public class WebappConfig extends WebMvcConfigurerAdapter
{
@Bean
public InternalResourceViewResolver setupViewResolver() 
{
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");

return resolver;
}

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

And the Controller:

@Controller
public class HelloController {

@RequestMapping("/welcome")
public String helloWorld(Model model) {
//let’s pass some variables to the view script
model.addAttribute("wisdom", "Goodbye XML");

return "welcome"; 
}}

And my pom:

<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>hello</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
  <plugins>
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>
        <source>1.6</source>
        <target>1.6</target>
      </configuration>
    </plugin>
    <plugin>
      <artifactId>maven-war-plugin</artifactId>
      <version>2.3</version>
      <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
      </configuration>
    </plugin>
  </plugins>
</build>
<properties>
    <project.build.sourceEncoding>utf8</project.build.sourceEncoding>
</properties>
<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2.2</version>
</dependency>
 </dependencies>
 </project>

Solution

  • I solved it! I should have mentioned that I knew about the "bug" of tomcat prior of version 7.0.15. Below this version it is not possible to override the default url mapping. But as I said I was aware of it and I used version 7.0.33. So this couldn't be it right!? Wrong! On a side note I should have noticed in the 404 page of jboss that 7.1.1 (brontes) uses version 7.0.13 or something and is never going to work. But what was the problem. Well I was using the m2e wtp plugin for eclipse to build my applications. Unfortunately there is a something in maven that broke everything. First I thought it was the fact it was embedded and using an external maven source did solve my problem. Maybe a version difference? Well turns out they both use 3.0.4. Weird!? When I switched back to embedded it worked.

    Summary: When using webapplicationinitializer use Tomcat version of 7.0.15 and preferably use an external maven source.