简体   繁体   English

如何从URL获取querystring传递的值到spring控制器中?

[英]How to get querystring passed value from the url into spring controller?

I want to get the querystrting passing value from the given url , like: http://localhost:8080/app?contentid=10 I want to get the value of contentid is: 10 from the above url in my spring controller, how can I get this ? 我想从给定的url获取querystrting传递的值,例如: http://localhost:8080/app?contentid=10我想获得contentid is: 10从我的spring控制器中的上述url获得contentid is: 10我明白了吗?

Please note that if I pass any value there(like 10, 50,100, 200, ...etc - it can be any number), so whatever I am passing to contentid , that value should get into my controller. 请注意,如果我在那里传递任何值(例如10、50、100、200,...等-可以是任何数字),那么无论我传递给contentid哪个值,该值都应进入我的控制器。 I want to get this contentid value from that url only, not from my html page or I don't want to pass from my html page. 我只想从该url获取此contentid值,而不是从我的html页面获取,或者我不想从我的html页面传递。 Currently I am getting null from the below code in controller, I am not getting the passing value(like 10 from the url ). 目前,我从控制器中的以下代码获取null ,没有获取传递的值(如url 10)。 How can I achieve this ? 我该如何实现? Thanks in advance for your help ! 在此先感谢您的帮助 !

app.html: 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>

TestData.java: TestData.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: 服务:

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

    }

    }

After discussion per chat I will provide you a working solution where you can specifiy any stuff by your own needs. 在每次聊天讨论之后,我将为您提供一个可行的解决方案,您可以根据自己的需要指定任何内容。

app.html: 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";
    }

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Spring Boot从URL获取传递的查询字符串值 - How to get passed querystring value from url using Spring boot Spring Thymeleaf-如何映射到需要从HTML传递到控制器的ID的URL? - Spring Thymeleaf - How to map to a URL that requires an ID that is passed from HTML to the controller? 如何将传递的参数保存在URL中以在Spring中的其他控制器中使用? - How to save the passed parameter in the URL for use in different controller in Spring? Spring从Controller中的URL获取所有参数 - Spring Get all params from URL in Controller 如何从spring控制器获取angularjs中的模型属性值 - how to get model attribute value in angularjs from spring controller 如何从Spring MVC控制器中的无序列表下拉列表中获取值 - how to get value from unordered list dropdown in spring mvc controller 如何从JavaScript中的Spring Controller类获取价值? - How to get value from spring controller class in javascript? 如何获取在url中传递给模型和视图控制器的值? - How to get values passed in url to model and view controller? 如何使用Spring Controller获取请求URL? - How to get request url using spring controller? 在Spring Boot中如何从控制器向网页发送值或从网页上的控制器获取值? - How send value from controller to web page or get value from controller on web page in Spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM