0

When i launch the below url

http://localhost:8090/QuickStartConsulting/quickstart/email?key=6226f7cbe59e99a90b5cef6f94f966fd

The following controller method is invoked

    @RequestMapping(value = "/quickstart/email")

    public String viewQuickStartEmailForm(@ModelAttribute(value = "quickbean") 
    QuickStartBean quickbean,BindingResult result,Model model) {

        try {   

            //System.out.println(quickbean.getKey());           
email=quickbean.getEmail();
model.addAttribute("email", email);

model.addAttribute("quickstartdatabean",new QuickStartBean() );


        } catch (Exception e) {
            e.printStackTrace();
        }


        return "quickstart/emaillogin";
    }

Here is my emaillogin jsp page

    <form:form id="requestForm" method="GET" modelAttribute="quickbean" ACTION="${pageContext.request.contextPath}/quickstart/email" >
                   <form:hidden path="key" />                                      
                    <table>
                        <tr>
                            <td><span><img src="<c:url value="/views/images/youremailid.png"/>"> </img></span></td>
                            <td>
                                <form:input type="text" style="width: 300px;" id="name" path="email" title="xyz@email.com"/>

                        </td></tr>


                        <tr>
                        <td>

                         <input type="image" id="invite_btn" src="<c:url value="/views/images/submit.png"/>"  title="create invite" width="170" height="32"/> 

                        </td>
                        </tr>


                    </table>



                </form:form>

How can i store the value of path variable 'key' and use it in my controller method? the emaillogin page is the first page and is not redirected from any other jsp page.

Sarthak Srivastava
  • 1,490
  • 3
  • 15
  • 33
  • 'key' is no path variable, it is a request parameter! see http://stackoverflow.com/questions/13715811/requestparam-vs-pathvariable/13718502#13718502 – Ralph Oct 14 '16 at 08:51

3 Answers3

1

Use the @RequestParam annotation

public String viewQuickStartEmailForm(@RequestParam("key") String key, ....) {...}

Btw: @See @RequestParam vs @PathVariable for details and difference between request params and path variables

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • when i use the @RequestParam("key") String key i am getting a exception – Sarthak Srivastava Oct 14 '16 at 08:45
  • java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'quickbean' available as request attribute – Sarthak Srivastava Oct 14 '16 at 08:46
  • `public String viewQuickStartEmailForm(@RequestParam("key") String key, @ModelAttribute(value = "quickbean") QuickStartBean quickbean,BindingResult result,Model model)` -- but you should clean up that parameter mess – Ralph Oct 14 '16 at 08:50
  • when i am putting RequestParam("key") before ModelAttribute.... I am not able to use quickbean to get form values – Sarthak Srivastava Oct 14 '16 at 09:40
  • than put at at the end (but not between quickbeen and binding result) – Ralph Oct 14 '16 at 09:48
0

First define pathVariables as follows:

public String viewQuickStartEmailForm(@PathVariable Map pathVariables,..)

You can get the values by using below code snippet:

    if (pathVariables != null) {
        if (pathVariables.containsKey("fieldName") &&
                !"undefined".equals(
                    (String)pathVariables.get("fieldName")) &&
                !"null".equals(
                    (String)pathVariables.get("fieldName"))) {
            params.setFieldName((String)pathVariables.get("fieldName"));
        }
    }
0

There is a difference between @PathVariable and @RequestParam. For example :

@RequestParam used for accessing the values of the query parameters.

URL : http://localhost:8081/Test/card?cardId=1012111

@RequestMapping("/card")
public String getCard(@RequestParam(value="cardId", required=true) String cardId){

System.out.println("print the value of the cardId : "+cardId)

....... }

where as @PathVariable used for accessing the values from the URI template.

URL : http://localhost:8081/Test/card/1012111

@RequestMapping("/card/{cardId}")
public String updateCard(@PathVariable(value="cardId") String cardId){

System.out.println("print the value of the cardId : "+cardId)

....... }

So you can correct your code and use any one of the above mentioned approach rather then mixing both of them.

denzal
  • 1,225
  • 13
  • 20