java - Retain the value of textbox when an error occurs in spring -


i developing webapp. in app have page have 3 search options based on id. if id wrong want retain id in search textbox , want show error message. tried

modelandview.addobject("id", value);

and working fine , now want know if there better way because assume have big form , want retain value of every field difficult using above approch. please help!

and using search id , name both thats why have try , catch blocks

this jsp file

    html>     <head>         <link rel="stylesheet" type="text/css" href="./css/style.css" />         <link rel="stylesheet" type="text/css" href="./bootstrap/css/bootstrap.css" />         <link rel="stylesheet" type="text/css" href="./bootstrap/css/bootstrap.min.css" />         <link rel="stylesheet" type="text/css" href="./bootstrap/css/bootstrap-responsive.css" />         <link rel="stylesheet" type="text/css" href="./bootstrap/css/bootstrap-responsive.min.css" />         <script>             function redirecttosearchresult(textboxid){                 window.location.href= document.getelementbyid(textboxid).name+'.htm?'+'id='+document.getelementbyid(textboxid).value;             }         </script>     </head>     <body>         <div class="page-header"></div>          <div id="searchbybuyer">             <label id="e_b_i"><h2>buyer search<h2></label><br>             <input type="text" id="s_b_b" class="text-box" name="searchbybuyer" value=${buyerid} ></input>             <input type="button" id="buyersearch" class="btn-custom" value="search" onclick="redirecttosearchresult('s_b_b')"/>         </div>          <div id="searchbycategory">             <label id="e_c_i"><h2>category search<h2></label><br>             <input type="text" id="s_b_c" class="text-box" name="searchbycategory" value=${categoryid} ></input>             <input type="button" id="categorysearch" class="btn-custom" value="search" onclick="redirecttosearchresult('s_b_c')"/>         </div>          <div id="searchbyarticle">             <label id="e_a_i"><h2>article search<h2></label><br>             <input type="text" id="s_b_i" class="text-box" name="searchbyarticle" value=${articleid} ></input>             <input type="button" id="articlesearch" class="btn-custom" value="search" onclick="redirecttosearchresult('s_b_i')"/><br>         </div>          <br>         <label style="color:red; padding-left:45em; padding-top:15em"><h4>${error}</h4></label>      </body>  </html> 

and controller

`

import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.servlet.modelandview;  @controller public class buyercontroller {      @autowired     private buyerrepo buyerrepo;      @requestmapping(value = "/searchbybuyer.htm", method = requestmethod.get)     public modelandview searchfields(@requestparam(value = "id") string buyerid) throws invalididexception {         buyer buyer = null;         modelandview buyersearch = new modelandview("buyersearch");         modelandview errorview = errorview.geterrorview("buyerid", buyerid, "enter valid buyer id!");          try {             buyer = buyerrepo.getbuyer(long.parselong(buyerid));          } catch (numberformatexception e) {             buyer = buyerrepo.getbuyer(buyerid);             if (buyer == null) return errorview;          } catch (exception e) {             buyersearch = errorview;         }          buyersearch.addobject("buyer", buyer);         return buyersearch;     }  } 

`

this error , view class create error view parameters

`

import org.springframework.web.servlet.modelandview;  public class errorview {      public static modelandview geterrorview(string key, string value, string message) {         modelandview errorview = new modelandview("index");         errorview.addobject("error", message);         errorview.addobject(key, value);         return errorview;     } } 

`

spring has jsr 303-bean-validation support web forms. build in way easyer use own implementation.

you need:

  • a command object gets values form. each fields can have jsr 303-bean-validation annotations indicate constaints want enforce
  • you need web controller method (at least) 2 parameters: @valid command object, bindingresult (the bindingresult must parameter after command object)
  • in web controller method need check bindingresult, , if has failure need render form again
  • in form need use form:errors show errors (form:errors can show errors specific field)
  • you need spring configuration: <mvc:annotation-driven /> (there more possible should enough begin)
  • you need jsr 303-bean-validation libary, example: hibernate validator (this not hibernate orm)

but example explain best: http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -