0

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

eis
  • 51,991
  • 13
  • 150
  • 199
vigamage
  • 1,975
  • 7
  • 48
  • 74
  • 1
    Could you provide the log output that shows what happens when your web server is started up? – hotzst Oct 03 '15 at 08:39
  • 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/welcome` – ChiefTwoPencils Oct 03 '15 at 08:57
  • @ChiefTwoPencils If you mean the thing which has been commented in the code posted here, I tried that. Still the same result – vigamage Oct 03 '15 at 09:00
  • Even with the url I have in the comment? You shouldn't need to put the project directory there. – ChiefTwoPencils Oct 03 '15 at 09:01
  • 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 Oct 03 '15 at 09:01
  • @ChiefTwoPencils I think that's a war name, which needs to be there. – eis Oct 03 '15 at 09:02
  • @ChiefTwoPencils yes it is the same result – vigamage Oct 03 '15 at 09:02
  • @eis, I'm going off the img. Perhaps I'm missing something. I don't have the war in the app I've got up now but that's probably a difference of configs I guess. – ChiefTwoPencils Oct 03 '15 at 09:04
  • @eis I think I found a reason here. In my server directory, my project has not been deployed into that. There is a deployment issue I think – vigamage Oct 03 '15 at 09:07
  • so you never even deployed it? – eis Oct 03 '15 at 09:08
  • How I made the project run was, right click-> run on server. Isn't that all I have to do? – vigamage Oct 03 '15 at 09:10
  • yes, if everything is configured correctly in eclipse. Did you see any change in server output when you deployed to server? – eis Oct 03 '15 at 09:11
  • Are you running the App from STS/IDE? if yes then In server directory, project may not be deployed into that. – Arshad Ali Oct 03 '15 at 09:12
  • @ArshadAli in Eclipse Juno. There is no project in server directory. Is that how it is? – vigamage Oct 03 '15 at 09:13
  • there should be a war file in webapps folder of tomcat. But I asked about change in server output logs during deployment. Please check that. – eis Oct 03 '15 at 09:14
  • @eis there is no war file in webapps folder. The server output log is attached to the question. – vigamage Oct 03 '15 at 09:17
  • yes, but I asked was there a *change* in server output logs during deployment. Please try redoing the deployment and check if there are any changes during it. – eis Oct 03 '15 at 09:18
  • @eis No any change in the server tab. It sows nothing. And the console output is shown in my question. I don't know whether I understood u correctly. – vigamage Oct 03 '15 at 09:32
  • if there is no change, then deployment is not happening and you haven't configured eclipse deployment correctly. – eis Oct 03 '15 at 09:36
  • Could you please tell me how should I do that. I have followed the normal usual procedure to do that. To make sure I removed the project from the server and added it again. Still it is same – vigamage Oct 03 '15 at 09:43

1 Answers1

2

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>
biology.info
  • 3,500
  • 2
  • 28
  • 39
  • Sorry but I have this does not work in the case asked above?? – Arshad Ali Oct 03 '15 at 09:20
  • I added that. but it gives me this `The prefix "mvc" for element "mvc:annotation-driven" is not bound.` – vigamage Oct 03 '15 at 09:21
  • Still the same result too – vigamage Oct 03 '15 at 09:21
  • @vigamage you need to also add schemalocation, xml namespaces like in this answer. They define the mvc namespace. (but keep the context namespace there as well, like you have) – eis Oct 03 '15 at 09:22
  • I changed them too. no luck – vigamage Oct 03 '15 at 09:25
  • did you do the redeployment and watched the logs like I asked you to? – eis Oct 03 '15 at 09:26
  • @vigamage > i didn't watch your web.xml, i edited my answer – biology.info Oct 03 '15 at 09:41
  • when I have added `org.springframework.web.context.ContextLoaderListener` do I have to add that too? BTW it did not work :( – vigamage Oct 03 '15 at 09:48
  • I'm pretty sure that your log output show something now; don't forget to clear your tomcat work directory and clean by clicking right on your tomcat server into Servers tab in eclipse – biology.info Oct 03 '15 at 09:53
  • I cannot understand what the log output is. But in the console tab, there is nothing new than the thing I have posted in the question – vigamage Oct 03 '15 at 09:58
  • How do you have add / configure your tomcat, it look strange – biology.info Oct 03 '15 at 10:14
  • What I did was just added Tomcat into my project. As the accepted answer described in in here http://stackoverflow.com/questions/8046871/how-to-add-tomcat-server-in-eclipse – vigamage Oct 03 '15 at 10:25
  • I believe your tomcat deploy path changed and Eclipse is not deploying your application. Can you paste your server config by double clicking on your tomcat server (Servers tab) or just check your deploy path / server path. Thx – biology.info Oct 04 '15 at 08:16