Skip to content

Main #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Main #183

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ru.alishev.springcourse.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
* @author Neil Alishev
Expand All @@ -11,11 +13,34 @@
@RequestMapping("/first")
public class FirstController {


@GetMapping("/hello")
public String helloPage() {
public String helloPage(@RequestParam(value="a", required = false) int a,
@RequestParam(value="b", required = false) int b,
@RequestParam(value="action", required = false) String action,
Model model) {
model.addAttribute("someMessage","result "+ a + action + b +" = " + calculate(a,b,action));

//int sum = calculate(a,b,action);

//System.out.println("Hello, " + name + " " + surname);
return "first/hello";
}

public int calculate(int a, int b, String action) {
switch (action) {
case "multiplication":
return a * b;
case "addition":
return a+b;
case "substruction":
return a%b;
case "division":
return a-b;
default: return 0;
}
}

@GetMapping("/goodbye")
public String goodByePage() {
return "first/goodbye";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
Goodbye!

<a href="/first/hello">Say hello</a> or <a href="/exit">Exit</a>
or <a href ="/first/hello?name=Pidor&surname=Chertov">Request with anower parameters!!OMG</a>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Hello</title>
Expand All @@ -8,5 +8,10 @@
Hello!

<a href="/first/goodbye">Say goodbye</a> or <a href="/exit">Exit</a>
or <a href ="/first/hello?a=4&b=2&action=addition">Request with parameters</a>

<p th:text="${someMessage}" />


</body>
</html>
20 changes: 13 additions & 7 deletions Lesson20_Starter.IntroToModel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>

<spring.version>5.2.1.RELEASE</spring.version>
<spring.version>6.0.4</spring.version>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
Expand Down Expand Up @@ -59,12 +61,16 @@
<version>3.0.11.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>



</dependencies>

<build>
Expand Down
24 changes: 12 additions & 12 deletions Lesson21.CRUD_App1/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<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">
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>ru.alishev.springcourse</groupId>
Expand Down Expand Up @@ -102,15 +102,15 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package ru.alishev.springcourse.config;

import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

/**
* @author Neil Alishev
*/
Expand All @@ -20,4 +24,15 @@ protected Class<?>[] getServletConfigClasses() {
protected String[] getServletMappings() {
return new String[]{"/"};
}

@Override
public void onStartup(ServletContext aServletContext) throws ServletException {
super.onStartup(aServletContext);
registerHiddenFieldFilter(aServletContext);
}

private void registerHiddenFieldFilter(ServletContext aContext) {
aContext.addFilter("hiddenHttpMethodFilter",
new HiddenHttpMethodFilter()).addMappingForUrlPatterns(null ,true, "/*");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import ru.alishev.springcourse.dao.PersonDAO;
import ru.alishev.springcourse.models.Person;

/**
* @author Neil Alishev
Expand All @@ -33,4 +32,32 @@ public String show(@PathVariable("id") int id, Model model) {
model.addAttribute("person", personDAO.show(id));
return "people/show";
}

@GetMapping("/new")
public String newPerson (@ModelAttribute("person") Person person) {
return "people/new";

}
@PostMapping()
public String create(@ModelAttribute("person") Person person) {
personDAO.save(person);
return "redirect:/people";
}
@GetMapping("/{id}/edit")
public String edit (Model model,@PathVariable("id") int id) {
model.addAttribute("person",personDAO.show(id));
return "people/edit";
}

@PatchMapping("/{id}")
public String update (@ModelAttribute("person") Person person, @PathVariable("id") int id) {
personDAO.update(id,person);
return "redirect:/people";
}

@DeleteMapping("/{id}")
public String delete (@PathVariable("id") int id) {
personDAO.delete(id);
return "redirect:/people";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,19 @@ public List<Person> index() {
public Person show(int id) {
return people.stream().filter(person -> person.getId() == id).findAny().orElse(null);
}

public void save(Person person) {
person.setId(++PEOPLE_COUNT);
people.add(person);
}

public void update (int id, Person updatePerson) {
Person personToBeUpdated = show(id);

personToBeUpdated.setName(updatePerson.getName());
}

public void delete (int id) {
people.removeIf(p -> p.getId()==id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ public class Person {
private int id;
private String name;

public Person () {

}

public Person(int id, String name) {
this.id = id;
this.name = name;
Expand Down
17 changes: 17 additions & 0 deletions Lesson21.CRUD_App1/src/main/webapp/WEB-INF/views/people/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Update person</title>
</head>
<body>

<form th:method="Patch" th:action="@{/people/{id}(id=${person.getId()})}" th:object="${person}">
<label for="name">Enter name: </label>
<input type="text" th:field="*{name}" id="name"/>
<br/>
<input type="submit" value="Update!"/>
</form>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Все люди</title>
<title>All people</title>
</head>
<body>

<div th:each="person : ${people}">
<a th:href="@{/people/{id}(id=${person.getId()})}" th:text="${person.getName()}">user</a>
</div>

<br/>
<hr/>

<a href="/people/new">Create new person</a>

</body>
</html>
18 changes: 18 additions & 0 deletions Lesson21.CRUD_App1/src/main/webapp/WEB-INF/views/people/new.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>New person</title>
</head>
<body>

<form th:method="POST" th:action="@{/people}" th:object="${person}">
<label for="name">Enter name: </label>
<input type="text" th:field="*{name}" id="name"/>
<br/>
<input type="submit" value="Create!"/>

</form>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Человек</title>
<title>Person</title>
</head>
<body>
<p th:text="${person.getName()}">VALUE</p>
<p th:text="${person.getId()}">VALUE</p>

<a th:href="@{/people/{id}/edit(id=${person.getId()})}">Edit</a>

<form th:method="DELETE" th:action="@{/people/{id}(id=${person.getId()})}">
<input type="submit" value="Delete"/>
</form>
</body>
</html>
15 changes: 15 additions & 0 deletions Lesson24_Starter.SpringFormsValidation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.6.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.4</version>
</dependency>


</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.alishev.springcourse.dao.PersonDAO;
import ru.alishev.springcourse.models.Person;

import javax.validation.Valid;

/**
* @author Neil Alishev
*/
Expand Down Expand Up @@ -39,7 +42,10 @@ public String newPerson(@ModelAttribute("person") Person person) {
}

@PostMapping()
public String create(@ModelAttribute("person") Person person) {
public String create(@ModelAttribute("person") @Valid Person person,
BindingResult bindingResult) {
if (bindingResult.hasErrors())
return "people/new";
personDAO.save(person);
return "redirect:/people";
}
Expand All @@ -51,7 +57,11 @@ public String edit(Model model, @PathVariable("id") int id) {
}

@PatchMapping("/{id}")
public String update(@ModelAttribute("person") Person person, @PathVariable("id") int id) {
public String update(@ModelAttribute("person") @Valid Person person, BindingResult bindingResult,
@PathVariable("id") int id) {
if ( bindingResult.hasErrors())
return "people/edit";

personDAO.update(id, person);
return "redirect:/people";
}
Expand Down
Loading