Springboot MVC – Passing Values from JSP to Controller

In this article let us see how to pass values from JSP page to Spring Controller. We are going to implement MVC with SpringBoot.

For understanding Springboot with MVC – Basic example – Please refer here

In this example, We are going to have a Employee Bean, Controller and a main class. We are going to have 2 JSP pages, home.jsp and welcome.jsp.

home.jsp page loads when the springboot application is deployed, we will enter the name there and the name will be passed to controller and from controller it will go to welcome.jsp and will display the name.

In order to use spring form in the JSP pages, we require to include this taglib

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

Here is the Project Structure,

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>SpringBootMVC</groupId>
	<artifactId>SpringBootMVC</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>SpringBootMVC</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.source>1.8</maven.compiler.source>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- Tomcat for JSP rendering -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>

		</dependency>

		<!-- JSTL tag lib -->
		<dependency>
			<groupId>javax.servlet.jsp.jstl</groupId>
			<artifactId>javax.servlet.jsp.jstl-api</artifactId>
			<version>1.2.1</version>
		</dependency>

		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>


	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

Employee.java

package com.javainfinite.model;

import org.springframework.stereotype.Component;

@Component
public class Employee {

	private String empName;

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

}

SpringController.java

package com.javainfinite.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.javainfinite.model.Employee;

@Controller
public class SpringController {
	
	@Autowired
	Employee employee;

	@RequestMapping(value = "/")
	public String welcome(Model model) {
		model.addAttribute("Employee", employee); //Note This Line
		return "home";
	}
	
	@RequestMapping(value="/welcome", method=RequestMethod.POST)
	public String welcomeUser(@RequestParam("empName") String name, Model model) {
		model.addAttribute("name", name);
		return "welcome";
	}
}

In the above code, We have declared model.addAttribute(“Employee”, employee); where employee is a bean and is autowired. So the bean is made available.

What will happen if we don’t declare Bean?
Well, if we dont make the bean available, we will be hitting an exception – BindingResult nor plain target object for bean name available as request attribute.

So when creating an mvc project, make sure bean is available.

home.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"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>Home Page</title>
</head>
<body>
	Welcome to SpringBoot MVC
	<form:form modelAttribute="Employee" method="POST" action="/welcome">
User Name :
<form:input path="empName" />
		<input type="submit" value="Submit">
	</form:form>
</body>
</html>

welcome.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>Insert title here</title>
</head>
<body>Welcome ${name }

</body>
</html>

App.java

package com.javainfinite.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@ComponentScan("com.javainfinite")
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

Output:

 

 

 

Download code here

By Sri

Leave a Reply

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