icons8-facebook-64 icons8-discord-64

icons8-instagram-64 icons8-markdown-64

portfolio@dsv:~$

MS-CS @ Rochester Institute of Technology, NY

View on GitHub Tech Music Philosophy Leetcode Solutions

back

Spring Basics



MVC Architecture

MVC - Non-REST setup

Here’s how the Controller action would look like:

public String showForm(Model theModel) {
    // create student object
    Student theStudent = new Student();

    // add student object to the model
    theModel.addAttribute("student", theStudent);
    theModel.addAttribute("countries", moreCountryOptions);
    return "student-form";
}

The corresponding view page will have the following:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Student Confirmation</title>
	</head>
	<body>
		<a href="showForm"> Back to student form </a>
		<br><br>
		The student is confirmed: ${student.firstName} ${student.lastName}
		<br><br>
		Country: ${student.country}
		<br><br>
		Favorite Language: ${student.favoriteLanguage}
		<br><br>
		Operating Systems:
			<ul>
				<c:forEach var="temp" items="${student.operatingSystems}">
					<li> ${temp} </li>
				</c:forEach>
			</ul>
	</body>
</html>

Spring MVC Form Tags

In the JSP view page include the following to use form tags.

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

Code sample for a form:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Student Registration Form</title>
	</head>
	<body>
		<a href="/spring-mvc-demo/"> Back to main menu </a>
		<br><br>
		<form:form action="processForm" modelAttribute="student">
			First Name: <form:input path="firstName"/>
			<br><br>
			Last Name: <form:input path="lastName"/>
			<br><br>
			Country:
				<form:select path="country">
					<form:options items="${countries}" />
					<form:options items="${student.countryOptions}" />
					<form:option value="Indonesia" label="Indonesia" />
					<form:option value="Sri Lanka" label="Sri Lanka" />
					<form:option value="Poland" label="Poland" />
					<form:option value="Iceland" label="Iceland" />
				</form:select>
			<br><br>
			Favorite Language:
				<form:radiobutton path="favoriteLanguage" value="Java" /> Java
				<form:radiobutton path="favoriteLanguage" value="Ruby" /> Ruby
				<form:radiobutton path="favoriteLanguage" value="Python" /> Python
				<form:radiobutton path="favoriteLanguage" value="Javascript" /> JavaScript
				<form:radiobuttons path="favoriteLanguage" items="${student.favoriteLanguageOptions}" />
			<br><br>
			Operating Systems:
				Linux <form:checkbox path="operatingSystems" value="Linux" />
				Mac OS <form:checkbox path="operatingSystems" value="Mac OS" />
				Windows <form:checkbox path="operatingSystems" value="Windows" />
			<br><br>
			<input type="submit" value="Submit" />
		</form:form>
	</body>
</html>