【发布时间】:2018-07-15 00:26:12
【问题描述】:
我正在尝试从 url 查询字符串中获取 contentid 值。
例如:http://localhost:8080/app?contentid=10
我想在我的spring控制器中从上面的url中获取contentid的值为:10,我该如何获取呢?
请注意,如果我在那里传递任何值(如 10、50,100、200 等 - 我可以输入或传递任何数字到 url),所以无论我传递给 contentid,那值应该进入我的控制器(System.out.println(contentid);)。
目前点击提交按钮后出现以下错误:
http://localhost:8080/app?contentid=
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='testData'. Error count: 1
我在我的 STS 控制台中低于。
2018-02-05 13:09:45.186 INFO 8428 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-02-05 13:09:45.187 INFO 8428 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-02-05 13:09:45.231 INFO 8428 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 44 ms
控制器:
package com.example.demo;
import java.util.concurrent.ThreadLocalRandom;
import javax.servlet.http.HttpServletRequest;
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.bind.annotation.RequestParam;
@Controller
public class MyController {
@GetMapping("/app")
public String getApp(Model model) {
return "app";
}
@PostMapping("/app")
public String postApp(@RequestParam(required=false) Integer contentid, @ModelAttribute TestData testData, Model model, HttpServletRequest request) {
System.out.println(contentid);
if(contentid == null) {
contentid = ThreadLocalRandom.current().nextInt(0, 100 + 1);
}
model.addAttribute("contentid",contentid);
return "app";
}
}
TestData.java:
package com.example.demo;
import java.io.Serializable;
public class TestData implements Serializable {
private static final long serialVersionUID = 1L;
private String firstname;
private String secondname;
private int contentid;
public TestData() {
}
//setters and getters
public TestData(String firstname, String secondname, int contentid) {
this.firstname = firstname;
this.secondname = secondname;
this.contentid = contentid;
}
@Override
public String toString() {
return String.format("TestData[firstname=%s, secondname=%s, contentid=%d]", firstname, secondname, contentid);
}
}
app.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<title></title>
</head>
<body>
<th:block th:if="${contentid != null}">
<div th:text="${'contentId: ' + contentid}"></div>
</th:block>
<form action="#" th:action="@{/app(contentid=${contentid})}" th:object="${TestData}"
method="post">
<div class="form-group">
<label for="firstname">First Name: </label>
<input type="text" class="form-control" id="firstname"
name="firstname" />
</div>
<div class="form-group">
<label for="secondname">Second Name:</label>
<input type="text" class="form-control" id="secondname"
name="secondname" />
</div>
<button type="submit" value="Submit">Submit</button>
</form>
</body>
</html>
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>MySpringBootApp-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MySpringBootApp-2</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
-
请在此处发布详细的堆栈跟踪
-
@dsharew,我发布了我在 STS 控制台中获得的堆栈跟踪。
-
你想传递一个数组?我的意思是像 contentid= 10,20,30 ?或者你只会传递一个值。为什么你的网址里多了一个逗号?
-
@pvpkiran,我想传递一个值,例如:localhost:8080/app?contentid=10 或 localhost:8080/app?contentid=20 等(contentid 可以是任何整数值)。网址中没有逗号
-
错误消息显示,testData 验证失败。您在发布请求中传递了什么作为正文?你怎么称呼这个网址localhost:8080/app?contentid=10?从浏览器?
标签: java spring-mvc spring-boot thymeleaf query-string