0

So my Spring MVC project home page has two input fields within a form.

The user can enter the roll number or name.

Jsp

<form action="search" method="GET" >
        <div style="text-align: center;">
            <input  type="text" id="regNo" name="regNo" size="30" maxLength="50" "></input> or 
            <input  id="studentName" type="text" size="30" maxLength="50" "></input>
</form>

In my controller, I have two methods mapped to two input fields.

@RequestMapping(value ="/search", method = RequestMethod.GET)
    public String getStudent(String regNo, ModelMap model){

        return "A";
    }

    @RequestMapping(value="/search", method = RequestMethod.GET)
    public String searchStudentByName(String studentName, ModelMap model){

        return "B";
    }

Since both the input fields are String, I don't know how can I map them to two different methods in controller?

sofs1
  • 3,834
  • 11
  • 51
  • 89

1 Answers1

2

Did you want something like this :

@RequestMapping(method= RequestMethod.GET,value = "/search/r/regNo={regNo}")
public String getStudent(@PathVariable String regNo){

}

for studentName :

@RequestMapping(method= RequestMethod.GET,value = "/search/s/studentName={studentName}")
public String getStudent(@PathVariable String studentName){

}

then you need to add both request in different form tag, & in action tag provide :

if regNo send :

/search/r/

for studentName :

/search/s/

Jsp :

<form action="search/r" method="GET" >
        <div style="text-align: center;">
            <input  type="text" id="regNo" name="regNo" size="30" maxLength="50" "></input> 
            <input type="submit" name="approve" value="RegNo" />
</form>    
<form action="search/s" method="GET" >
        <div style="text-align: center;">
            <input  id="studentName" type="text" size="30" maxLength="50" "></input>
            <input type="submit" name="approve" value="StudentName" />
</form>

OR Second way to do so :

<form action="search" method="GET" >
                <input  type="text" id="regNo" name="regNo" size="30" maxLength="50" "></input> 
                <input  id="studentName" type="text" size="30" maxLength="50" "></input>

                <input type="submit" name="regno" value="regno" />
                <input type="submit" name="studentName" value="studentName" />
</form>

controller :

@RequestMapping(value = "/search", method = RequestMethod.GET, params = { "regno" })
public String getRegno(@RequestParam String regno) {

}

@RequestMapping(value = "/search", method = RequestMethod.GET, params = { "studentName" })
public String getStudent(@RequestParam String studentName) {

}

Post me.

user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
  • I want to send regNo on one request and studentName on another request. I won't be doing both at a time. – sofs1 Sep 30 '14 at 04:46
  • OK just a second then wait a minute, i just update the answer. – user3145373 ツ Sep 30 '14 at 04:47
  • Could you please write jsp changes as well? Thanks. – sofs1 Sep 30 '14 at 04:53
  • did you want multiple controller ? – user3145373 ツ Sep 30 '14 at 04:54
  • In jsp, I will be bringing in javascript, where if the user starts typing in the regNo, the studentName field will be disabled and vice versa. Does Using two different form tags will open any security holes? – sofs1 Sep 30 '14 at 04:55
  • Nope. I prefer single controller, since the application doesn't have any heavy business logic. – sofs1 Sep 30 '14 at 04:55
  • If I have a single search button in common for both regNo and studentName, how can I still have single search button when I have two form tags? – sofs1 Sep 30 '14 at 05:00
  • that you ll have to do using JavaScript that which to send & where to send.. Use any javascript library to perform requests. That ll be better. – user3145373 ツ Sep 30 '14 at 05:03
  • or you can use `multiple RequestMapping` concept with `2 form tags`, so controller will be one just use 2 form tag into `jsp`. – user3145373 ツ Sep 30 '14 at 05:06
  • see http://www.mkyong.com/spring-mvc/spring-mvc-handling-multipage-forms-with-abstractwizardformcontroller/, you will other new way for to do so – user3145373 ツ Sep 30 '14 at 05:07
  • May I know how can I send requests using Javascript library. Any online examples would be helpful. – sofs1 Sep 30 '14 at 06:13
  • OK. I am ready to use two controllers. But now can I use single search button in UI? – sofs1 Sep 30 '14 at 06:15
  • In your second method, should the RequestMethod be POST? If yes, Why? – sofs1 Sep 30 '14 at 06:34
  • sorry by mistake i have added it, if you want to use 2 controller & 1 button then on button click call javascript function that will check which textbox has data and as per it will send ajax request to that controller. Have you got it ? – user3145373 ツ Sep 30 '14 at 06:43
  • I don't know AJAX. Could you help me with an outline code. Please. – sofs1 Sep 30 '14 at 06:46
  • look http://www.commonj.com/blogs/2013/02/10/spring-mvc-tutorial-passing-request-parameters-via-javascript-jquery-example/, you ll get idea of ajax from it. in that one request is there, you need to check under condition. You need 2 requests, one in if true & one in if false(in else). – user3145373 ツ Sep 30 '14 at 06:49
  • Hi, Could you help me in writing the AJAX condition alone. Please. – sofs1 Sep 30 '14 at 08:35
  • HI, sorry for late reply : see http://pastebin.com/7YGKXm9D, I have done some stuff, there can be some naming & controller name change then change it as per you. – user3145373 ツ Sep 30 '14 at 08:58
  • Thanks. I was reading this http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call and thought of sharing with you. Thank you very much. – sofs1 Sep 30 '14 at 09:14
  • Hmm. Have you done it ? Means solved it ? Requests working or not ? – user3145373 ツ Sep 30 '14 at 09:24
  • Yeah I made the changes. But I am confused as how should my form block look in jsp when I use ajax. Because now I am using one button and two form tags `
    `
    – sofs1 Sep 30 '14 at 09:44
  • Oh sorry sorry, now no need of 2 form tag, use only one form tag, because all control in hand of JavaScript so easy & chill. try by removing one form & post me what happen. I also want to know how it behave. – user3145373 ツ Sep 30 '14 at 09:48
  • 1)If it is one form tag, how should my action attribute of form block look like. because in the JS we are giving two different url. 2) In the JS, should the url be /ProjectCtxt/app/search/s or just search/s would work. My actual url would be localhost:8080/ProjectCtxt/app/search/s?regNo=123 – sofs1 Sep 30 '14 at 09:51
  • in JS give `ProjectCtxt/app/search/s` as `url` in form action will be remain blank. – user3145373 ツ Sep 30 '14 at 09:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62173/discussion-between-user3705478-and-user3145373-). – sofs1 Sep 30 '14 at 10:18
  • Hi Could you please answer this question http://stackoverflow.com/questions/26136283/why-does-this-ajax-call-doesnt-return-success-and-change-url-in-browser-after-r – sofs1 Oct 02 '14 at 07:26