Java Spring Model View Controller Hello World Example in Eclipse
Description : In this post we are sharing only source code for java spring application, For theoretical note you can refer this site I could nt provide a better description on java spring than this. Please note the deployment descriptor (web.xml) where we had provide the name index.jsp as our welcome file.
index page (index.jsp)
<html><head>
<title>Office Management System</title>
</head>
<body>
<a href="hello.html">Click here</a>
</body>
</html>
Deployment Descriptor (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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CfedOMS</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-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: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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="net.cfed.oms.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
JSTL (Java Standard Tag Library) : Expression Language ( EL) makes it possible to easy access application data stored in java bean.
Format : ${expression}
hello.jsp
<html><head> <title> This is my First application in Java Spring MVC </title></head>
<body> ${message} </body>
</html>
What and Why annotation in java ?
Annotation allows implemented class to be auto-detected during class path spanning.
format : @component
RequestMapping is most used annotation method in java spring. It is used to map web request to a specific handler method. Here ModelAndViewHelloWorld Method. This concept is similar to java jsp servlets
Controller it indicates that the annotated class is a controller i e a web controller.
HelloworldController.java
package net.cfed.oms.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello, This is my first sample spring MVC project !";
System.out.println(message);
return new ModelAndView("hello", "message", message);
}
}
Project structure
Once you configured java spring, you can easily work on the business logic of your application.
http://javabelazy.blogspot.in/
No comments:
Post a Comment
Your feedback may help others !!!