Here, I demonstrate both the approach in single example.
Project Structure:
1)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">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.go</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
</web-app>
2)dispatcher-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="com.Raamji.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean name="/welcome.go" class="com.Raamji.controller.HelloWorldController" >
<property name="message" value="Jai Sh Raam!" />
</bean>
</beans>
3)redirect.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("welcome.go"); %>
4)HelloWorlController.java
package com.Raamji.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HelloWorldController extends AbstractController {
private String message;
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("Welcome");
return new ModelAndView("welcomePage","welcomeMessage", message);
}
public void setMessage(String message) {
this.message = message;
}
}
5)welcomePage.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Page</title>
</head>
<body>
${welcomeMessage}
</body>
</html>
6)home.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="input" action="addContact.go" method="post">
Username: <input type="text" name="name">
Age: <input type="text" name="age">
<input type="submit" value="Submit">
</form>
</body>
</html>
7)HomeController.java
package com.Raamji.controller;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.Raamji.model.Student;
@Controller
@SessionAttributes
public class HomeController {
@RequestMapping(value = "/addContact.go", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact")Student student, BindingResult result) {
System.out.println("Name:" + student.getName() +
"Age:" + student.getAge());
return "redirect:home.html";
}
@RequestMapping("/showcontacts.go")
public ModelAndView showContacts() {
return new ModelAndView("contact", "command", "");
}
}
8)Student.java
package com.Raamji.model;
import java.util.List;
public class Student {
String name;
List<String> city;
int age;
boolean gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getCity() {
return city;
}
public void setCity(List<String> city) {
this.city = city;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
}
//Annotation based
http://localhost:8080/ShRaamSpringMVC/home.html
//xml configuration based
http://localhost:8080/ShRaamSpringMVC/
No comments:
Post a Comment