Ich habe versucht, mit Java und Spring eine Funktion / einen Bildschirm für den Administrator einer Einkaufsseite zu erstellen

Einführung

Ich habe Java für ein paar Jahre studiert. Vor kurzem (wenn auch spät) habe ich angefangen, Frühling zu studieren. Ich habe sofort versucht, eine Funktion / einen Bildschirm für den Administrator der Einkaufsseite zu erstellen. Ich bin immer noch arm, aber bitte beziehen Sie sich darauf, wenn Sie möchten.

STS-Version: 4.7.0.RELEASE MySQL-Version: 8.0.15 mysql-connector-java version: 8.0.16

SampleApplication.java


package com.example.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@SpringBootApplication
public class Sample1Application {
	@RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
		
		return "index";
    }
    
	public static void main(String[] args) {
		SpringApplication.run(Sample1Application.class, args);
	}
}

SampleController.java


package com.example.demo;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;


@Controller
public class SampleController {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private SampleService sampleService;
	
	@ModelAttribute
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model) {
		LoginForm loginForm = new LoginForm();
		loginForm.setId("");
		loginForm.setName("");
        model.addAttribute("loginForm", loginForm);
        model.addAttribute("message", "Bitte geben Sie die Administratorinformationen ein");
        
        return "login";
    }

	@ModelAttribute
	@RequestMapping(value = "/login_result", method = RequestMethod.POST)
	public String login_result(LoginForm loginForm, Model model) {
		       List<Map<String, Object>> list;
		       list = jdbcTemplate.queryForList("select * from user");
		       
               for(int i = 0; i < list.size(); i++) {
            	   if (("[" + loginForm.getId() + "," + " " + loginForm.getName() + "]").compareTo((list.get(i).values().toString())) == 0) {
            	        model.addAttribute("message", "Sie haben sich erfolgreich angemeldet");
                   }
               
                   else {
            	         model.addAttribute("message", "Ich konnte mich nicht anmelden");
                   }
               }

	    	return "login_result";
	}
	
    @ModelAttribute
    @RequestMapping(value = "/admin_menu", method = RequestMethod.GET)
    public String admin_menu(Model model) {
	  model.addAttribute("message", "Bitte wählen Sie ein Menü");

    return "admin_menu";
    }
    
    @ModelAttribute
    @RequestMapping(value = "/show_product", method = RequestMethod.GET)
    public String show_product(Model model) {
    	List<String> products = null;
    	products = sampleService.selectAll();
    	model.addAttribute("products",products);
	    model.addAttribute("message", "Produkt angezeigt");

    return "show_product";
    }
    
    @ModelAttribute
    @RequestMapping(value = "/register_product", method = RequestMethod.GET)
    public String register_product(Model model) {
    	ProductForm productForm = new ProductForm();
    	model.addAttribute("productForm", productForm);
	    model.addAttribute("message", "Bitte registrieren Sie das Produkt");

    return "register_product";
    }

	@ModelAttribute
    @RequestMapping(value = "/update_product", method = RequestMethod.GET)
    public String update_product(Model model) {
      ProductForm productForm = new ProductForm();
      model.addAttribute("productForm", productForm);
	  model.addAttribute("message", "Bitte aktualisieren Sie das Produkt");

    return "update_product";
    }
	
    @ModelAttribute
    @RequestMapping(value = "/delete_product", method = RequestMethod.GET)
    public String delete_product(Model model) {
      ProductForm productForm = new ProductForm();
      model.addAttribute("productForm", productForm);    	
	  model.addAttribute("message", "Bitte löschen Sie den Artikel");

    return "delete_product";
    }
    
    @ModelAttribute
    @RequestMapping(value = "/afeter_delete_product", method = RequestMethod.POST)
    public String afeter_delete_product(ProductForm productForm, Model model) {
      sampleService.delete(productForm);	
	  model.addAttribute("message", "Die Verarbeitung ist abgeschlossen");

    return "afeter_delete_product";
    }
    
    @ModelAttribute
    @RequestMapping(value = "/show_result", method = RequestMethod.POST)
    public String show_result(ProductForm productForm, Model model) {
    	sampleService.insert(productForm);
	    model.addAttribute("message", "Die Verarbeitung ist abgeschlossen");

    return "show_result";
    }
}

SampleService.java


package com.example.demo;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;


@Service
public class SampleService {  
  public List<String> selectAll() {
	  List<String> entities = null;
	  entities = new ArrayList<String>();
	  ResultSet resultSet = null;
	  Connection connection = null;
	  
	  try {
		  connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample?serverTimezone=JST", "root", "root");
          Statement statement = connection.createStatement();
          resultSet = statement.executeQuery("select * from product");

	  while (resultSet.next()) {
		  String str = resultSet.getString("code") + "  " + resultSet.getString("name") + "  " + resultSet.getString("description") + "  " + resultSet.getString("price") + "  " + resultSet.getString("evaluation");
		  entities.add(str);
	  }

		if (connection != null) {
			connection.close();
		}

	  } catch (SQLException e) {
          e.printStackTrace();
	  }
	  
      return entities;
   }
  
  public void insert(ProductForm productForm) {
	  Connection connection = null;
	  
	  try {
		  connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample?serverTimezone=JST", "root", "root");
		  PreparedStatement statement = connection.prepareStatement("INSERT INTO product VALUES (?, ?, ?, ?, ?)");

		  statement.setString(1, productForm.getCode());
          statement.setString(2, productForm.getName());
          statement.setString(3, productForm.getDescription());
          statement.setString(4, productForm.getPrice());
          statement.setString(5, productForm.getEvaluation());
          
          connection.setAutoCommit(true);
          statement.execute();

		if (connection != null) {
			connection.close();
		}

	  } catch (SQLException e) {
          e.printStackTrace();
	  }
   }

  public void update(ProductForm productForm) {
	  Connection connection = null;
	  
	  try {
		  connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample?serverTimezone=JST", "root", "root");
		  PreparedStatement statement = connection.prepareStatement("UPDATE product SET code=?, name=?, description=?, price=?, evaluation=? WHERE code=?");

		  statement.setString(1, productForm.getCode());
          statement.setString(2, productForm.getName());
          statement.setString(3, productForm.getDescription());
          statement.setString(4, productForm.getPrice());
          statement.setString(5, productForm.getEvaluation());
          statement.setString(6, productForm.getCode());

          connection.setAutoCommit(true);
          statement.execute();

		if (connection != null) {
			connection.close();
		}

	  } catch (SQLException e) {
          e.printStackTrace();
	  }
   }
  
  public void delete(ProductForm productForm) {
	  Connection connection = null;
	  
	  try {
		  connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample?serverTimezone=JST", "root", "root");
		  PreparedStatement statement = connection.prepareStatement("DELETE FROM product WHERE code=?");

          statement.setString(1, productForm.getCode());

          connection.setAutoCommit(true);
          statement.execute();
          
		if (connection != null) {
			connection.close();
		}

	  } catch (SQLException e) {
          e.printStackTrace();
	  }
   }
}

LoginForm.java


package com.example.demo;


public class LoginForm {
    private String id;
    private String name;
    
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

ProductForm.java


package com.example.demo;


public class ProductForm {
    private String code;
    private String name;
    private String description;
    private String price;
    private String evaluation;
    
	public String getCode() {
		return code;
	}
	
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public String getPrice() {
		return price;
	}
	
	public void setPrice(String price) {
		this.price = price;
	}

	public String getEvaluation() {
		return evaluation;
	}
	
	public void setEvaluation(String evaluation) {
		this.evaluation = evaluation;
	}	
}

Speichern Sie die obigen Quelldateien in src / main / java des Projekts.

index.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring-Shopsite</title>
</head>
<body>
<p>Spring-Willkommen bei Shopsite!</p>
<a href="/login" >Wenn Sie Administrator sind, klicken Sie bitte hier</a>
</body>
</html>

login.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring-Shopsite</title>
</head>
<body>
<p th:text="${message}"></p>
<form th:action="@{/login_result}" th:object="${loginForm}" method="post">  
ID:<input type="text" th:field="*{id}">
<BR/>
Name:<input type="text" th:field="*{name}"> 
<BR/>
<input type="submit" value="Einloggen"/>
<BR/>
</form>
</body>
</html>

login_result.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring-Shopsite</title>
</head>
<body>
<p th:text="${message}"></p>
<div th:if="${#strings.equals(message, 'Sie haben sich erfolgreich angemeldet')}">
<a href="/admin_menu">Administratormenü</a>
</div>
<div th:if="${#strings.equals(message, 'Ich konnte mich nicht anmelden')}">
<a href="/login">Bitte melden Sie sich erneut an</a>
</div>
</body>
</html>

admin_menu.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring-Shopsite</title>
</head>
<body>
<p th:text="${message}"></p>
<a href="/show_product">Anzeige aller registrierten Produkte</a>
<br/>
<a href="/register_product">Produktregistrierung</a>
<br/>
<a href="/update_product">Produktaktualisierung</a>
<br/>
<a href="/delete_product">Produkt löschen</a>
</body>
</html>

show_product.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring-Shopsite</title>
</head>
<body>
<p th:text="${message}"></p>
<table th:each="product : ${products}">
<tr>
<td>
<p th:text="${product}"></p>
</td>
</tr>
</table>
</body>
</html>

register_product.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring-Shopsite</title>
</head>
<body>
<p th:text="${message}"></p>
<form th:action="@{/show_result}" th:object="${productForm}" method="post">
Produktcode:<input type="text" th:field="*{code}">
<BR />
Produktname:<input type="text" th:field="*{name}">
<BR />
Erklärung:<input type="text" th:field="*{description}">
<BR />
Preis:<input type="text" th:field="*{price}">
<BR />
Bewertung:<input type="text" th:field="*{evaluation}">
<BR />
<input type="submit" value="Anmeldung"/>
<BR />
</form>
</body>
</html>

update_product.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring-Shopsite</title>
</head>
<body>
<p th:text="${message}"></p>
<form th:action="@{/show_result}" th:object="${productForm}" method="post">
Produktcode:<input type="text" th:field="*{code}">
<BR />
Produktname:<input type="text" th:field="*{name}">
<BR />
Erklärung:<input type="text" th:field="*{description}">
<BR />
Preis:<input type="text" th:field="*{price}">
<BR />
Bewertung:<input type="text" th:field="*{evaluation}">
<BR />
<input type="submit" value="aktualisieren"/>
<BR />
</form>
</body>
</html>

delete_product.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring-Shopsite</title>
</head>
<body>
<p th:text="${message}"></p>
<form th:action="@{/afeter_delete_product}" th:object="${productForm}" method="post">
Produktcode:<input type="text" th:field="*{code}">
<BR />
<input type="submit" value="Löschen"/>
<BR />
</form>
</body>
</html>

after_delete_product.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring-Shopsite</title>
</head>
<body>
<p th:text="${message}"></p>
</body>
</html>

show_result.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring-Shopsite</title>
</head>
<body>
<p th:text="${message}"></p>
</body>
</html>

Speichern Sie die obigen Vorlagendateien in Vorlagen unter src / main / resources.

abschließend

Erstellen Sie danach das Projekt und es sollte normal ausgeführt werden. Von dieser Zeit Leider verarbeitet das Projekt keine Produktbilder, aber ich beabsichtige, nur die Grundfunktionen beizubehalten. Vielleicht werden wir dieses Projekt fortsetzen, aber nochmals vielen Dank zu diesem Zeitpunkt. Wir sehen uns wieder.

Apropos

Ich werde auch pom nur für den Fall posten.

pom.xml


<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>Sample-1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Sample-1</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
    
</project>

Recommended Posts

Ich habe versucht, mit Java und Spring eine Funktion / einen Bildschirm für den Administrator einer Einkaufsseite zu erstellen
Ich habe versucht, mit Chocolatey eine Java8-Entwicklungsumgebung zu erstellen
Ich möchte eine Funktion mit Kotlin und Java erstellen!
Ich habe versucht, den Block mit Java zu brechen (1)
Ich habe versucht, eine Clova-Fähigkeit in Java zu erstellen
Ich habe versucht, eine Anmeldefunktion mit Java zu erstellen
Ich habe versucht, eine Java EE-Anwendung mit OpenShift zu modernisieren.
[Rails] Ich habe versucht, eine Mini-App mit FullCalendar zu erstellen
Ich möchte eine Liste mit Kotlin und Java erstellen!
Ich habe versucht, mit Java zu interagieren
[Azure] Ich habe versucht, eine kostenlose Java-App zu erstellen ~ Mit FTP verbinden ~ [Anfänger]
Ich habe versucht, mit Docker eine Padrino-Entwicklungsumgebung zu erstellen
Lassen Sie uns eine TODO-Anwendung mit Java 2 erstellen. Ich möchte eine Vorlage mit Spring Initializr erstellen und eine Hello-Welt erstellen
Neuer Mitarbeiter hat versucht, mit Spring Security eine Authentifizierungs- / Autorisierungsfunktion von Grund auf neu zu erstellen
Ich habe versucht, eine Nachrichtenfunktion für die Erweiterung Rails Tutorial (Teil 2) zu erstellen: Erstellen Sie einen Bildschirm zum Anzeigen
Ich habe versucht, ein Formular mit Spring MVC und Jasper Reports 1/3 (Jasper Reports-Einstellungen) zu drucken.
Ich habe versucht, ein Formular mit Spring MVC und Jasper Reports 3/3 (Spring MVC-Steuerung) zu drucken.
Ich möchte mit Kotlin und Java zum vorherigen Bildschirm zurückkehren!
Ich habe versucht, eine Spring MVC-Entwicklungsumgebung auf einem Mac zu erstellen
Ich habe versucht, mit Rails eine Gruppenfunktion (Bulletin Board) zu erstellen
Erstellen Sie eine einfache CRUD mit SpringBoot + JPA + Thymeleaf ② ~ Bildschirm- und Funktionserstellung ~
Ich habe versucht, ein Formular mit Spring MVC und Jasper Reports 2/3 (Formularvorlagenerstellung) zu drucken.
Ich möchte Bilder mit REST Controller von Java und Spring anzeigen!
Ich möchte mit Jakarta EE 8 mit Java 11 ein dunkles Web-SNS erstellen
Server mit Spring Gradle ausführbar So erstellen Sie JAR und WAR
Ich habe eine Lambda-Funktion in Java geschrieben und mit SAM bereitgestellt
Ich habe versucht, eine Standardauthentifizierung mit Java durchzuführen
Java Ich habe versucht, einen einfachen Block zu brechen
Ich habe versucht, eine LINE-Klon-App zu erstellen
Ich habe versucht, JavaFX und Spring Framework zu verknüpfen.
Ich habe versucht, Alexa-Fähigkeiten mit Java zu erstellen
Ich habe versucht, ein Portfolio mit AWS, Docker, CircleCI, Laravel [mit Referenzlink] zu erstellen.
Ich habe versucht, mit HCE-F von Android eine Funktion zu implementieren, die Felica Lite entspricht
[Java] Ich habe versucht, über den Verbindungspool eine Verbindung mit Servlet (Tomcat) & MySQL & Java herzustellen
Ich habe versucht, ein Formular mit Spring MVC und Jasper Reports Extra Edition (Variables Edition) zu drucken.
Ich habe versucht, ein Formular mit Spring MVC und Jasper Reports Extra Edition (Bildausgabe) zu drucken.
[Android] Ich habe mit ListView + Bottom Sheet einen Materiallistenbildschirm erstellt
Ich habe versucht, eine Webanwendung voller Fehler mit Spring Boot zu klonen
[Azure] Ich habe versucht, eine Java-App für die Erstellung von kostenlosen Web-Apps zu erstellen. [Anfänger]
Ich habe versucht, mit der Java Stream-API eine Methode zu erstellen, die mehrere Filter gleichzeitig anwendet. Ist das in Ordnung?
[Java] Ich habe JDBC installiert und versucht, eine Verbindung mit Servlet + MySQL herzustellen. (Es gibt eine Version mit DAO / Bean)
Ich habe versucht, das Hochladen von Dateien mit Spring MVC zu implementieren
Ich habe versucht, CSV mit Outsystems zu lesen und auszugeben
Ich habe versucht, TCP / IP + BIO mit JAVA zu implementieren
[Java 11] Ich habe versucht, Java auszuführen, ohne mit Javac zu kompilieren
Erstellen einer EC-Site mit Rails 5 ⑨ ~ Erstellen einer Warenkorbfunktion ~
Ich habe MySQL 5.7 mit Docker-Compose gestartet und versucht, eine Verbindung herzustellen
Ich habe versucht, mit Spring Data JPA zu beginnen
Ich habe versucht, mit OCR eine PDF-Datei mit Java zu verarbeiten
Ich habe versucht, Sterling Sort mit Java Collector zu implementieren
Erstellen Sie mit Spring Security 2.1 eine einfache Demo-Site mit Spring Security
Ich möchte Bildschirmübergänge mit Kotlin und Java machen!
Ich habe versucht, mit Wercker ein Docker-Image zu erstellen und zu veröffentlichen, mit dem GlassFish 5 gestartet wird
Ich habe versucht, eine Nachrichtenfunktion der Rails Tutorial-Erweiterung (Teil 1) zu erstellen: Erstellen Sie ein Modell
Ich habe eine Funktion zum Registrieren von Bildern bei der API in Spring Framework erstellt. Teil 1 (API Edition)
Java zum Spielen mit Function
Ich habe versucht, die Grundlagen von Kotlin und Java zusammenzufassen
Ich habe versucht, dies und das von Spring @ Transactional zu überprüfen
Ich habe versucht, Java Optional und Guard-Klausel koexistieren zu lassen