我在学习 Java
和 Spring
. 我正在做一个小的链接缩短项目(如www.google.com 例如gg等)。实现添加链接和显示它们。但是在显示之后,这些链接是不可点击的。我试着用 hyperlink
,但最终一无所获。如何从数据库建立链接( MySQL
)可点击?没有指定构造函数、getter和setter的存储库和类。
这是我的密码
@Controller
public class MainController {
DB db = new DB();
@Autowired
private LinkRepository linkRepository;
@GetMapping("/")
public String link(Model model) {
Iterable<Links> links = linkRepository.findAll();
model.addAttribute("title", "Page with links");
model.addAttribute("link", links);
return "link";
}
@GetMapping("/uslugi")
public String uslugi(Model model) {
model.addAttribute("title", "Page with services");
return "uslugi";
}
@PostMapping("/-add")
public String linkAdd(@RequestParam String long_link, @RequestParam String short_link, Model model) {
Links links = new Links(short_link, long_link);
linkRepository.save(links);
return "redirect:/";
}
}
分贝
import java.sql.*;
public class DB {
private final String HOST = "localhost";
private final String PORT = "3306";
private final String DB_NAME = "spring";
private final String LOGIN = "root";
private final String PASS = "root";
private Connection dbConn = null;
private Connection getDbConnection() throws ClassNotFoundException, SQLException {
String connStr = "jdbc:mysql://" + HOST + ":" + PORT + "/" + DB_NAME +
"?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
Class.forName("com.mysql.cj.jdbc.Driver");
dbConn = DriverManager.getConnection(connStr, LOGIN, PASS);
return dbConn;
}
public void isConnected() throws SQLException, ClassNotFoundException {
dbConn = getDbConnection();
System.out.println(dbConn.isValid(1000));
}
}
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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.java10</groupId>
<artifactId>serving-web-content</artifactId>
<version>1.0-SNAPSHOT</version>
<name>java10</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>10</source><target>10</target></configuration></plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
html,如果需要
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="main.css">
</head>
<header class="d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-white border-bottom shadow-sm">
<p class="h5 my-0 me-md-auto fw-normal">Java</p>
<nav class="my-2 my-md-0 me-md-3">
<a class="p-2 text-dark" href="/">Главная</a>
<a class="p-2 text-dark" href="/uslugi">Про нас</a>
</nav>
</header>
<body>
<div class="container mt-5">
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
<h3 class="mt-2">Все отзывы</h3>
<div th:each="row : ${link}">
<h4 class="mt-2" th:text="${row.long_link}"/>
<h4 th:text="${row.short_link}"/>
</div>
<h1 class="mt-5" th:text="${title}"/>
<form th:action="@{/-add}" method="post">
<input type="text" name="long_link"
placeholder="Введите ссылку" class="form-control"><br>
<input type="text" name="short_link"
placeholder="Введите короткую ссылку" class="form-control"><br>
<button type="submit" class="btn btn-success">Добавить</button>
</form>
</div>
</body>
<footer class="pt-4 my-md-5 pt-md-5 border-top">
<div class="row">
<div class="col-12 col-md">
<small class="d-block mb-3 text-muted">©2021 Все права защищены</small>
</div>
</div>
</footer>
</html>
1条答案
按热度按时间k10s72fa1#
实际上,你唯一需要修改的东西就是thymeleaf模板。
你得换新的
h4
带有a
(锚元素),类似于:您可以在这里阅读更多关于如何使用thymeleaf创建不同类型的html元素的信息。