First spring MVC web application
In this page,
I have given first spring MVC web application step by step.
In this spring MVC web application we will get
the output message on the web browser along with input field as shown in the
bellow.
Steps to create spring MVC web application:
Note: load the jar files in lib folder also
1. Load the required jar file.
2. Create the request page to give input.
3. Create the Controller class.
4. Create the web.xml file.
5. Create the spring configuration xml
file.
6. Run the project.
Load the required jar files:
·
commons-logging-1.1.1
·
org.springframework.asm-3.0.1
·
org.springframework.beans-3.0.1
·
org.springframework.context-3.0.1
·
org.springframework.core-3.0.1
·
org.springframework.expression-3.0.1
·
org.springframework.web-3.0.1
·
org.springframework.web.servlet-3.0.1
Create the request page to give input: This file contains input fields.
index.jsp
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Welcome page</title>
</head>
<body>
<h1>Welcome to first
spring web app</h1>
<form action="./hello.ds">
name : <input type="text"
name="name"/><br>
<input type="submit"
value="submit"/>
</form>
</body>
</html>
Create the Controller class: in this file we get the details from
input page and send the result to success page.
HelloController.java
package com.javathub;
import java.util.HashMap;
import java.util.Map;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.Controller;
public class HelloController implements Controller {
@Override
public ModelAndView
handleRequest(HttpServletRequest req,
HttpServletResponse res) throws Exception {
String name=req.getParameter("name");
Map m=new HashMap();
m.put("msg", "hello...."+name);
ModelAndView mav=new ModelAndView("success",m);
return mav;
}
}
Create the web.xml file: in this file we are specifying the
DispatcherServlet, it is act as front controller. This file forwards the input requests
to DispatcherServlet.
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>FirstSpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- default configurations -->
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>*.ds</url-pattern>
</servlet-mapping>
</web-app>
Create the spring configuration xml
file: this file name
should be (what we specified in web.xml)servletname-servlet.xml, here we define
the handler mapping class, Controller class and configure the view resolver.
helloworld-servlet.xml
<?xml version="1.0"
encoding="UTF-8"?>
<!DOCTYPE beans
PUBLIC "-//SPRING//DTD
BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- default
configuration -->
<!--default
handler mapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
</bean>
<!-- define
controller -->
<bean name="/hello.ds"
class="com.javathub.HelloController"></bean>
<!-- view
resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"
value="/"></property>
<property name="suffix"
value=".jsp"></property>
</bean>
</beans>
Output page:
success.jsp
${msg}
application run successfully but sucess.jsp not created.
ReplyDeletewhat type of exception you got? we have to create the success.jsp, it will not create automatically.
Delete