Reputation: 4259
I have tried simple Hello world program using spring.It is not working as expected.
package com.Spring;
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
public class Hello {
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String helloWorld(ModelMap model){
model.addAttribute("message", "hello world");
return "hello";
}
}
HelloWeb-servlet.xml file.
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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.Spring" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>com.Spring.Hello</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
hello.jsp file.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
${message}
</body>
</html>
I have created war file using ant and deployed.After deployed war file there is no error in the terminal.Now i have try to access URL in browser it shows 404 error.
http://ipaddress:8080/SpringHello/hello.jsp
How to access the URL? or what am i did wrong?
Upvotes: 1
Views: 1458
Reputation: 325
The Handlermapping is missing in the context file, too. So add annotation-driven bean on top of what Kamlesh suggested.
<mvc:annotation-driven />
Upvotes: 1
Reputation: 4962
You are adding wrong servlet class in web.xml.
<servlet-class>com.Spring.Hello</servlet-class>
It should be :
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
For more understanding : http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/
Upvotes: 0