Home > OS >  How to send data from input id in Form to RequestMapping and PathVariable in Spring?
How to send data from input id in Form to RequestMapping and PathVariable in Spring?

Time:07-15

I'm using Spring to parse data Json when send request and find a phone number by parameter mobilePhoneNumber. I want to input phone number in text box, and submit data and send to controller by PathVariable parameter to search phone number in JSON data:

this is my format json request:

{
  "sessionKey": "2022111723",
  "fiCode": "B100000022",
  "code": "0011524",
  "appNumber": "APPL000000000000999",
  "customerNumber": "12345678",
  "mobilePhoneNumber": "0988567896"
}

this is my code in JSP:

<form id="insBasicForm" name="insBasicForm" action="${pageContext.request.contextPath}/api/v1/checkPhoneNumber/$mobilePhoneNumber" method="POST">
    <input type="text" id="mobilePhoneNumber" name="mobilePhoneNumber" maxlength="10" title="" placeholder="Telephone Number (Mobile- 10 Digits)"  />
    <input type="submit" name="" value="Check CCP" >
</form>

this is my class:

@RequestMapping("/api/v1/checkPhoneNumber/{mobilePhoneNumber}")
@ResponseBody
public ResponseEntity<ReturnMessage> checkCCPHistoryCustomerPoint(@PathVariable("mobilePhoneNumber") String mobilePhoneNumber,
        @RequestParam Map<String, Object> params, ModelMap model) throws Exception {
            
                
        ReturnMessage message = new ReturnMessage();
        RestTemplate restTemplate = new RestTemplate();

        String url = "https://localhost:8080/VR2_REQUEST";

        String token = AppConstants.TOKEN;

        JSONObject jsonObject =  new JSONObject();
        JSONArray jsonArray =  new JSONArray();

        int recordSuccess = 0 , recordFail = 0;     
        
        try {
            String sessionKey = "2022111723";
            String fiCode = "B100000022";
            String code = "0011524";
            String appNumber = "APPL000000000000999";
            String customerNumber = "12345678";                 // "customerNumber": "12345678",
            // String mobilePhoneNumber = "0988567896";                 // "0988567896"     

            System.out.println("String URL: "   url);

            jsonObject.put("sessionKey", sessionKey);
            jsonObject.put("fiCode", fiCode);
            jsonObject.put("code", code);
            jsonObject.put("appNumber", appNumber);
            jsonObject.put("customerNumber", customerNumber);
            jsonObject.put("mobilePhoneNumber", mobilePhoneNumber);

            jsonArray.put(jsonObject);

            System.out.println("params -> {}"   jsonObject);    
        }
        catch(HttpClientErrorException exception) {
            // TODO Auto-generated catch block              
            essage.setMessage(e.toString());
            e.printStackTrace();
            return ResponseEntity.ok(message);
        }
                
   }

when i run program, it seem return null value and there is an exception following as:

2022-07-15 15:37:45,839  WARN [org.springframework.web.servlet.PageNotFound] No mapping found for HTTP request with URI [/api/v1/checkPhoneNumber/] in DispatcherServlet with name 'servlet'

So how to send data from input id in Form to RequestMapping and PathVariable ?

CodePudding user response:

I have tried all the possible ways to get the input value inside form through the PathVariable but it seems to not work.

I assume you already know about this below. If your requirement is to get the number into the controller, You can directly use @ModelAttribute(mobilePhoneNumber) String mobilePhoneNumber instead of using a path variable and string to fetch the number through @PathVariable.

In this case the

action="${pageContext.request.contextPath}/api/v1/checkPhoneNumber"

and controller will be like

public ResponseEntity<ReturnMessage> checkCCPHistoryCustomerPoint(@ModelAttribute("mobilePhoneNumber") String mobilePhoneNumber,
        @RequestParam Map<String, Object> params, ModelMap model) throws Exception

I assume you already know this. Thankyou

CodePudding user response:

you will need javaScript for that as jps will be converted into HTML after processing and since HTML is static it means that when the form load it loads with a fixed URL.

CodePudding user response:

Hii I have tried different methods to send data from input to URL but it is not working then I have tried using javascript if you have no restrictions to use js you can do like this but it will work only if you have one data to send so if you use to model the work will be done quickly or you can try like this.

<form id="insBasicForm" name="insBasicForm"  method="post">
<input type="text" id="mobilePhoneNumber" name="mobilePhoneNumber" maxlength="10" placeholder="Telephone Number (Mobile- 10 Digits)"  />
<input type="button" name="" value="submit"  onclick="createUrl()">
<script type="text/javascript">
function createUrl(){
let mobilePhoneNumber = document.getElementById("mobilePhoneNumber").value;
window.location =  "/api/v1/checkPhoneNumber/" mobilePhoneNumber;
}
  • Related