Java 如何将querystring传递的值从url获取到spring控制器?

Java 如何将querystring传递的值从url获取到spring控制器?,java,spring,spring-mvc,spring-boot,query-string,Java,Spring,Spring Mvc,Spring Boot,Query String,我想从给定的url获取QueryStarting传递值,如:http://localhost:8080/app?contentid=10 我想在我的spring控制器中从上面的url中获取contentid的值为:10,我如何才能得到这个值 请注意,如果我传递任何值,比如10、50100、200等等,它可以是任何数字,所以无论我传递给contentid什么,该值都应该进入我的控制器。我只想从那个url获取这个contentid值,而不是从我的html页面,或者我不想从我的html页面传递。目前

我想从给定的url获取QueryStarting传递值,如:http://localhost:8080/app?contentid=10 我想在我的spring控制器中从上面的url中获取contentid的值为:10,我如何才能得到这个值

请注意,如果我传递任何值,比如10、50100、200等等,它可以是任何数字,所以无论我传递给contentid什么,该值都应该进入我的控制器。我只想从那个url获取这个contentid值,而不是从我的html页面,或者我不想从我的html页面传递。目前我在控制器中从下面的代码中得到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>
<!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>
控制器:

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

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

}
服务:

@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();

    }

    }

首先更改html代码th:action=@{/app?contentid=10},然后在TestData类中为实例变量添加setter和getter方法。

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

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>
<!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>

请提供一个工作代码示例,例如,在第二个代码段中,没有变量userComments2。@DominikSandjaja,很抱歉,我更新了它,这是我的拼写错误。请将contentid作为请求参数发送。问题出在哪里?@Patrick,我如何将contentid作为请求参数发送,请告诉我如何获取该值尝试以下操作:th:action=@{/app?contentid=10}它可以工作contentid=10,但如果我随机给出一些其他值,那么它将不起作用。获取错误:object='testData'的验证失败。错误计数:1