thymeleaf 实例

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.
 */
@Controller
public 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";
    }*/

}

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>zoo listtitle>
head>
<body>

<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>Actionth>
        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>Actionth>
        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>1td>
        <td>大马猴td>
        <td>10td>
        <td>机灵古怪,俏皮活泼td>
    tr>
    <tr>
        <td>2td>
        <td>大熊猫td>
        <td>80td>
        <td>体型笨重,喜欢吃竹子td>
    tr>
    <tr>
        <td>3td>
        <td>澳洲羊驼td>
        <td>13td>
        <td>长相奇特,大国人俗称其草泥马td>
    tr>
    <tr>
        <td>4td>
        <td>峨眉山猴td>
        <td>90td>
        <td>不怕人,有时候发贱抢游客面包吃td>
    tr>
table>
body>
html>

你可能感兴趣的:(thymeleaf 实例)