Read From Csv File Line by Line Jsp Input Box

In this tutorial, y'all volition acquire how to write code to brandish a form that captures information from user and write lawmaking to get form information when the user submits the grade. I will walk you lot to develop a Spring Boot awarding that presents the following course to the user:

spring boot form

As you can see, this form contains near standard HTML input fields like textbox, radiobutton, select box (dropdown list), checkbox and text area – forth with labels and submit push.

We volition be using Jump Boot with Spring MVC, Bound Class tags and JSP. You lot can utilize any Java IDE like Eclipse or Jump Tool Suite.

If y'all are new to Leap Boot, I recommend you to follow this tutorial first. Then come dorsum to this one.

1. Create Spring Boot project

You can create a Bound Boot project using the Spring Initializr tool then import the project into Eclipse IDE. If y'all use Spring Tool Suite, you can create a Spring Boot projection right inside the IDE. In any manner, the project must have the following dependency information in the pom.xml file:

<?xml version="1.0" encoding="UTF-viii"?> <project ...> 	<modelVersion>iv.0.0</modelVersion> 	<parent> 		<groupId>org.springframework.boot</groupId> 		<artifactId>spring-boot-starter-parent</artifactId> 		<version>2.ii.four.RELEASE</version> 		<relativePath /> 	</parent> 	 	<groupId>cyberspace.codejava</groupId> 	<artifactId>SpringBootFormHandling</artifactId> 	<version>1.0</version> 	 	<name>SpringBootFormHandling</proper name> 	<description>Form handling with Spring Boot</clarification>  	<properties> 		<java.version>1.8</coffee.version> 	</properties>  	<dependencies> 		<dependency> 			<groupId>org.springframework.boot</groupId> 			<artifactId>spring-boot-starter-web</artifactId> 		</dependency>		 	</dependencies>  	<build> 		<plugins> 			<plugin> 				<groupId>org.springframework.kicking</groupId> 				<artifactId>spring-kicking-maven-plugin</artifactId> 			</plugin> 		</plugins> 	</build>  </project>

Every bit y'all tin can run into, here we utilize Leap Boot two.2.4 with Java one.eight. The spring-boot-starter-spider web dependency provides default configuration for a Spring MVC spider web application running on embedded Tomcat server. The spring-boot-maven-plugin enables packaging our Leap Boot application to a JAR/War file.

And brand sure that the project has the post-obit directory structure:

Spring boot form handling project structure

All the configurations for a Jump MVC application are done by Jump Boot nether the hood.

TIP:  UseLeap Kicking DevTools for automatic restart so you don't accept to manually restart the application during development.

2. Enable JSP with Spring Kicking

To use JSP (JavaServer Pages) with Jump Boot, you must add together the following dependency in the pom.xml file:

<dependency> 	<groupId>org.apache.tomcat.embed</groupId> 	<artifactId>tomcat-embed-jasper</artifactId> 	<telescopic>provided</scope> </dependency>

And configure Spring MVC resolver in the application.properties file equally follows:

bound.mvc.view.prefix=/Spider web-INF/views/ bound.mvc.view.suffix=.jsp

This means you lot must put JSP files nether this directory in the project:

src/main/webapp/WEB-INF/views

3. Create Jump MVC Controller Class

Create a Spring MVC controller class named MvcController with the post-obit initial lawmaking:

package cyberspace.codejava;  import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;  @Controller public class MvcController { 	 	@RequestMapping("/") 	public String domicile() { 		Organisation.out.println("Going domicile..."); 		return "alphabetize"; 	} }

You tin can come across the handler method domicile() volition serve requests to the application'due south homepage (context root /). It returns the view named index which will be resolved to a JSP file under /WEB-INF/views/index.jsp.

four. Update Homepage

Side by side, create the homepage index.jsp nether the src/principal/webapp/Spider web-INF/views directory with the following HTML code:

<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Bound Kicking Form Handling Example</title> </head> <torso> 	<h1>Spring Boot Form Handling Example</h1> 	<h2>Spring Course Tags with JSP</h2> 	<a href="/register">Click hither</a> </torso> </html>

Run the Jump Boot application, and access the web awarding at localhost, you volition see the homepage like this:

spring boot form homepage

When the user clicks the hyperlink, the user registration form will display.

v. Code Domain Model Grade

Create the User class that represents the user's details in the registration class, as beneath:

packet net.codejava;  import java.sql.Date;  public class User { 	individual Cord name; 	private String email; 	individual Cord gender; 	private String countersign; 	private String profession; 	private String note; 	private Date birthday; 	individual boolean married;  	// getters... 	// setters...  	// override toString() 	 }

Note that getter and setter methods are non shown for brevity. You should also override the toString() method that returns all the details of the user.

half dozen. Lawmaking Show Form Handler Method

To handle request that comes to the application when the user clicks the hyperlink in homepage, add the post-obit method in the controller class:

public Cord showForm(Model model) { 	User user = new User();	 	List<String> professionList = Arrays.asList("Developer", "Designer", "Tester"); 	 	model.addAttribute("user", user); 	model.addAttribute("professionList", professionList); 	 	return "register_form"; }

With the following import statements:

import java.util.Arrays; import java.util.List;  import org.springframework.ui.Model; import org.springframework.spider web.demark.annotation.GetMapping;

The showForm() handler method will be called when the user clicks the hyperlink /register in the homepage. It creates and adds two objects to the model:

- The User object to capture values of fields on the class.

- The professionList to display values for the dropdown list (select box) on the form.

So it returns the view proper noun register_form – which volition be resolved to a JSP file described side by side.

vii. Lawmaking Spring MVC Class Page

To show the user registration grade to the user, create the register_form.jsp file nether /Web-INF/views with the following lawmaking:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"     pageEncoding="ISO-8859-1"%> <%@ taglib prefix="form" uri="http://world wide web.springframework.org/tags/form" %>     <!DOCTYPE html> <html> <caput> <meta charset="ISO-8859-1"> <championship>User Registration Grade</championship> </caput> <body> 	<div marshal="middle"> 		<h2>User Registration</h2> 		<class:class action="register" method="post" modelAttribute="user"> 			<form:label path="name">Full name:</form:label> 			<form:input path="name"/><br/> 			 			<course:label path="email">Email:</form:label> 			<class:input path="email"/><br/> 			 			<course:characterization path="password">Password:</form:label> 			<class:password path="countersign"/><br/>		  			<form:label path="birthday">Birthday (yyyy-mm-dd):</form:label> 			<class:input path="birthday"/><br/> 			 			<form:label path="gender">Gender:</form:label> 			<class:radiobutton path="gender" value="Male"/>Male 			<course:radiobutton path="gender" value="Female"/>Female<br/> 			 			<class:label path="profession">Profession:</form:label> 			<course:select path="profession" items="${professionList}" /><br/> 					 			<form:label path="married">Married?</form:characterization> 			<class:checkbox path="married"/><br/> 			 			<class:characterization path="annotation">Note:</form:label> 			<form:textarea path="note" cols="25" rows="five"/><br/> 				 			<form:push button>Annals</form:button> 		</form:form> 	</div> </torso> </html>

We use Spring form tags to map an object in the model with fields in the form, so it is required to declare the post-obit directive at the beginning of the file:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

So nosotros use the <course:class> tag to create a form:

<form:form action="annals" method="post" modelAttribute="user">

Here, pay attention to the modelAttributeattribute that specifies proper name of an object in the model that maps to this class. Remember the user object in the showForm() method?

Then for each form field, we use Leap grade tags for the label and the field:

<class:label path="email">E-mail:</form:label> <course:input path="email"/>

Note that the value of the path attribute must refer to a field in the model object.

To format the form, add together the post-obit CSS style into the page'due south head section:

<head> ... <style type="text/css"> 	label { 		display: inline-block; 		width: 200px; 		margin: 5px; 		text-align: left; 	} 	input[type=text], input[blazon=password], select { 		width: 200px;	 	} 	input[blazon=radio] { 		display: inline-block; 		margin-left: 45px; 	} 	input[type=checkbox] { 		brandish: inline-block; 		margin-right: 190px; 	}	 	 	push { 		padding: 10px; 		margin: 10px; 	} </style> </caput>

When running, the form looks like the following screenshot:

spring boot form

8. Code Class Submission Handler Method

To handle the grade submission when the user clicks Register push button on the grade, add together the following method to the controller class:

@PostMapping("/register") public String submitForm(@ModelAttribute("user") User user) { 	 	System.out.println(user); 	 	return "register_success"; }

With the following import statements:

import org.springframework.web.bind.notation.ModelAttribute; import org.springframework.web.demark.annotation.PostMapping;

As y'all can meet, the method of the form is mail service so the @PostMappingannotation is used for the handler method to procedure HTTP Mail service request.

Annotation that the @ModelAttribute annotation is used for the method parameter User user – so Spring will know to read values of fields on the class and fix them to the model object's fields. We don't have to write a unmarried line of code to get form data – but use the model object!

The submitForm() method merely prints the user object's details to the console, and return the view named register_success which resolves to the JSP page described beneath.

9. Code Outcome Page

Finally, create the register_success.jsp file with the post-obit code:

<%@ page linguistic communication="java" contentType="text/html; charset=ISO-8859-1"     pageEncoding="ISO-8859-i"%> <!DOCTYPE html> <html> <caput> <meta charset="ISO-8859-ane"> <title>Registration Success</title> <mode type="text/css"> 	span { 		display: inline-block; 		width: 200px; 		text-align: left; 	} </style> </head> <body> 	<div align="eye"> 		<h2>Registration Succeeded!</h2> 		<span>Full name:</span><bridge>${user.proper noun}</span><br/> 		<bridge>Due east-mail:</span><span>${user.email}</bridge><br/> 		<span>Countersign:</bridge><span>${user.countersign}</bridge><br/> 		<span>Altogether:</span><span>${user.birthday}</span><br/> 		<span>Gender:</span><bridge>${user.gender}</span><br/> 		<bridge>Profession:</span><span>${user.profession}</span><br/> 		<span>Married?:</span><bridge>${user.married}</bridge><br/> 		<bridge>Note:</span><span>${user.notation}</span><br/> 	</div> </body> </html>

This page but uses Expression Language (EL) of JSP to display the information of the form submitted the user, equally shown in the following screenshot:

spring boot form success

That'south the tutorial almost handling course in Spring Boot using Leap grade tags and JSP. You can download the sample projection attached beneath.

To validate the user'due south input for the course, refer to Spring Boot Form Validation Tutorial.

You can too lookout the video version of this tutorial here:

Other Spring Boot Tutorials:

  • Spring Boot Hello World Example
  • Spring Boot automatic restart using Bound Kick DevTools
  • Jump Boot Hi Globe RESTful Web Services Tutorial
  • How to create a Spring Kick Web Application (Spring MVC with JSP/ThymeLeaf)
  • Bound Boot - Spring Information JPA - MySQL Example
  • Spring Boot Grime Example with Spring MVC – Spring Data JPA – ThymeLeaf - Hibernate - MySQL
  • How to use JDBC with Spring Boot
  • Jump Boot CRUD Web Application with JDBC - Thymeleaf - Oracle
  • Leap Boot RESTful Crud API Examples with MySQL database
  • How to package Spring Boot application to JAR and WAR

About the Author:

Nam Ha Minh is certified Coffee programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Coffee since then. Brand friend with him on Facebook and sentry his Coffee videos you YouTube.

Add comment

toddmilignigh.blogspot.com

Source: https://www.codejava.net/frameworks/spring-boot/spring-boot-form-handling-tutorial-with-spring-form-tags-and-jsp

0 Response to "Read From Csv File Line by Line Jsp Input Box"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel