thymeleaf 实例

来源:互联网 发布:淘宝消保30元的条件 编辑:程序博客网 时间:2024/06/05 16:19
package com.nroad.heartserver.model;import org.hibernate.validator.constraints.NotEmpty;import org.hibernate.validator.constraints.Range;import javax.validation.constraints.NotNull;import javax.validation.constraints.Size;/** * Created by jiyy on 2017/1/3. */public class AnimalForm {    private long id;    @NotEmpty(message="动物名: 不能为空")    private String oname;    @Range(min = 1, message="数量: 必须大于0")    @NotNull(message="数量: 不能为空")    private int ocount;    @Size(max = 10, message="备注: 长度不能超过10个字符")    private String memo;    public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public String getOname() {        return oname;    }    public void setOname(String oname) {        this.oname = oname;    }    public int getOcount() {        return ocount;    }    public void setOcount(int ocount) {        this.ocount = ocount;    }    public String getMemo() {        return memo;    }    public void setMemo(String memo) {        this.memo = memo;    }}
package com.nroad.heartserver.controller;import com.nroad.heartserver.model.AnimalForm;import org.springframework.stereotype.Controller;import org.springframework.validation.BindingResult;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import javax.validation.Valid;/** * Created by jiyy on 2017/1/3. */@Controllerpublic class AnimalController {    @RequestMapping("/tohtml")    public ModelAndView algorithmConfiguration(){        ModelAndView model = new ModelAndView();        model.addObject("animalForm",new AnimalForm());        model.addObject("animalFormU",new AnimalForm());        model.setViewName("/test/zoolist");        return model;    }   /* @RequestMapping(path = "/list", params = {"save"}, method = RequestMethod.POST)    public String doAdd(Model model, @Valid AnimalForm form, BindingResult result){        System.out.println("动物名:" + form.getOname());        System.out.println("数量:" + form.getOcount());        System.out.println("备注:" + form.getMemo());        if(result.hasErrors()){            model.addAttribute("MSG", "出错啦!");        }else{            model.addAttribute("MSG", "提交成功!");        }        return "/test/zoolist";    }*/    @RequestMapping(path = "/list", params = {"save"}, method = RequestMethod.POST)    public ModelAndView doAdd(@Valid AnimalForm form, BindingResult result){        ModelAndView model = new ModelAndView();        System.out.println("动物名:" + form.getOname());        System.out.println("数量:" + form.getOcount());        System.out.println("备注:" + form.getMemo());        if(result.hasErrors()){            model.addObject("MSG", "出错啦!");        }else{            model.addObject("MSG", "提交成功!");        }        model.setViewName("/test/zoolist");        return model;    }    /*@RequestMapping(path = "/list", params = {"save"}, method = RequestMethod.POST)    public ModelAndView doAdd(ModelAndView model, @Valid AnimalForm form, BindingResult result){        System.out.println("动物名:" + form.getOname());        System.out.println("数量:" + form.getOcount());        System.out.println("备注:" + form.getMemo());        if(result.hasErrors()){            model.addObject("MSG", "出错啦!");        }else{            model.addObject("MSG", "提交成功!");        }        model.setViewName("/test/zoolist");        return model;    }    @RequestMapping(path = "/list", params = {"save"}, method = RequestMethod.POST)    public String doAdd(@Valid AnimalForm form, BindingResult result){        System.out.println("动物名:" + form.getOname());        System.out.println("数量:" + form.getOcount());        System.out.println("备注:" + form.getMemo());        return "/test/zoolist";    }*/}
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>    <title>zoo list</title></head><body><!--<a href='.'>首页</a>->动物列表--><br/><br/><div th:text="${MSG}">这里是信息提示.</div><br/><div th:errors="${animalForm.oname}"></div><div th:errors="${animalForm.ocount}"></div><div th:errors="${animalForm.memo}"></div><br/><form id="iform" th:action="@{/list.html?save}" th:method="post" th:object="${animalForm}">    <table border="1">        <tr>            <th>动物名称</th>            <th>数量</th>            <th>备注</th>            <th>Action</th>        </tr>        <tr>            <td><input type="text" name="oname" value="" th:value="*{oname}"/><br/><b th:errors="${animalForm.oname}"/></td>            <td><input type="text" name="ocount" value="" th:value="*{ocount}"/><br/><b th:errors="${animalForm.ocount}"/></td>            <td><input type="text" name="memo" value="" th:value="*{memo}"/><br/><b th:errors="${animalForm.memo}"/></td>            <td><input type="submit" value="添加"/></td>        </tr>    </table></form><form id="uform" th:action="@{/list.html?save}" th:method="post" th:object="${animalFormU}">    <table border="1">        <tr>            <th>动物名称</th>            <th>数量</th>            <th>备注</th>            <th>Action</th>        </tr>        <tr>            <td><input type="text" name="oname" value="" th:value="*{oname}"/><br/><b th:errors="${animalFormU.oname}"/></td>            <td><input type="text" name="ocount" value="" th:value="*{ocount}"/><br/><b th:errors="${animalFormU.ocount}"/></td>            <td><input type="text" name="memo" value="" th:value="*{memo}"/><br/><b th:errors="${animalFormU.memo}"/></td>            <td><input type="submit" value="添加"/></td>        </tr>    </table></form><hr/><table border="1">    <tr>        <th>序号</th>        <th>动物名称</th>        <th>数量</th>        <th>备注</th>    </tr>    <tr>        <td>1</td>        <td>大马猴</td>        <td>10</td>        <td>机灵古怪,俏皮活泼</td>    </tr>    <tr>        <td>2</td>        <td>大熊猫</td>        <td>80</td>        <td>体型笨重,喜欢吃竹子</td>    </tr>    <tr>        <td>3</td>        <td>澳洲羊驼</td>        <td>13</td>        <td>长相奇特,大国人俗称其草泥马</td>    </tr>    <tr>        <td>4</td>        <td>峨眉山猴</td>        <td>90</td>        <td>不怕人,有时候发贱抢游客面包吃</td>    </tr></table></body></html>
0 0
原创粉丝点击