Skip to content

Commit 6d60beb

Browse files
authored
Merge pull request #123 from WebFuzzing/develop-triangle-spa
Develop triangle spa
2 parents 96f0432 + 6e43d4b commit 6d60beb

File tree

7 files changed

+190
-9
lines changed

7 files changed

+190
-9
lines changed
Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,58 @@
1-
TODO replace me
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.4.3</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.example</groupId>
12+
<artifactId>trianglespa</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>TriangleSPA</name>
15+
<description>TriangleSPA</description>
16+
<url/>
17+
<licenses>
18+
<license/>
19+
</licenses>
20+
<developers>
21+
<developer/>
22+
</developers>
23+
<scm>
24+
<connection/>
25+
<developerConnection/>
26+
<tag/>
27+
<url/>
28+
</scm>
29+
<properties>
30+
<java.version>21</java.version>
31+
</properties>
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-web</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.trianglespa;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
5+
import static org.springframework.boot.SpringApplication.*;
6+
7+
@SpringBootApplication
8+
public class TriangleSPAApplication {
9+
public static void main(String[] args) {
10+
run(TriangleSPAApplication.class, args);
11+
}
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Triangle Classifier SPA</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
<body>
9+
<h1>Triangle Classifier SPA</h1>
10+
11+
<div id="main-view">
12+
<form id="triangleForm">
13+
<label for="a">Side A:</label>
14+
<input type="text" id="a" name="a" required><br>
15+
16+
<label for="b">Side B:</label>
17+
<input type="text" id="b" name="b" required><br>
18+
19+
<label for="c">Side C:</label>
20+
<input type="text" id="c" name="c" required><br>
21+
22+
<button type="submit">Classify</button>
23+
</form>
24+
25+
<div id="result" class="hidden result"></div>
26+
</div>
27+
28+
<script src="script.js"></script>
29+
</body>
30+
</html>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function classifyTriangle(a, b, c) {
2+
a = Number(a);
3+
b = Number(b);
4+
c = Number(c);
5+
6+
if (a <= 0 || b <= 0 || c <= 0 || a + b <= c || a + c <= b || b + c <= a) {
7+
return "not-a-triangle";
8+
} else if (a === b && b === c) {
9+
return "equilateral";
10+
} else if (a === b || b === c || a === c) {
11+
return "isosceles";
12+
} else {
13+
return "scalene";
14+
}
15+
}
16+
17+
document.getElementById("triangleForm").addEventListener("submit", function (event) {
18+
event.preventDefault();
19+
20+
const a = document.getElementById("a").value;
21+
const b = document.getElementById("b").value;
22+
const c = document.getElementById("c").value;
23+
24+
const resultType = classifyTriangle(a, b, c);
25+
const resultText = `Result: ${resultType.replace(/-/g, " ")}`;
26+
27+
// Update URL and show result
28+
history.pushState({ a, b, c }, "", `/${resultType}`);
29+
const resultEl = document.getElementById("result");
30+
resultEl.textContent = resultText;
31+
resultEl.classList.remove("hidden");
32+
});
33+
34+
// Handle back/forward navigation
35+
window.onpopstate = function () {
36+
const resultEl = document.getElementById("result");
37+
if (location.pathname === "/") {
38+
resultEl.classList.add("hidden");
39+
} else {
40+
const text = location.pathname.slice(1).replace(/-/g, " ");
41+
resultEl.textContent = `Result: ${text}`;
42+
resultEl.classList.remove("hidden");
43+
}
44+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
padding: 30px;
4+
background-color: #f9f9f9;
5+
}
6+
7+
h1 {
8+
color: #333;
9+
}
10+
11+
form {
12+
margin-bottom: 20px;
13+
}
14+
15+
label {
16+
margin-right: 10px;
17+
}
18+
19+
input {
20+
margin-bottom: 10px;
21+
padding: 5px;
22+
width: 60px;
23+
}
24+
25+
button {
26+
padding: 6px 12px;
27+
cursor: pointer;
28+
}
29+
30+
.result {
31+
margin-top: 20px;
32+
font-weight: bold;
33+
color: #00529B;
34+
}
35+
36+
.hidden {
37+
display: none;
38+
}

jdk_21_maven/cs/web/triangle-ssr/src/main/java/com/example/springmvcdocker/FormController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ public String showForm() {
1717

1818
@PostMapping("/process")
1919
public String processForm(
20-
@RequestParam("a") int a,
21-
@RequestParam("b") int b,
22-
@RequestParam("c") int c)
20+
@RequestParam("a") String a,
21+
@RequestParam("b") String b,
22+
@RequestParam("c") String c)
2323
{
24-
int value = TriangleClassification.classify(a, b, c);
24+
int value = TriangleClassification.classify(Integer.parseInt(a), Integer.parseInt(b), Integer.parseInt(c));
2525
String triangleType = getTriangleType(value);
2626
return "redirect:/" + triangleType.toLowerCase().replace(" ", "-");
2727
}

jdk_21_maven/cs/web/triangle-ssr/src/main/resources/templates/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html xmlns:th="http://www.thymeleaf.org">
2+
<html lang="en">
33
<head>
44
<title>SUT</title>
55
</head>
@@ -9,13 +9,13 @@ <h2>Triangle classification</h2>
99
<h2>Enter side values:</h2>
1010
<form action="/process" method="post">
1111
<label for="a">Side A:</label>
12-
<input type="number" id="a" name="a" required><br><br>
12+
<input type="text" id="a" name="a" required><br><br>
1313

1414
<label for="b">Side B:</label>
15-
<input type="number" id="b" name="b" required><br><br>
15+
<input type="text" id="b" name="b" required><br><br>
1616

1717
<label for="c">Side C:</label>
18-
<input type="number" id="c" name="c" required><br><br>
18+
<input type="text" id="c" name="c" required><br><br>
1919

2020
<button type="submit">Submit</button>
2121
</form>

0 commit comments

Comments
 (0)