spring mvc 資料校正

來源:互聯網
上載者:User

標籤:ber   htm   int   frame   text   imp   orm   result   let   

1、需要匯入的jar包:

slf4j-api-1.7.21.jar

validation-api-1.0.0.GA.jar

hibernate-validator-4.0.1.GA.jar

 

 

2、訪問頁面編碼:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title></title>    <script type="text/javascript" src="js/jquery-1.8.3.js"></script>     </head>    <body>    <h1>資料校正</h1>    <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>  </body></html>

3、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:mvc="http://www.springframework.org/schema/mvc"      xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd        ">        <!-- 配置包掃描器-->       <context:component-scan base-package="cn.happy.controller"></context:component-scan>              <!-- 配置驗證器 -->       <bean id="myvalidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">           <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>       </bean>               <mvc:annotation-driven validator="myvalidator"/></beans>

4、controller控制器

package cn.happy.controller;import javax.validation.Valid;import org.springframework.stereotype.Controller;import org.springframework.validation.BindingResult;import org.springframework.validation.FieldError;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import cn.happy.entity.UserInfo;@Controllerpublic class FirstController {    @RequestMapping("/first.do")    public ModelAndView doFirst(@Valid UserInfo info,BindingResult br){        ModelAndView mv=new ModelAndView();        mv.setViewName("/WELCOME.jsp");        //記錄到底是哪個欄位驗證失敗了        //有一個可以偵測到驗證錯誤總數的方法        int errorCount = br.getErrorCount();        if (errorCount>0) {            //證明模型驗證失敗            FieldError score = br.getFieldError("score");            FieldError name = br.getFieldError("name");            FieldError phone = br.getFieldError("phone");            if (score!=null) {                mv.addObject("scoremsg",score.getDefaultMessage());            }                        if (name!=null) {                mv.addObject("namemsg",name.getDefaultMessage());            }                        if (phone!=null) {                mv.addObject("phonemsg",phone.getDefaultMessage());            }            mv.setViewName("/index.jsp");        }                //高中  英文版的吻別        return mv ;    }}

 

spring mvc 資料校正

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.