0% found this document useful (0 votes)
288 views6 pages

Spring MVC Notes

This document provides steps for setting up and implementing a basic Spring MVC application. It includes: 1) Configuring the DispatcherServlet in web.xml to handle requests; 2) Creating a Spring configuration file named after the servlet to enable annotations and component scanning; 3) Configuring a ViewResolver to map views to JSP files; 4) Creating a sample Controller class annotated with @Controller and mapping requests to methods; 5) Returning a ModelAndView from controller methods to render views along with model data.

Uploaded by

VKM2013
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
288 views6 pages

Spring MVC Notes

This document provides steps for setting up and implementing a basic Spring MVC application. It includes: 1) Configuring the DispatcherServlet in web.xml to handle requests; 2) Creating a Spring configuration file named after the servlet to enable annotations and component scanning; 3) Configuring a ViewResolver to map views to JSP files; 4) Creating a sample Controller class annotated with @Controller and mapping requests to methods; 5) Returning a ModelAndView from controller methods to render views along with model data.

Uploaded by

VKM2013
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

Spring MVC Notes:

>Change Perspective to JavaEE


> Get a server view.
(Window->Showview->server)
>select a server , browse Head/Installed Location.
>Finish
--------------------------------------------------File>new>Dynamic Web Project.
Provide Project name>next>next> Check web.xml
generation
>Finish
---------------------------Sample Proram:
JARS:
Copy and paste all Spring-JARS in lib folder.
--------------------------------------Step 1:
Configure DispatcherServlet in web.xml
step 2: creating spring config file:
name rule: [<servlet-name>]-servlet.xml
Location: /WEB-INF/ location.
Step 3: Configure A view Resolver Class. With
prefix and
suffix.
Step 4: Controller
>Create a Java class.

>Use an annotation @Controller (Sterio type annot.


)
>Enable annotations in config file
(use context schema for base pakage and
annotation config)
>Writre a request method.
(@RequestMapping("/url"))
ex: @RequestMapping("/home") -- GET type
@RequestMapping(value="/abcd",method=Request
Method.POST)
> Return a ModelAndView Object.
ex:
return new ModelAndView("HomePage");

Step 5: VIEW : create a folder structure for prefix


(if not exist ex: /WEB-INF/jsps)
-------------------------------------Note: RequestMethod is enum, which define named
constans
for request methods like GET,POST,PUT,TRACE...etc

Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" 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">
<servlet>
<servlet-name>sample</servlet-name>
<servletclass>org.springframework.web.servlet.DispatcherServlet</servle
t-class>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>*.abc</url-pattern>
</servlet-mapping>
</web-app>

Sample-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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context
"
xsi:schemaLocation="http://www.springframework.org/schema/be
ans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/springcontext.xsd
">
<context:annotation-config/>
<context:component-scan basepackage="com.app.controller"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceVi
ewResolver">
<property name="prefix">
<value>/WEB-INF/jsps/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>
HomeController.java

package com.app.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@RequestMapping("/home")
public ModelAndView showView(){
//ModelAndView mav=new
ModelAndView("HomePage");
//return mav;
String s1="hello";
Employee emp=new Employee();
emp.setEmpId(101);
emp.setEmpName("ABCD");
List<String> listData=new ArrayList<String>();
listData.add("hello");
listData.add("bye");
listData.add("ok");
return new
ModelAndView("HomePage","listData",listData);
}
@RequestMapping(value="/home",method=RequestMethod.POST
)
public ModelAndView showView1(){
return null;

}
}
Employee.java
package com.app.controller;
public class Employee {
private int empId;
private String empName;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}

You might also like