@PostMapping是Spring Boot的注解,但不起作用。

huangapple go评论71阅读模式
英文:

PostMapping Spring boot annotation is not working

问题

@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>
<!-- ViewCustomers.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>
<!-- Retrieve.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>
@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 + "]";
  }
}
public interface CustomerRepo extends CrudRepository<Customers, Integer> {
}
@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/>
    </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>
        <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>
英文:

I am new to spring boot and do not know why my @PostMapping is not working and leading to a white label error. Everything else works fine and I can add customers to the h2 database. However, once I add customers and go to URL localhost:8084/getdetails and type 1, a white label error occurs. However, the form in the ViewCustomers.jsp file is still displayed but cannot take in any input. The form should invoke toString() of the customer object and its fields.

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(&quot;/&quot;)
public String details() {
return &quot;edureka&quot;;
}
@RequestMapping(&quot;/details&quot;)
public String details(Customers customers) {
repo.save(customers);
return &quot;edureka&quot;;
}
@RequestMapping(&quot;/getdetails&quot;)
public String getdetails() {
return &quot;ViewCustomers&quot;;
}
@PostMapping(&quot;/getdetails&quot;)
public ModelAndView getdetails(@RequestParam int cid) {
System.out.println(&quot;here&quot;);
ModelAndView mv = new ModelAndView(&quot;Retrieve&quot;);
Customers customers = repo.findById(cid).orElse(null);
mv.addObject(customers);
return mv;
}
}

edureka.jsp

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

ViewCustomer.jsp

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

Retrieve.jsp

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

Customers.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 &quot;Customers [cid=&quot; + cid + &quot;, cname=&quot; + cname + &quot;, cemail=&quot; + cemail + &quot;]&quot;;
}
}

CustomerRepo interface
package com.example.demo;

import org.springframework.data.repository.CrudRepository;
public interface CustomerRepo extends CrudRepository&lt;Customers,Integer&gt;{
}

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.1.13.RELEASE&lt;/version&gt;
&lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.example&lt;/groupId&gt;
&lt;artifactId&gt;SubmissionForm&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;SubmissionForm&lt;/name&gt;
&lt;description&gt;First Spring Boot Web app&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-jersey&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web-services&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.h2database&lt;/groupId&gt;
&lt;artifactId&gt;h2&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-jasper&lt;/artifactId&gt;
&lt;version&gt;9.0.31&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

答案1

得分: 2

ViewCustomer.jsp表单的Method应为method,值为postaction应为get details。问题出在HTML中而不是Spring。

英文:

The ViewCustomer.jsp form's Method should have method as post and action as get details. The problem is in html and not spring.

答案2

得分: 1

几点建议:

  1. 您的Controller可以使用@RestController进行注释,而不仅仅是@Controller

  2. 您的POST映射应使用@PostMapping,为了保持一致,我建议您的GET映射应为@GetMapping,所以我会将所有的@RequestMapping替换为@GetMapping(这样可以消除歧义,也提高了可读性)。

  3. 在您的eureka.jsp表单中,需要更改URL的提交地址。将action=details更改为action=getdetails

  4. 在您的视图viewcustomer.jsp中,表单的动作和提交方式被颠倒了(方法应该是POST,动作应该是getdetails)。

英文:

A few things:

  1. Your Controller can be annotated like @RestController instead of just @Controller.

  2. Your mappings for POST is @PostMapping - For consistency, I would recommend your mapping for GET to be @GetMapping - So I'd replace all @RequestMapping with @GetMapping (this removes ambiguity and improves readability as well)

  3. The form in your eureka.jsp, needs to change the URL is posting to. Instead of action=details it should point to action=getdetails

  4. The form in your view viewcustomer.jsp has the action and the post switched (the method should be POST the action should be getdetails)

答案3

得分: 0

@PostMapping("/getdetails")映射到路径"/getdetails"。这样的映射已经存在。尝试映射到另一个路径以解决此冲突。

英文:

The @PostMapping(&quot;/getdetails&quot;) maps to the path &quot;/getdetails&quot;. Such a mapping already exists. Try to map to another path in order to resolve this conflict.

huangapple
  • 本文由 发表于 2020年4月9日 12:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/61114089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定