gpt4 book ai didi

java - PostMapping Spring 启动注释不起作用

转载 作者:行者123 更新时间:2023-12-02 08:45:27 25 4
gpt4 key购买 nike

我是 Spring Boot 新手,不知道为什么我的 @PostMapping 不起作用并导致白标签错误。其他一切工作正常,我可以将客户添加到 h2 数据库。但是,一旦我添加客户并转到 URL localhost:8084/getdetails 并输入 1,就会出现白标签错误。但是,ViewCustomers.jsp 文件中的表单仍会显示,但无法接受任何输入。该表单应调用客户对象及其字段的 toString()。

Controller

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class FormController {
@Autowired
CustomerRepo repo;

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

@RequestMapping("/details")
public String details(Customers customers) {
repo.save(customers);
return "edureka";
}

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

@PostMapping("/getdetails")
public ModelAndView getdetails(@RequestParam int cid) {
System.out.println("here");
ModelAndView mv = new ModelAndView("Retrieve");
Customers customers = repo.findById(cid).orElse(null);
mv.addObject(customers);
return mv;
}
}

edureka.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edureka Customers</title>
</head>
<body>
<form method="post" action="details">
Enter Customer ID: <input type="text" name="cid"><br><br>
Enter Customer Name: <input type="text" name="cname"><br><br>
Enter Customer Email Address: <input type="email" name="cemail"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

ViewCustomer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>View Customer Details</h1>
<h2>Details as submitted as follows</h2>
<form method="getdetails" action="post">
<input type="number" name="cid"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

检索.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Retrieve Customer Details</h1>
<h2>Details as submitted as follows:</h2>
<h5>${customers}</h5>
</body>
</html>

客户.java

package com.example.demo;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Customers {
@Id
private int cid;
private String cname;
private String cemail;

public int getCid() {
return cid;
}

public void setCid(int cid) {
this.cid = cid;
}

public String getCname() {
return cname;
}

public void setCname(String cname) {
this.cname = cname;
}

public String getCemail() {
return cemail;
}

public void setCemail(String cemail) {
this.cemail = cemail;
}

@Override
public String toString() {
return "Customers [cid=" + cid + ", cname=" + cname + ", cemail=" + cemail + "]";
}
}

CustomerRepo 接口(interface) 包 com.example.demo;

import org.springframework.data.repository.CrudRepository;

public interface CustomerRepo extends CrudRepository<Customers,Integer>{

}

SubmissionFormApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import com.example.demo.SubmissionFormApplication;

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

}

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.1.13.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SubmissionForm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SubmissionForm</name>
<description>First Spring Boot Web app</description>

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

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</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>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.31</version>
</dependency>
</dependencies>

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

</project>

最佳答案

ViewCustomer.jsp 表单的方法应将 method 作为 post 并将 action 作为 get details 。问题出在 html 而不是 spring。

关于java - PostMapping Spring 启动注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61114089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com