Spring MVC Notes
Spring MVC Notes
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;
}