我试图从url querystring获取contentid值。
例如:http://localhost:8080/app?contentid=10
我想得到contentid的值是:我的弹簧控制器上面的url是10,我怎么能得到这个?
请注意,如果我在那里传递任何值(例如10,50,100,200,......等等 - 我可以输入或传递任何数字到url
),所以无论我传递给{{1那个值应该进入我的控制器(contentid
)。
目前,点击“提交”按钮后,我收到以下错误:
System.out.println(contentid);
我在STS控制台下面了。
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
控制器:
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
TestData.java:
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";
}
}
app.html:
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);
}
}
的pom.xml:
<!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>
答案 0 :(得分:0)
TestData.java存在问题。 int contentId
不能为空。将其更改为Integer contentId
。