0

Following is my header.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

 <head>
 <script src="http://code.jquery.com/jquery.min.js"></script>
     <style>
        #mycontainer, h1, h3 {
            text-align:center;
        }
        form {
            display:inline-block;
        }       
    </style>
 </head>

<div id="mycontainer">  
    <form method="get" action="search/s" id="number">
        <div style="text-align: center;">
            <input  type="text" id="regNo" name="regNo" size="30" maxLength="50" placeholder="Enter Register Number"> OR       
        </div>      
    </form>           

    <form method="get" action="search/l" id="name">                      
        <input  type="text" id="studentName" name="studentName" size="30" maxLength="50" placeholder="Enter Student Name"></input>          
    </form>                             
</div>             

<div style="text-align: center;">
    <input id="inputFields" type="button" value="Search" />
</div>

<script>
    $(document).ready(function(){
        $('#inputFields').click(function(event){
            if (document.getElementById('regNo').value !=""){
                $("#number").submit();

            }else if(document.getElementById('studentName').value !=""){
                $("#name").submit();
            }
        });
    });    
</script>

So I go to the homepage by localhost:8080/ProjectCtxt/mvc/template

After that I enter some value in number text field and hit enter, then the url becomes localhost:8080/ProjectCtxt/mvc/search/s?regNo=123 . Now if I again enter another register number the url becomes

localhost:8080/ProjectCtxt/mvc/search/s/search/s?regNo=124

An additional search/s gets added. I am using Apache tiles.

"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">

<tiles-definitions>
    <definition name="template" template="/WEB-INF/jsp/template.jsp">
        <put-attribute name="title" value=""/>
        <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
        <put-attribute name="body" value="/WEB-INF/jsp/ads.jsp  "/>
        <put-attribute name="center" value="/WEB-INF/jsp/ads.jsp" />
        <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" /> 
    </definition>

    <definition name="header" extends="template">
        <put-attribute name="title" value="" />  
        <put-attribute name="body" value="/WEB-INF/jsp/ads.jsp" />  
    </definition>

    <definition name="numberResult" extends="template">
        <put-attribute name="title" value="" />  
        <put-attribute name="body" value="/WEB-INF/jsp/numberResult.jsp" />  
    </definition>

    <definition name="nameResult" extends="template">
        <put-attribute name="title" value="" />  
        <put-attribute name="body" value="/WEB-INF/jsp/nameResult.jsp" />  
    </definition>

    <!-- <definition name="bottom" extends="new.template">
        <put-attribute name="bottom" value="/mvc/jsp/ads.jsp" />
        <put-attribute name="bottom" value="/WEB-INF/jsp/ads.jsp" />
    </definition> -->

</tiles-definitions>

My controller returns numberResult for every successful search has been made.

Anders Lindén
  • 6,839
  • 11
  • 56
  • 109
sofs1
  • 3,834
  • 11
  • 51
  • 89

1 Answers1

1

Use <form method="POST" action="search/s" id="number"> instead of
<form method="get" action="search/s" id="number">.
GET merge the fields in url. BUT POST sends the field in the body of request. And modify your Controller accordingly e.g. @RequestMapping(value="/new", method = RequestMethod.POST) For more details have a look at http://www.w3schools.com/tags/ref_httpmethods.asp

Pradeep Kr Kaushal
  • 1,506
  • 1
  • 16
  • 29
  • Ok. Now I don't see the name=value after the url when I make a second search, but I see it as localhost:8080/ProjectCtxt/mvc/search/search/s and I get 404 error. – sofs1 Oct 05 '14 at 22:31
  • If you are using spring mvc then you need to put information at your method level e.g. @RequestMapping(value="/new", method = RequestMethod.POST) – Pradeep Kr Kaushal Oct 06 '14 at 01:44
  • 1)Yes, I have put in method level only. I renamed 'search/s' as 'number' and 'search/l' as name and it is working now when I use POST. But I don't know why it doesn't work when I use search/s and search/l. Could you explain please. I am learning Spring. 2)Could you answer this question, please. http://stackoverflow.com/questions/26209892/why-does-the-search-button-in-the-following-jsp-code-doesnt-work – sofs1 Oct 06 '14 at 04:09
  • Could you please answer this question? http://stackoverflow.com/questions/26291881/how-can-i-submit-a-html-form-post-that-has-several-different-iterated-values – sofs1 Oct 10 '14 at 04:20