You are on page 1of 4

EXPERIMENT No.

8
 Setting up a New Project with Spring Web MVC Support

 Implementing a Service
import org.springframework.stereotype.Service;

@Service

public class PizzaOrderService (

public void orderAPizza (String pizzaType, int quantity) (

System.out.println("Ordered "+ quantity + + pizzaType + pizza (s)");

public double generateBill (String pizzaType, int quantity) (

double pricePerPizza getPricePerPizza (pizzaType);

double totalBill pricePerPizza quantity;

System.out.println("Total bill for quantity+""+ pizzaType + pizza(s): $" + totalBill); return totalBill;

private double getPricePerPizza (String pizzaType) {

return 10.0;
}

public static void main(String[] args) (

PizzaOrderService pizzaOrderService new PizzaOrderService();

pizzaOrderService.orderAPizza ("Cheese Burst Pizza", 2);

double totalBill pizzaOrderService.generateBill("Corn Pizza", 3); System.out.println("Total bill: $" +


totalBill);

 Implementing the Controller and Model


import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class UserController ( private string username;
private String email;
public String getUsername() [ return username;
}
public void setUsername (String username) (
this.username username;
public String getEmail() ( return email;
public void setEmail (String email) ( this.email email;
GetMapping("/user-form")
public String showUserForm() {
return "user-form";
@PostMapping("/process-user")
public String processUserForm (@RequestParam String username, RequestParam String
email, Model model) (
User user = new User();
user.setUsername (username);
user.setEmail(email);
model.addAttribute("user", user);
if (username.equals("admin
return "admin-view";
")) {
} else {
return "user-view";
}

 Implementing the Views

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Welcome Page</title>

</head>

<body>

<h2>Welcome</h2>

<form action="helloView" method="post">

<label for="name">Enter your name:</label>

<input type="text" id="name" name="name">

<input type="submit" value="Submit">

</form>

</body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8 pageEncoding="UTF-8">

<!DOCTYPE html>

<html>

<head>
<meta charset="UTF-8">

<title>Greeting Page</title>

</head>

<body>

<p>Hi, <%= request.getParameter("name") >!</p>

</body>

</html>

 OUTPUT

You might also like