3:37 pm - Sunday September 21, 0634

Spring MVC Form controller Example Prgram Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Project Structure:

 

 

 

 

 

 

 

 

 

 

 

JAR Files:

 

 

 

 

 

 

 

Controller.java:

package com.candidjava;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.candidjava.Student;

@SuppressWarnings("deprecation")
public class Controller extends SimpleFormController {

	public Controller() {
		setCommandClass(Student.class);
		System.out.println("Student controller");
		setCommandName("user");
		System.out.println("user");
	}

	protected ModelAndView onSubmit(Object command) throws Exception {
		Student user = (Student) command;
		// userService.add(user);
		System.out.println("model and view");
		return new ModelAndView("SuccessPage", "user", user);

	}

}

Student.java:

package com.candidjava;

public class Student {

	private String name;
	private String degree;
	private String mark;
	private String address;
	private String mailId;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setDegree(String degree) {
		this.degree = degree;
	}

	public String getDegree() {
		return degree;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public String getMark() {
		return mark;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getAddress() {
		return address;
	}

	public void setMailId(String mailId) {
		this.mailId = mailId;
	}

	public String getMailId() {
		return mailId;
	}

}

redirect.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
	response.sendRedirect("userRegistration.htm");
%>

FormDetails.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Page</title>
</head>
<body bgcolor="pink">

<form:form method="POST" commandName="user">
	<table>
		<tr>
			<td>Enter Ur Name :</td>
			<td><form:input path="name" /></td>
		</tr>

		<tr>
			<td>Enter Degree</td>
			<td><form:input path="degree" /></td>
		</tr>

		<tr>
			<td>Enter Mark</td>
			<td><form:input path="mark" /></td>
		</tr>

		<tr>
			<td>Enter Address</td>
			<td><form:input path="address" /></td>
		</tr>

		<tr>
			<td>Enter Mail ID</td>
			<td><form:input path="mailId" /></td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" value="submit"></td>
		</tr>
	</table>
</form:form>

</body>
</html>

SuccessPage.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success Page</title>
</head>
<body bgcolor="pink">
Your Details
<hr>
User Name : ${user.name}
<br />
Degree : ${user.degree}
<br />
Mark : ${user.mark}
<br />
Address : ${user.address}
<br />
Mail Id : ${user.mailId}
<br />

</body>
</html>

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>SpringExample6</display-name>
	<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>*.htm</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>redirect.jsp</welcome-file>
	</welcome-file-list>
</web-app>

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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/jspFiles/" p:suffix=".jsp" />

	<bean name="/userRegistration.htm" class="com.candidjava.Controller"
		p:formView="FormDetails" p:successView="SuccessPage" />

</beans>

OUTPUT:

DOWNLOAD+SOURCECODE

Filed in: Z-A Spring Codes

Comments are closed.