Skip to content
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

TODOs #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
229 changes: 120 additions & 109 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,119 +1,130 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.inventi.academy</groupId>
<artifactId>springweb</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Basic Spring Web</name>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.inventi.academy</groupId>
<artifactId>springweb</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Basic Spring Web</name>

<properties>
<!-- Common -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring.version>4.1.6.RELEASE</spring.version>
<junit.version>4.12</junit.version>
<properties>
<!-- Common -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring.version>4.1.6.RELEASE</spring.version>
<junit.version>4.12</junit.version>

<!-- Web -->
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<servlet.version>3.1.0</servlet.version>
<hibernate.validator.version>5.2.2.Final</hibernate.validator.version>
<!-- Web -->
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<servlet.version>3.1.0</servlet.version>
<hibernate.validator.version>6.2.3.Final</hibernate.validator.version>

<!-- Logging -->
<slf4j.version>1.7.12</slf4j.version>
<logback.version>1.1.3</logback.version>
</properties>
<!-- Logging -->
<slf4j.version>1.7.12</slf4j.version>
<logback.version>1.1.3</logback.version>
</properties>

<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- Other Web dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- Other Web dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>

<!-- Testing -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>springweb-${project.version}</warName>
</configuration>
</plugin>
</plugins>
</build>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>

<!-- Testing -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<warName>springweb-${project.version}</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven3-plugin</artifactId>
<version>1.9.2</version>
<configuration>
<container>
<containerId>tomcat9x</containerId>
<type>embedded</type>
</container>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
package cz.inventi.academy.springweb.controller;

import java.util.Locale;

import javax.validation.Valid;

import cz.inventi.academy.springweb.model.Book;
import cz.inventi.academy.springweb.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cz.inventi.academy.springweb.model.Book;
import javax.validation.Valid;

//TODO: Write annotation for controller
import static org.springframework.context.i18n.LocaleContextHolder.getLocale;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Controller
public class MainController {

// TODO: Define bookService and messageSource parameter and inject it via spring annotation
@Autowired
private BookService bookService;
@Autowired
private MessageSource messageSource;

@RequestMapping("/")
public String index() {
return "index";
}

// TODO: Add @RequestMapping with correct parameters (value, method)
@RequestMapping(value = "/list", method = GET)
public String list(Model model) {
// TODO: Load all books
// TODO: Add books to model
model.addAttribute("books", bookService.loadBooks());
return "list";
}

// TODO: Add @RequestMapping with correct parameters (value, method)
@RequestMapping(value = "/edit", method = GET)
public String formEdit(Model model) {
// TODO: Add new book to model
model.addAttribute("book", new Book());
return "edit";
}

// TODO: Add @RequestMapping with correct parameters (value, method)
public String formSave(//TODO: Use correct types (validate input book)) {
// TODO: When there are no error, then save book and add text message ("info.book.saved") to model from resource bundle
@RequestMapping(value = "/edit", method = POST)
public String formSave(Model model, @Valid @ModelAttribute("book") Book book, BindingResult bindingResult) {
if (!bindingResult.hasErrors()) {
bookService.save(book);
model.addAttribute("info", messageSource.getMessage("info.book.saved", null, getLocale()));
}

return "edit";
}

// TODO: Add @RequestMapping with correct parameters (value, method)
public String delete(//TODO: Use correct types (e.g. PathVariable)) {
// TODO: Delete book by id
// TODO: Load all books
// TODO: Add books to model
@RequestMapping(value = "/delete/{id}", method = GET)
public String delete(Model model, @PathVariable long id) {
bookService.delete(id);
model.addAttribute("books", bookService.loadBooks());
return "redirect:/list";
}
}
6 changes: 3 additions & 3 deletions src/main/java/cz/inventi/academy/springweb/model/Book.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cz.inventi.academy.springweb.model;

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.NotEmpty;

public class Book {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package cz.inventi.academy.springweb.service;

import cz.inventi.academy.springweb.model.Book;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cz.inventi.academy.springweb.model.Book;

//TODO: Write annotation for service
@Service
public class BookServiceImpl implements BookService {

private long ids;

private Map<Long, Book> books = new HashMap<>();
private final Map<Long, Book> books = new HashMap<>();

@Override
public List<Book> loadBooks() {
Expand Down
14 changes: 11 additions & 3 deletions src/main/resources/spring/application-config.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="cz.inventi.academy.springweb"/>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<property name="defaultEncoding" value="UTF-8"/>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martineez ahoj, to kodovani tam mam, mam to odebrat?

<!-- TODO: Define component scan and message source resource bundle (UTF-8) -->
</beans>
Loading