gpt4 book ai didi

java - 如何将 url 中的查询字符串传递的值传递到 spring Controller 中?

转载 作者:行者123 更新时间:2023-12-02 11:40:46 26 4
gpt4 key购买 nike

我想从给定的url获取查询字符串传递值,例如:http://localhost:8080/app?contentid=10我想获取我的 spring Controller 中上述 url 的 contentid 值为:10,我怎样才能得到这个?

请注意,如果我在那里传递任何值(例如 10、50,100、200,...等 - 可以是任何数字),因此无论我传递给 contentid,该值都应该进入我的 Controller 。我只想从该 url 获取此 contentid 值,而不是从我的 html 页面获取,或者我不想从我的 html 页面传递。目前,我从 Controller 中的以下代码中获取 null ,但没有获取传递值(例如来自 url 的 10)。我怎样才能做到这一点?预先感谢您的帮助!

app.html:

<form action="#" th:action="@{/app}" 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" ></textarea>
</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>

测试数据.java:

public class TestData implements Serializable{
private String firstname;
private String secondname;
private int contentid;

//setters and getters

public TestData() {}
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);
}

}

Controller :

public class TestDataController {
@Autowired
TestService testDataService;
@RequestMapping(value="/app", method=RequestMethod.POST)
public String testDataSubmit(@RequestParam(required=false) Integer contentid, @ModelAttribute TestData testData, Model model, HttpServletRequest request) {
String id = request.getQueryString();
System.out.println("My Id: "+id);//null
System.out.println("URL Parameter: "+testData.getContentid());//0
System.out.println("URL Parameter passed objectid: "+contentid); //null
testDataService.saveTestDataDetails(testData);
return "app";
}

}

服务:

@Service
public class TestService {

@PersistenceContext
private EntityManager entityManager;

public void saveTestDataDetails(TestData testData) {
StoredProcedureQuery sp = entityManager.createStoredProcedureQuery("APP.TESTDATA");
sp.registerStoredProcedureParameter("firstname", String.class, ParameterMode.IN);
sp.registerStoredProcedureParameter("secondname", Integer.class, ParameterMode.IN);
sp.registerStoredProcedureParameter("contentid", Integer.class, ParameterMode.IN);

sp.setParameter("firstname", testData.getFirstname());
sp.setParameter("secondname", testData.getSecondname());
sp.setParameter("contentid", testData.getContentid());

sp.execute();

}

}

最佳答案

经过每次聊天的讨论后,我将为您提供一个可行的解决方案,您可以根据自己的需要指定任何内容。

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>

Controller :

@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";
}

}

关于java - 如何将 url 中的查询字符串传递的值传递到 spring Controller 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48577315/

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