SpringMVC的資料校正

來源:互聯網
上載者:User

標籤:request   bin   ppi   mode   資料   font   方法   size   asc   

SpringMVC的資料校正:

  在web應用程式中,為了防止用戶端傳來的資料引發程式異常,常常需要對 資料進行驗證。輸入驗證分為用戶端驗證與伺服器端驗證。用戶端驗證主要通過JavaScript指令碼進行,而伺服器端驗證則主要通過Java代碼進行驗證。 為了保證資料的安全性,一般情況下,用戶端和伺服器端驗證都是必須的。

實現步驟如下:

  1.首先我們需要引入jar包,注意jar包的衝突,hirbernate-validator引入4.0版本即可。

  

  2.配置驗證器

  

  3.定義實體類,進行註解配置

  

  4.書寫Controller方法,進行處理器方法的處理

@Controllerpublic class MyController {@RequestMapping("/first.do")public ModelAndView doFrist(@Valid UserInfo info,BindingResult br){ModelAndView mv=new ModelAndView();int errorCount = br.getErrorCount();if(errorCount>0){FieldError name = br.getFieldError("name");FieldError score = br.getFieldError("score");FieldError phone = br.getFieldError("phone");if(name!=null){mv.addObject("namemsg",name.getDefaultMessage());}if(score!=null){mv.addObject("scoremsg",score.getDefaultMessage());}if(phone!=null){mv.addObject("phonemsg",phone.getDefaultMessage());}

                   mv.setViewName("/index.jsp");}mv.setViewName("/welcome.jsp");return mv;}}

   5.然後在介面進行資料的渲染

<form action="${pageContext.request.contextPath }/first.do" method="post">                 成績:<input name="score"/> <span>${scoremsg }</span><br/><br/>                 姓名:<input name="name"/><span>${namemsg }</span><br/><br/>                 電話:<input name="phone"/><span>${phonemsg }</span><br/><br/>             <input type="submit" value="註冊"/></form>

     6.測試:

 

資料校正,保護資料的第一道防線!!!

      --- 厲害了,我的校正

 

 

  

 

SpringMVC的資料校正

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.