Creating a UI form and process with spring mvc controller

This is a basic tutorial of getting form values to spring controller and printing them in console.

we have a page called createoffer.jsp which it will take form values.

createoffer.jsp


<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<!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=US-ASCII">
<title>Insert title here</title>
</head>
<body>

//we transferred the form request to /docreate controller using expression language and then we passed the form 
//data to docreate controller model object.
<form method="get" action=${pageContext.request.contextPath}/docreate>
<table>
<tr><td>Name: </td><td><input name="name" type="text"/></td></tr>
<tr><td>Email: </td><td><input name="email" type="text"/></td></tr>
<tr><td>Your offer: </td><td><textarea name="text" rows="10" cols="10"></textarea></td></tr>
<tr><td> </td><td><input value="Create advert" type="submit"/></td></tr>
</table>
</form>

</body>
</html>

OffersController.java


@RequestMapping("/docreate")
public String doCreate(Model mode, Offer offer) {

System.out.println(offer);
return "offercreated";
}

output:

Offer [id=0, name=krishna, email=pbkrishna.java, text=hello, any doughts on spring clear here. Its Free!]

Download

the complete source code is here http://sourceforopensource.com/wp-content/uploads/2016/06/offers.zip

Balakrishna Pendyala

Author: Balakrishna Pendyala

Balakrishna is a Software Engineer working in Media Streaming Domain, and also worked on Banking and Health Care domains. His areas of interest include Object Oriented Design, SOLID Design principles, Restful Web Services and Open Source softwares including Spring, Spring Boot, and Hibernate.

If you found an error, highlight it and press Shift + Enter or click here to inform us.

Leave a Reply

Your email address will not be published. Required fields are marked *