• springmvc-3.2-jsr303解决服务端验证问题


    从以前的验证:Stringutils.isEmpty....到struts的验证:xxxvalidate
    现在使用jsr303使之更加简单

     依赖hibernate-validator-4.xx.jar

    实体类中的变化

    @Entity
    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
    public class Admin {
    
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Integer id;
        // 加NotEmpty注解,如果为空有提示
        @NotEmpty(message="账号不能为空")
        private String name;
        @NotEmpty(message="密码不能为空")
        private String password;

    表单书写方式发生变化

    首先导入springmvc的标签

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

            <!--来这个表单之前先传一个admin过来  commandName="admin"  -->
    <form:form method="POST" commandName="admin">
        <legend>登录</legend>
        <!-- <form:errors path="*" cssClass="help-inline text-error" element="span"/>这样是所有错误都在这显示,也可以像下面分开写 -->
        <label>账号</label>
        <!-- path相当于以前的name属性 -->
        <form:input path="name"/>
        <form:errors path="name" cssClass="help-inline text-error" element="span"/>
        <label>密码</label>
        <form:password path="password"/>
        <form:errors path="password" cssClass="help-inline text-error" element="span"/>
        <div class="form-actions">
            <button class="btn btn-primary">登录</button>
        </div>
    </form:form>        
            

    ----------------------------------------------------------

    /*以前是这样 
        @RequestMapping(value="/",method=RequestMethod.GET)
        public String index() {
            
            return "index";
        }    */
    /* 现在有了commandName="admin"这个form属性就必须在到达页面前传过去一个admin,虽然是空的 */
    @RequestMapping(value="/",method=RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("admin", new Admin());
        return "index";
    }        
            
    /* 在提交的方法中也要有所改变 */
    @RequestMapping(value="/",method=RequestMethod.POST)
        /* 使用jsr303,提交过来的表单必须用对象接受,不能用name,password
        需要加注解@Valid表明admin是需要验证的
            而且@Valid Admin admin必须放在最前面*/
        public String login(@Valid Admin admin,BindingResult bindingResult,HttpSession session,RedirectAttributes redirectAttributes) {
            /* 添加了BindingResult这个参数,如果验证有了错误就return到index页面
                注意,不是重定向到index
                
                做到这里效果就是如果有错误就会还在index页面并提示账户和密码不能空
                当账户写了,但是密码没写,就会只提示密码为空,而且账户的input框中填写的信息不会消失
                就是因为
                1.我们来这个页面的时候传过来一个空的admin对象,所以他把你写的值都set放入到admin中
                2.input框中填写的信息没有消失,是因为我们是return index而不是重定向,然后又把admin中的值get进去
                */
            if(bindingResult.hasErrors()) {
                return "index";
            }
            
            
            Admin currAdmin = adminService.login(admin.getName(),admin.getPassword());
            if(currAdmin == null) {
                redirectAttributes.addFlashAttribute("message", "账号或密码错误");
                <!-- 重定向 -->
                return "redirect:/";
            } else {
                session.setAttribute("curr_admin", currAdmin);
                return "redirect:/book";
            }
        }        
            
            
  • 相关阅读:
    SQLite的使用
    Messenger类的使用
    Binder的使用(跨进程——AIDL,非跨进程)
    Android Studio中如何创建AIDL
    第二章——Parcelable接口的使用(跨进程,Intent传输)
    InetAddress的作用
    第二章——Serializable的使用(跨进程使用和Intent的传递对象)
    SurfaceView绘图机制
    双缓冲机制简介
    内部类代码
  • 原文地址:https://www.cnblogs.com/itliucheng/p/4432696.html
Copyright © 2020-2023  润新知