0

[enter image description here][1][enter image description here][1]I am trying to create Maven + Spring web demo project. For this I created an index.jsp from where I am trying to hit controller with value as my name. But After running application even if I am getting index page, if I hit the hyper link to hit my controller, I am immediately getting The requested resource is not available. I am novice in maven and spring. Please help on this.

My POM.xml is,

<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.randem.maven</groupId>
<artifactId>test</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>test Maven Webapp</name>
<url>http://maven.apache.org</url>

<dependencies>
<!-- JUnit dependencies -->  
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

<!-- Spring dependencies -->

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>

<dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aspects</artifactId> 
     <version>3.2.8.RELEASE</version> 
</dependency>

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.10.52</version>
</dependency>


<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

<dependency>
        <groupId>org.jboss.as</groupId>
        <artifactId>jboss-as-arquillian-container-managed</artifactId>
        <version>7.1.1.Final</version>
        <scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>test</finalName>
<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
        </plugin>
    </plugins>  
</build>
</project>

My applicationContext is,

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

</beans>

My index.jsp is,

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC - HelloWorld Index Page</title>
</head>
<body>

<center>
    <h2>Hello World</h2>
    <h3>
        <a href="/hello?name=Gourab">Click Here</a>
    </h3>
</center>
</body>
</html>

My helloworld.jsp is,

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC -HelloWorld</title>
</head>
<body>
<center>
    <h2>Hello World</h2>
    <h2>
        ${message} ${name}
    </h2>
</center>
</body>
</html>

My dispatcher-servlet is,

<?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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context-3.2.xsd">

   <context:component-scan base-package ="web.controller.*" use-default-filters="false"/>
   <context:annotation-config />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

My Controller class is,

package web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {
String message = "Welcome to Spring MVC!";

@RequestMapping(value="/hello", method = RequestMethod.GET)
public ModelAndView showMessage(
        @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
    System.out.println("in controller");

    ModelAndView mv = new ModelAndView("helloworld");
    mv.addObject("message", message);
    mv.addObject("name", name);
    return mv;
  }
}

I am not able to understand the specific reason for not hitting the controller. Kindly help on this. I am using JDK 1.7, Spring 3.2.8.Release and dynamic web module 2.5

My web.xml is,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="WebApp_ID" version="2.5">
  <display-name>Archetype Created Web Application</display-name>

  <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>/WEB-INF/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>
</web-app>

and my Project Folder structure is, [TestMaven project folder structure][1]: https://i.stack.imgur.com/QV0pC.jpg

Gourab Banerjee
  • 59
  • 1
  • 1
  • 9

1 Answers1

1

Try to update your dispatcher-servlet configuration to something like this:

<context:component-scan base-package="web.controller"/>

The use-default-filters documentation in the xsd says:

Indicates whether automatic detection of classes annotated with @Component, @Repository, @Service, or @Controller should be enabled. Default is "true".

W-S
  • 516
  • 5
  • 14
  • I edited it thinking the code will work but it wasn't. Previously use-default-filters was not even mentioned. And also was there. It didn't work then. Its not working now – Gourab Banerjee Feb 22 '16 at 19:47
  • Can you post the directory structure of your project, so we know if the file is in the correct place. And can you post the content of your `web.xml` file. – W-S Feb 22 '16 at 22:21
  • I have updated my web.xml and attached image of project Folder. In views folder helloworld.jsp resides - @W-S – Gourab Banerjee Feb 23 '16 at 04:20
  • @W-S has provided the correct solution. Your annotation configuration is incorrect. I have copied your code on [Github](https://github.com/manish-in-java/stackoverflow-questions/tree/master/35552122) with the change suggested by @W-S. Check it out and run it as `mvn clean tomcat7:run`. Then launch a web browser at [localhost:8080](http://localhost:8080). Your controller will get executed when you click the link on the presented page as you expect. – manish Feb 23 '16 at 04:35
  • @GourabBanerjee your `web.xml`, the project structure and the location of files look right to me. And as confirmed by @manish, by just removing the `use-default-filters` your code should work as expected. – W-S Feb 23 '16 at 05:07