为什么Spring返回String而不是HTML视图

c90pui9n  于 2023-04-28  发布在  Spring
关注(0)|答案(1)|浏览(108)

我刚刚学习Spring框架,最近发现了一个简单的应用程序,试图与mysql数据库连接。Here is a screen capture
我的代码也在这里:
Actor.java:

package com.dbtest.dbTest;

import jakarta.persistence.Entity;

import jakarta.persistence.Id;

@Entity

public class Actor {
    
    @Id
    private Integer actor_id;
    private String first_name;
    private String last_name;
    
    private String last_update;
    
    public Actor(Integer actor_id, String first_name, String last_name, String last_update) {
        this.actor_id = actor_id;
        this.first_name = first_name;
        this.last_name = last_name;
        this.last_update = last_update;
    }
    
    public Actor() {}
    
    public Integer getActor_id() {
        return actor_id;
    }
    public void setActor_id(Integer actor_id) {
        this.actor_id = actor_id;
    }
    public String getFirst_name() {
        return first_name;
    }
    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }
    public String getLast_name() {
        return last_name;
    }
    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }
    public String getLast_update() {
        return last_update;
    }
    public void setLast_update(String last_update) {
        this.last_update = last_update;
    }
    
}

ActorRepository.java:

package com.dbtest.dbTest;

import org.springframework.data.repository.CrudRepository;

//This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
//CRUD refers Create, Read, Update, Delete

public interface ActorRepository extends CrudRepository<Actor, Integer> {
}

MainController.java:

package com.dbtest.dbTest;

import java.util.Date;

import java.text.SimpleDateFormat;  

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path="/actor")

public class MainController {
    @Autowired
    private ActorRepository actorRepository;
    
    @PostMapping(path="/add") 
    public @ResponseBody String addNewActor(@RequestParam String first_name, @RequestParam String last_name) {
        Actor a = new Actor();
        a.setActor_id(this.getMaxId()+1);
        a.setFirst_name(first_name);
        a.setLast_name(last_name);
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String formattedDate = sdf.format(date);
        a.setLast_update(formattedDate);
        actorRepository.save(a);
        return "OK!";
    }
    
    @PostMapping(path="/delete")
    public @ResponseBody String deleteActorById(@RequestParam int id) {
        for(Actor a : this.actorRepository.findAll()) {
            if(a.getActor_id() == id) {
                this.actorRepository.deleteById(id);
                return "DELETED!";
            }
        }
        return "USER NOT FOUND "+id;
    }
    
    public int getMaxId() {
        int max = 0;
        for(Actor a :actorRepository.findAll()) {
            if(a.getActor_id() > max) {
                max = a.getActor_id();
            }
        }
        return max;
    }
    
    @GetMapping(path="/all")
    public String showActor(Model model) {
        model.addAttribute("actors", this.actorRepository.findAll());
        return "actor";
    }
}

DbTestApplication.java:

package com.dbtest.dbTest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DbTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(DbTestApplication.class, args);
    }

}

actor.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="Listado de actores"></title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Nombre</th>
                <th>Apellido</th>
                <th>Última actualización</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="actor : ${actors}">
                <td th:each="${actor.actor_id}"></td>
                <td th:each="${actor.first_name}"></td>
                <td th:each="${actor.last_name}"></td>
                <td th:each="${actor.last_update}"></td>
            </tr>
        </tbody>
    </table>
</body>
</html>

application.properties:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/sakila
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
logging.level.root=INFO
logging.file.name=spring.log

Directory hierarchy here
提前感谢:)

h43kikqp

h43kikqp1#

@Controller用于返回视图,@RestController用于返回JSON响应。如果你想使用百里香叶和MVC项目,你应该使用@Controller
只要记住,你需要返回一个视图在其他地方,而不是return "OK!";,因为这可能不是一个HTML页面的名称,你有。

相关问题