簡體   English   中英

如何從URL獲取querystring傳遞的值到spring控制器中?

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

我想從給定的url獲取querystrting傳遞的值,例如: http://localhost:8080/app?contentid=10我想獲得contentid is: 10從我的spring控制器中的上述url獲得contentid is: 10我明白了嗎?

請注意,如果我在那里傳遞任何值(例如10、50、100、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>

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

}

控制器:

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM