JSP 文件无法链接

发布于 2025-01-16 19:16:50 字数 5184 浏览 0 评论 0原文

我在同一文件夹中有 2 个 JSP 文件。 src/main/webapp/WEB-INF/jsp.jsp.jsp

我试图在本地主机上将两个页面链接在一起,它将读取我的索引页面,但不会链接到我的添加库存 jsp。

控制器:


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.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;


import com.project.domain.Vehicle;
import com.project.service.VehicleService;

@Controller
public class VehicleController {
    
    @Autowired
    VehicleService vehicleService;

    @GetMapping("/")
    public String welcome(Model model) {
        return "index";
        
    }
    
    @GetMapping("/add-inventory")
    public ModelAndView addInventory(Model model) {
        return new ModelAndView("add-inventory", "vehicle", new Vehicle());
    }
    
    @PostMapping("/add-inventory")
    public String handleAddInventory(Model model, @ModelAttribute("vehicle") Vehicle vehicle, HttpSession session) {
        vehicleService.saveVehicle(vehicle);
        model.addAttribute("newvehicle", vehicle);
        return "index";
}
}

索引文件:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
Hello World

<a href="/add-inventory">Add Inventory</a> <br>



</body>
</html>

添加库存文件: add-inventory 文件:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Cars</title>
</head>
<body>
Why is this not working

</body>
</html> 

我尝试更改方法以返回这样的字符串,但它也不起作用。

@GetMapping("/add-inventory")
    public String welcome(Model model) {
        return "add-inventory";

依赖项:

<?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.6.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.project</groupId>
    <artifactId>carApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>carApp</name>
    <description>Claim Academy</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- hot swapping, disable cache for template, enable live reload -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>


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

</project>

jsp页面错误图片

I have 2 JSP files in the same folder. src/main/webapp/WEB-INF/jsp.

I'm trying to link the two pages together on a local host, it will read my index page, but will not link to my add-inventory jsp.

Controller:


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.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;


import com.project.domain.Vehicle;
import com.project.service.VehicleService;

@Controller
public class VehicleController {
    
    @Autowired
    VehicleService vehicleService;

    @GetMapping("/")
    public String welcome(Model model) {
        return "index";
        
    }
    
    @GetMapping("/add-inventory")
    public ModelAndView addInventory(Model model) {
        return new ModelAndView("add-inventory", "vehicle", new Vehicle());
    }
    
    @PostMapping("/add-inventory")
    public String handleAddInventory(Model model, @ModelAttribute("vehicle") Vehicle vehicle, HttpSession session) {
        vehicleService.saveVehicle(vehicle);
        model.addAttribute("newvehicle", vehicle);
        return "index";
}
}

index file:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
Hello World

<a href="/add-inventory">Add Inventory</a> <br>



</body>
</html>

add-inventory file:
add-inventory file:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Cars</title>
</head>
<body>
Why is this not working

</body>
</html> 

I've tried changing the method to return a string like this and it does not work either.

@GetMapping("/add-inventory")
    public String welcome(Model model) {
        return "add-inventory";

dependencies:

<?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.6.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.project</groupId>
    <artifactId>carApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>carApp</name>
    <description>Claim Academy</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- hot swapping, disable cache for template, enable live reload -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>


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

</project>

Error image of jsp page

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2025-01-23 19:16:50

解决了!

我必须将此代码添加到应用程序中。但不知道 @Componentscan 和 @Override 方法实际上做了什么。任何解释都会很棒。哈哈

@SpringBootApplication
@ComponentScan(basePackages="com.project")
public class CarAppApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CarAppApplication.class);
    }
    
    public static void main(String[] args) {
        SpringApplication.run(CarAppApplication.class, args);
    }

SOLVED!

I had to add this code to the Application. No clue what the @Componentscan and @Override methods actually do though. Any explanation would be great. Haha

@SpringBootApplication
@ComponentScan(basePackages="com.project")
public class CarAppApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CarAppApplication.class);
    }
    
    public static void main(String[] args) {
        SpringApplication.run(CarAppApplication.class, args);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文