0
votes

I have a Spring HelloWorld project developed by me and for the library management I used Maven in my project. My intention is to see how Spring handles url requests.

I think I developed it correctly but, the outcome is not achieved.

I have entered my Controller class, web.xml file and the mvc-dispatcher-servlet.xml files here.

Controller class

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


@Controller
@RequestMapping("/welcome")
public class AccessController {

    //@RequestMapping(value = {"/welcome"}, method = RequestMethod.GET)
    @RequestMapping(method=RequestMethod.GET)
    public String printWelcome(ModelMap model){
        System.out.println("There in an access atempt");

        model.addAttribute("message","Hello I am spring web MVC !!!");

        return "hello";

    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringLMS</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


</web-app>

mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

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

</beans>

According to the way I have coded, when I enter the URL http://localhost:8080/SpringLMS/welcome the jsp page should be shown.

But the output I get is HTTP Status 404, the requested resource is not available

It does not come in to the Controller class I think.(because it does not print the sentence on the console which I have put there in the controller class.)

I cannot figure out the mistake I have done here. If you could find out where I have done it wrong, Please point it out.

This is my folder structure.

enter image description here

EDIT

The server output

Oct 03, 2015 1:52:56 PM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SpringLMS' did not find a matching property. Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Server version: Apache Tomcat/7.0.64 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Server built: Aug 19 2015 17:18:06 UTC Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Server number: 7.0.64.0 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Name:
Windows 8.1 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Version: 6.3 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Architecture: amd64 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Java Home: C:\Program Files\Java\jre7 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Version: 1.7.0_79-b15 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Vendor: Oracle Corporation Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_BASE: C:\Users\Viraj Gamage\Documents\EclipseMars.metadata.plugins\org.eclipse.wst.server.core\tmp1 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_HOME: C:\Users\Viraj Gamage\Documents\EclipseMars\Tomcat7\apache-tomcat-7.0.64 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.base=C:\Users\Viraj Gamage\Documents\EclipseMars.metadata.plugins\org.eclipse.wst.server.core\tmp1 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.home=C:\Users\Viraj Gamage\Documents\EclipseMars\Tomcat7\apache-tomcat-7.0.64 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dwtp.deploy=C:\Users\Viraj Gamage\Documents\EclipseMars.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.endorsed.dirs=C:\Users\Viraj Gamage\Documents\EclipseMars\Tomcat7\apache-tomcat-7.0.64\endorsed Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dfile.encoding=Cp1252 Oct 03, 2015 1:52:56 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.7.0_79\bin;C:\Program Files\MATLAB\R2013a\runtime\win64;C:\Program Files\MATLAB\R2013a\bin;C:\Apache\apache-maven-3.3.3\bin;C:\Program Files\Java\jdk1.7.0_79\bin;C:\Program Files (x86)\sox-14-4-2;;. Oct 03, 2015 1:52:56 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-8080"] Oct 03, 2015 1:52:56 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["ajp-bio-8009"] Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 775 ms Oct 03, 2015 1:52:56 PM org.apache.catalina.core.StandardService startInternal INFO: Starting service Catalina Oct 03, 2015 1:52:56 PM org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet Engine: Apache Tomcat/7.0.64 Oct 03, 2015 1:52:57 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [102] milliseconds. Oct 03, 2015 1:52:58 PM org.apache.catalina.startup.TldConfig execute INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. Oct 03, 2015 1:52:58 PM org.apache.catalina.core.ApplicationContext log INFO: No Spring WebApplicationInitializer types detected on classpath Oct 03, 2015 1:52:58 PM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-bio-8080"] Oct 03, 2015 1:52:58 PM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["ajp-bio-8009"] Oct 03, 2015 1:52:58 PM org.apache.catalina.startup.Catalina start INFO: Server startup in 1829 ms

1
Could you provide the log output that shows what happens when your web server is started up?hotzst
Can you try to remove the mapping on the controller and add it to the printWelcome method and remove the project name from the url? http://localhost:8080/welcomeChiefTwoPencils
@ChiefTwoPencils If you mean the thing which has been commented in the code posted here, I tried that. Still the same resultvigamage
Even with the url I have in the comment? You shouldn't need to put the project directory there.ChiefTwoPencils
I'd suggest to move request mapping from controller and to the method like ChiefTwoPencils suggests, but you need to have the war name in the url. When you deploy your app to Tomcat, it should say which context it was deployed in (in the server output). I don't see it here, could you try redeploying and observing what is the context path. Also, what is the output you see when you try to use the url you've given?eis

1 Answers

2
votes

1) You forgot to declare your Spring Context loader, please add in your web.xml :

    <listener>
        <display-name>Spring Context Loader</display-name>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

2) To achieve add the mvc:annotation-driven element in your DispatcherServlet context, it declares explicit support for annotation-driven MVC controllers (i.e. @RequestMapping, @Controller, ...)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />

    [...]

</beans>