Search code examples
javahtmlspringspring-mvctiles2

How to remove redundant form action tag from the url?


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.


Solution

  • 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