Spring MVC基础之表单标签库

Spring MVC基础之表单标签库

表单标签库涉及到到的标签
0、文本框和密码框
1、文本域
2、复选框
3、多项复选框
3、单选按钮
4、多项单选按钮
5、下拉框
6、列表多选项
7、隐藏字段域
8、错误处理
9、文件上传处理
使用Spring MVC创建一个项目如下:

在这里插入图片描述

编写如下代码:

Student.java

package my.model;
public class Student {
    private String name;
    private String sex;
    private String id;
    private int age;
    private String grade;
    private String school;
    private String email;
    private String hobby;
    private String advantage;
    private String education;
	//构造方法:自己补全
	//setter和getter方法:自己补全
	//toString()方法自己补全
}

StudentController.java

package my.controller;
import my.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;

@Controller
public class StudentController {

    //定位到首页:index.jsp
    @RequestMapping(value = "/index.do")
    public ModelAndView index(@ModelAttribute("student") Student student){
        return new ModelAndView("index","student",new Student());
    }

    //定位到结果打印页面:
    @RequestMapping(value = "/result.do")
    public String result(@ModelAttribute("student") Student student, ModelMap modelMap){
        modelMap.addAttribute("name",student.getName());
        modelMap.addAttribute("age",student.getAge());
        modelMap.addAttribute("id",student.getId());
        modelMap.addAttribute("sex",student.getSex());
        modelMap.addAttribute("grade",student.getGrade());
        modelMap.addAttribute("school",student.getSchool());
        modelMap.addAttribute("email",student.getEmail());
        modelMap.addAttribute("hobby",student.getHobby());
        modelMap.addAttribute("advantage",student.getAdvantage());
        modelMap.addAttribute("education",student.getEducation());
        return "/pages/result";
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--编码过滤器,防止出现乱码-->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

dispatcher-servlet.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"
       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">

    <context:component-scan base-package="my.controller"/>

    <!--映射到动态页面-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

index.jsp

<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <%
    //定义一个list_grade保存多个年级选项列表
    List<String> list_grade = new ArrayList<String>();
    list_grade.add("大一");
    list_grade.add("大二");
    list_grade.add("大三");
    list_grade.add("大四");

    //定义一个list_grade保存多个爱好的选项列表
    List<String> list_hobby = new ArrayList<String>();
    list_hobby.add("篮球");
    list_hobby.add("足球");
    list_hobby.add("羽毛球");
    list_hobby.add("铅球");
  %>


  <%--@elvariable id="student" type="my.model.Student"--%>
  <form:form action="/day0830/result.do" commandName="student" modelAttribute="student">
    姓名:<form:input path="name"/>
    性别:<form:radiobutton path="sex" value="" label="" /> <form:radiobutton path="sex" value="" label="" />
    学号:<form:input path="id"/>
    年龄:<form:select path="age">
            <form:option value="0" label="请选择..." />
            <form:option value="15" label="15" />
            <form:option value="20" label="20" />
            <form:option value="30" label="30" />
            <form:option value="40" label="40" />
            <%--如果有一个集合返回使用:<form:options items="${ageList}" />--%>
         </form:select>
    年级:<form:select path="grade" items="<%=list_grade%>" multiple="true"/>
    学校:<form:input path="school"/>
    邮箱:<form:input path="email"/>
    爱好:<form:checkboxes path="hobby" items="<%=list_hobby%>" />  <%--如果有很多项使用:<form:checkboxs path="" value="">--%>
    <%--特长为隐藏字段--%>
    <form:hidden path="advantage" value="暂无特长"/>
    教育经历:<form:textarea path="education" cols="30" rows="5"/>
  </form:form>
  </body>
</html>

result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h4>学生信息如下:</h4>
<hr>
姓名:${name}
年龄:${age}
性别:${sex}
学号:${id}
年级:${grade}
学校:${school}
邮箱:${email}
爱好:${hobby}
特长:${advantage}
教育经历:${education}
</body>
</html>
运行:

在这里插入图片描述
点击提交:
在这里插入图片描述

未完待续…

后续预告:标签库-错误处理&文件上传处理

发布了49 篇原创文章 · 获赞 38 · 访问量 8315

猜你喜欢

转载自blog.csdn.net/Mr_C_python/article/details/100084608