Spring MVC笔记(六) springMVC标签

记录下SPRINGMVC常用的标签,直接代码,不解释:

package com.bwy.springmvc.tag.textbox;

/**
 * @author Administrator
 *
 */
public class Student {
  private String name;
  private Integer age;
  private Integer id;
  private String passWord;
  private String address;
  private boolean agree;
  private String gender;
  private String favoriteColor;
  private String country;
  private String[] languages;


  public String[] getLanguages() {
    return languages;
  }

  public void setLanguages(String[] languages) {
    this.languages = languages;
  }

  public String getCountry() {
    return country;
  }

  public void setCountry(String country) {
    this.country = country;
  }

  public String getFavoriteColor() {
    return favoriteColor;
  }

  public void setFavoriteColor(String favoriteColor) {
    this.favoriteColor = favoriteColor;
  }

  public String getGender() {
    return gender;
  }

  public void setGender(String gender) {
    this.gender = gender;
  }

  private String[] favoriteSports;

  public String[] getFavoriteSports() {
    return favoriteSports;
  }

  public void setFavoriteSports(String[] favoriteSports) {
    this.favoriteSports = favoriteSports;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getPassWord() {
    return passWord;
  }

  public void setPassWord(String passWord) {
    this.passWord = passWord;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public boolean isAgree() {
    return agree;
  }

  public void setAgree(boolean agree) {
    this.agree = agree;
  }

}
package com.bwy.springmvc.tag.textbox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class StudentController {


  @InitBinder
  private void initBinder(WebDataBinder binder) {
    binder.setValidator(new StudentValidator());
  }


  /**
   * 我们已经在ModelAndView对象中传递了一个名称为“command”的空对象, 因为如果要在JSP中使用<form:form>标签,spring框架需要一个名称为“command”
   * 的对象文件。当调用student()方法时,它返回student.jsp视图
   * 
   * @return
   */
  @RequestMapping(value = "/student", method = RequestMethod.GET)
  public ModelAndView student() {
    return new ModelAndView("student", "command", new Student());
  }

  @ModelAttribute("student")
  public Student createStudentModel() {
    return new Student();
  }

  /**
   * 将在URL HelloWeb/addStudent 上的POST方法调用。将根据提交的信息准备模型对象。 最后,将从服务方法返回“result”视图,这将渲染result.jsp 页面。
   * 
   * @param student
   * @param map
   * @return
   */
  @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
  public String addStudent(@ModelAttribute("student") @Validated Student student,
      BindingResult bindingResult, Model map) {
    if (bindingResult.hasErrors()) {
      return "student";
    }
    System.out.println(student.isAgree());
    map.addAttribute("name", student.getName());
    map.addAttribute("age", student.getAge());
    map.addAttribute("id", student.getId());
    map.addAttribute("passWord", student.getPassWord());
    map.addAttribute("address", student.getAddress());
    map.addAttribute("agree", student.isAgree());
    map.addAttribute("favouriteSports", student.getFavoriteSports());
    map.addAttribute("gender", student.getGender());
    map.addAttribute("color", student.getFavoriteColor());
    map.addAttribute("country", student.getCountry());
    map.addAttribute("languages", student.getLanguages());
    return "result";
  }

  @ModelAttribute("sports")
  public List<String> getSports() {
    List<String> sports = new ArrayList<String>();
    sports.add("游泳");
    sports.add("足球");
    sports.add("篮球");
    sports.add("跑步");
    return sports;
  }

  @ModelAttribute("colors")
  public List<String> getColors() {
    List<String> colors = new ArrayList<String>();
    colors.add("红");
    colors.add("黄");
    colors.add("蓝");
    colors.add("绿");
    colors.add("青");
    colors.add("紫");
    colors.add("白");
    return colors;
  }

  @ModelAttribute("countries")
  public Map<String, String> getCountry() {
    Map<String, String> countryList = new HashMap<>();
    countryList.put("CH", "中国");
    countryList.put("USA", "美国");
    countryList.put("TK", "土耳其");
    countryList.put("BK", "巴基斯坦");
    return countryList;
  }

  @ModelAttribute("languages")
  public List<String> getLanguages() {
    List<String> languageList = new ArrayList<String>();
    languageList.add("汉语");
    languageList.add("英语");
    languageList.add("法语");
    languageList.add("日语");
    languageList.add("德语");
    return languageList;
  }
}
package com.bwy.springmvc.tag.textbox;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class StudentValidator implements Validator {

  @Override
  public boolean supports(Class<?> clazz) {
    return Student.class.isAssignableFrom(clazz);
  }

  @Override
  public void validate(Object obj, Errors errors) {
    ValidationUtils.rejectIfEmpty(errors, "name", null, "Field name is required.");

  }

}
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC表单之-输入框处理</title>
</head>

<body>

    <h2>学生信息</h2>
    <form:form method="POST" action="/SpringMVCTag/addStudent" commandName="student">
        <table>
            <tr>
                <td><form:label path="name">姓名:</form:label></td>
                <td><form:input path="name" /></td>
                <td><form:errors path="name"/></td>
            </tr>
            <tr>
                <td><form:label path="age">年龄:</form:label></td>
                <td><form:input path="age" /></td>
            </tr>
            <tr>
                <td><form:label path="id">编号:</form:label></td>
                <td><form:input path="id" /></td>
            </tr>
            <tr>
                <td><form:label path="passWord">密码:</form:label></td>
                <td><form:password path="passWord" /></td>
            </tr>
            <tr>
                <td><form:label path="address">地址:</form:label></td>
                <td><form:textarea path="address" rows="5" cols="30" /></td>
            </tr>

            <tr>
                <td><form:label path="agree">是否同意?</form:label></td>
                <td><form:checkbox path="agree" /></td>
            </tr>

            <tr>
                <td><form:label path="favoriteSports">喜欢的运动:</form:label></td>
                <td><form:checkboxes items="${sports}" path="favoriteSports" /></td>
            </tr>
            <tr>
                <td><form:label path="favoriteSports">性别:</form:label></td>
                <td><form:radiobutton path="gender" value="M" label="男" /> <form:radiobutton
                        path="gender" value="F" label="女" /></td>
            </tr>
            
            <tr>
                <td><form:label path="favoriteSports">喜欢的颜色:</form:label></td>
                <td><form:radiobuttons items="${colors}" path="favoriteColor" /></td>
            </tr>
            
            <tr>
                <td><form:label path="country">国家:</form:label></td>
                <td><form:select path="country" >
                    <form:option value="NONE" label="请选择..."/>
                    <form:options items="${countries }"/>
                </form:select></td>
            </tr>
            
            <tr>
                <td><form:label path="languages">语言:</form:label></td>
                <td><form:select items="${languages}" path="languages" multiple="true"/></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="提交学生信息" /></td>
            </tr>
            
        </table>
    </form:form>
</body>
</html>
<%@page import="java.io.PrintWriter"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC表单之-输入框处理</title>
</head>
<body>

    <h2>提交的学生信息如下 -</h2>
    <table>
        <tr>
            <td>姓名:</td>
            <td>${name}</td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td>${age}</td>
        </tr>
        <tr>
            <td>编号:</td>
            <td>${id}</td>
        </tr>
        <tr>
            <td>密码:</td>
            <td>${passWord}</td>
        </tr>
        <tr>
            <td>地址:</td>
            <td>${address}</td>
        </tr>
        
        <tr>
            <td>是否同意:</td>
            <td>${agree}</td>
        </tr>
        
        <tr>
            <td>喜欢的运动:</td>
            <td><%String[] favouriteSports = (String[])request.getAttribute("favouriteSports");
                for(String sprots: favouriteSports){
                  //PrintWriter pw =  response.getWriter();
                  out.println(sprots);
                }
            %></td>
        </tr>
        
        <tr>
            <td>性别:</td>
            <td>${gender == "M"?"男":"女"}</td>
        </tr>
        <tr>
            <td>喜欢的颜色:</td>
            <td>${color}</td>
        </tr>
        
        <tr>
            <td>国家:</td>
            <td>${country}</td>
        </tr>
        
        <tr>
            <td>语言:</td>
            <td><%String[] languages = (String[])request.getAttribute("languages");
                for(String language: languages){
                  //PrintWriter pw =  response.getWriter();
                  out.println(language);
                }
            %></td>
        </tr>
    </table>
</body>
</html>

 

posted @ 2017-12-25 22:22  Geek_Young  阅读(269)  评论(0编辑  收藏  举报