gpt4 book ai didi

java - 从 Java Controller 启动 html 页面时添加查询字符串值

转载 作者:行者123 更新时间:2023-11-30 01:59:31 24 4
gpt4 key购买 nike

我有一个 HTML 页面,在发布后从 java Controller 启动,我想在 url ex 中附加一个查询字符串值:(localhost:8000/gdata?id=11)。这可以做到吗?这是我的 Controller 代码:

package com.sa.example;

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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestParam;

import com.sentinel.advisor.GData;
import com.sentinel.advisor.GDataJdbcRepository;

@Controller
public class GDataController {

@Autowired
GDataJdbcRepository repository;

@GetMapping("/gdata")
public String gDataForm(Model model) {
return "gData";
}

@PostMapping("/gdata")
public String gDataSubmit(@ModelAttribute GData gData) {
String returnString = repository.insert(gData);
//returnString should be returned in the url as a query string
return "result";
}

}

最佳答案

您可以使用重定向(最好的做法是在发布后重定向,无论看到 - https://en.wikipedia.org/wiki/Post/Redirect/Get

Spring 的重定向 View :

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/view/RedirectView.html

类似于:

@Controller
public class GDataController {

@Autowired
GDataJdbcRepository repository;

@GetMapping("/gdata")
public String gDataForm(Model model) {
return "gData";
}

@PostMapping("/gdata")
public RedirectView gDataSubmit(@ModelAttribute GData gData) {
String returnString = repository.insert(gData);
return new RedirectView("/sucess?returnString=" + returnString, true);
}

@GetMapping("/success")
public String getResultPage(@RequestParam("returnString")String returnString){
return "result";
}
}

关于java - 从 Java Controller 启动 html 页面时添加查询字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53340916/

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