0

I have [web.xml]:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          version="2.5">    


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



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

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

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

Also [dispatcher-servlet.xml]:

<?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">



<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>
            /home=HomeController
        </value>
    </property>
</bean> 

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

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="1"></property>
</bean>

<bean name="HomeController" class="com.spring.mvcc.HomeController"></bean>

</beans>

[HomeController.java]

@Controller
public class HomeController {

    @RequestMapping({"/","/home"})
    public String showHomePage(Map<String, Object> model){
        model.put("spittles","works");

        return "home";     
    }
}

[home.jsp]

<html>
<body>
   <h2>HI!</h2>
</body>
</html>

When I try to run it through my browser like (I changed my Tomcat ports to 9090) : localhost:9090//MyProject/home - result : nothing happends. Just a blank page.

What is wrong with this?

solvator
  • 371
  • 1
  • 6
  • 12
  • Why are you running `home.jsp` directly? Try running with just `localhost:9090/MyProject/home` – Rohit Jain Jan 09 '14 at 09:27
  • home.jsp is in WEB-INF/views/ – solvator Jan 09 '14 at 09:27
  • How can I run it then? I just want to test it. – solvator Jan 09 '14 at 09:28
  • @solvator Yeah, but it won't be accessible from outside the application. – Rohit Jain Jan 09 '14 at 09:28
  • @RohitJain I tried, still a blank page – solvator Jan 09 '14 at 09:29
  • Show us your jsp page. – Rohit Jain Jan 09 '14 at 09:32
  • The logs should help you figure out what handlers are being registered and, at debug level, what's going on for a particular request. – Emerson Farrugia Jan 09 '14 at 14:38
  • When you say , blank page what is the HTTP Status Code 200 ? or 404 – Mani Jan 09 '14 at 14:52
  • @Mani Now it isn't when I changed my dispatcher file. But now it shows nothing, just an empty screen. how to fix it??? – solvator Jan 09 '14 at 16:06
  • i dont understand, are you saying it is returning 200 ?. Ok can you add some log / sop in your ShowHomePage method ? i doubt it is not finding the bean first. You dont see any error in log while loading the app ? any logs confirming the path recognized as "Mapped URL path [" or "Default mapping to " or "Cannot map " ? . may be stupid question - Your package name is correct in bean declaration ? .. note has "mvcc" – Mani Jan 09 '14 at 16:38
  • @Mani yes package name is correct. It's a pity, but I have never done this before - so I do not know how to log my methods. What I am doing now is running my project like this trough cmd: mvn tomcat7:run. And no errors or logs are showed....Also when I build my project through netbeans, there aren't any error. And it shows a message 'build success' – solvator Jan 09 '14 at 16:41
  • Follow the link http://stackoverflow.com/questions/10727396/configuring-logging-for-an-embedded-tomcat-from-maven-plugin to view the logs .. if you see the logs you should able to fix the problem your self. – Mani Jan 09 '14 at 16:52
  • @Mani thanks for helping, I think I finally solve the problem, I do not know what exactly was, but I followed this tutorial and re-write my project http://www.tutorialspoint.com/spring/spring_web_mvc_framework.htm. Now when I use localhost:8080/Test/hello , it shows 'hello word' which is described in views/hello.jsp :) thanks! – solvator Jan 09 '14 at 17:18

2 Answers2

0

Home.jsp is the view,you should visit localhost:9090//MyProject/home

0

I think the problem is with your configuration. First of all, you are mixing XML configuration and annotation configuration, which IMO is usually not a good thing (may lead to confusion from both the reader and Spring). Second, you haven't told Spring to actually look for annotation configuration anywhere.

What I would do is the following,

[dispatcher-servlet]

<?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


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

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="1"></property>
</bean>

<context:component-scan base-package="your.package.here" />
<mvc:annotation-driven />    

Please note that I have added two namespaces (mvc and context), and removed the SimpleUrlHandlerMapping and your HomeController bean.

But the magic happens in the last two lines, they basically "activate" your annotations.

There are ways of doing this without the annotations as well, extending the MultiActionController could be one way, but it looks like you want the annotations (and based on the Spring documentations these days it appears to be the preferred way to go).

Edit: Also, the DefaultAnnotationHandlerMapping is deprecated since 3.2 apparently. But with the added namespaces it seems useless anyway. I would just remove it.

jsundin
  • 226
  • 1
  • 8
  • I appreciate for response, I will try it as soon as I can. – solvator Jan 09 '14 at 14:52
  • Don't forget to enter your namespace in the base-package parameter. I did that when I went for a try. :) – jsundin Jan 09 '14 at 14:55
  • That's very strange. This works for me. What version of Spring are you using? And do you see an error in the console window and/or the server logs? – jsundin Jan 09 '14 at 16:11
  • I am opening my project through : mvn tomcat7:run (through cmd). How can I check an error message? Spring version 3.0.0.RELEASE – solvator Jan 09 '14 at 16:12
  • mvc annotation-driven is not needed in this case. As I understand, it handles all annotations in Spring MVC. Since @ Controller has the same role as @ Component in Spring beans contents. So I only need to use component scan to match these controllers as beans. – solvator Jan 09 '14 at 17:23