Reputation: 7529
Hi I know this question has been asked and answered many many times..I checked various question on stackoverflow
and on other web pages but still unable to find the solution
I am vert much beginner to Spring MVC
.
I am trying to create a simple HelloWorld
Program using Spring MVC- Controller
.
I have written the following code, but its giving me HTTP Status 404
when i try to access http://localhost:5151/SpringMaven/
This is my project hierarchy
Web.xml
<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>
Spring Configuration File
<context:component-scan base-package="com.evantage.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Controller
@Controller
public class HelloController {
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
String message = "Hello World";
return new ModelAndView("welcome", "message", message);
}
}
I have been trying for 2 days, but unable to get success. Kindly guide me
Thanks
Upvotes: 0
Views: 125
Reputation: 3917
Add those line in your dispatcher-servlet.
<context:component-scan base-package="com.evantage"/>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
Add those line in web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Let me know what is the output.
UPDATE:
In dispatcher-servlet.xml add those line in <bean>
tag.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
Upvotes: 0
Reputation: 1103
Try with this web.xml
:
<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>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</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>com.evantage</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>
And enable autowire in your dispatcher:
<!-- Enable autowire -->
<context:annotation-config />
Upvotes: 1
Reputation: 8387
Try to add this to Spring Configuration File:
<mvc:annotation-driven>
And add this in schema in top:
http://www.springframework.org/schema/mvc
Upvotes: 1
Reputation: 2057
Because you try to access /SpringMaven
URL, but you've bound your controller action to /welcome
path.
Either change @RequestMapping
annotation to use expected URL, or go to http://localhost:5151/welcome
.
-edit-
And make sure your controller is registred as a service - either register by hand in Spring configuration or enable MVC annotations scanning so Spring can auto-find the @Controller
-annotated service.
Upvotes: -1