欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用MVC的JSP表单标签库处理表单

程序员文章站 2022-06-01 22:50:23
...

JSP表单标签库的作用

(1)创建视图。类似于html中表单的作用,可以用mvc的表单标签库来创建视图,获取用户数据。

(2)与模型类的绑定功能。可以便捷地将HTTP请求的参数映射到模型中。

配置表单标签库

需要在jsp页面加一句:

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

 

常用的几个标签

(1)表单标签

<mvc:form modelAttribute=”user” action = “result” ></mvc:form>

创建一个表单

ModelAttribute参数设置要绑定的bean实例

Action设置表单提交后访问的资源url

(2)输入元素

<mvc:intput path=”name” />

<mvc:textarea path=”detail” />

<mvc:password path=”password” />

注意的是 path为要绑定的bean的属性名称

(3)下拉框

<mvc:select path=“country” items=”${countries}” />

其中path是绑定的bean的属性

Items可以是待选择的集合,数字等等

(4)单选按钮

<mvc:radiobutton path=”gender” value=”F” />

<mvc:radiobutton path=”gender” value=”M” />

Path为绑定的bean属性

Value为待选的值

(5)复选框

<mvc:checkbox path=”isSmoking” />

(6)添加标签

<mvc:label path=”name”>Name</mvc:label>

(7)放置按钮

<mvc:button>Submit</mvc:button>

 

用户注册表单实例

过程

用户输入http://localhost:8080/form -> 得到表单页面并输入个人信息 –> 提交表单并自动访问 http://localhost:8080/result -> 展示用户输入的个人信息

步骤

新建springMVC项目,具体过程见链接https://blog.csdn.net/weixin_42518668/article/details/103594144

创建User类来绑定输入信息

目录结构

使用MVC的JSP表单标签库处理表单

 

package com.beginning.mvc.dao;



public class User {

    private String name;

    private String lastname;

    private String password;

    private String detail;

    private String birthDate;

    private String gender;

    private String country;

    private boolean isSmoking;



    public String getBirthDate() {

        return birthDate;

    }



    public String getCountry() {

        return country;

    }



    public String getDetail() {

        return detail;

    }



    public String getGender() {

        return gender;

    }



    public String getLastname() {

        return lastname;

    }



    public String getName() {

        return name;

    }



    public String getPassword() {

        return password;

    }



    public boolean isSmoking() {

        return isSmoking;

    }



    public void setBirthDate(String birthDate) {

        this.birthDate = birthDate;

    }



    public void setCountry(String country) {

        this.country = country;

    }



    public void setDetail(String detail) {

        this.detail = detail;

    }



    public void setGender(String gender) {

        this.gender = gender;

    }



    public void setLastname(String lastname) {

        this.lastname = lastname;

    }



    public void setName(String name) {

        this.name = name;

    }



    public void setPassword(String password) {

        this.password = password;

    }



    public void setSmoking(boolean smoking) {

        isSmoking = smoking;

    }

}

创建对应的枚举类Gender

package com.beginning.mvc.dao;



public enum Gender {

    MALE,FEMALE;

}

创建Controller

package com.beginning.mvc.controller;



import com.beginning.mvc.dao.Gender;

import com.beginning.mvc.dao.User;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;



@Controller

public class HelloReaderController {

    private static final String[] countries = {"China","US","Germany"};

       @RequestMapping(value = "/form")

    public ModelAndView user(){

        ModelAndView mv = new ModelAndView("userForm","user",new User());

        mv.addObject("country",countries);

        mv.addObject("genders",Gender.values());

        return mv;

    }

    @RequestMapping(value = "/result")

    public ModelAndView processUser(User user){

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("userResult");

        modelAndView.addObject("u",user);

        return modelAndView;

    }

}

 

注意user()方法和processUser()方法分别是处理上述的第一次和第二次的请求

在/WEB-INF/pages文件夹下创建userForm.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

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

<html>

<head>

    <title>Title</title>

</head>

<body>

<h2>Spring MVC form Handling</h2>

<mvc:form modelAttribute="user" action="result">

    <table>

        <tr>

            <td><mvc:label path="name">name</mvc:label></td>

            <td><mvc:input path="name"/></td>

        </tr>

        <tr>

            <td><mvc:label path="lastname">Last name</mvc:label></td>

            <td><mvc:input path="lastname"/></td>

        </tr>

        <tr>

            <td><mvc:label path="password">Password</mvc:label></td>

            <td><mvc:input path="password"/></td>

        </tr>

        <tr>

            <td><mvc:label path="detail">Detail</mvc:label></td>

            <td><mvc:textarea path="detail"/></td>

        </tr>

        <tr>

            <td><mvc:label path="birthDate">BirthDate</mvc:label></td>

            <td><mvc:input path="birthDate"/></td>

        </tr>

        <tr>

            <td><mvc:label path="gender">Gender</mvc:label></td>

            <td><mvc:radiobuttons path="gender" items="${genders}"/></td>

        </tr>

        <tr>

            <td><mvc:label path="country">Country</mvc:label></td>

            <td><mvc:select path="country" items="${country}"/></td>

        </tr>

        <tr>

            <td><mvc:label path="smoking">IsSmokong</mvc:label></td>

            <td><mvc:checkbox path="smoking"/></td>

        </tr>

        <tr>

            <td colspan="2"><input type="submit" value="Submit"></td>

        </tr>

    </table>

</mvc:form>

</body>

</html>

在/WEB-INF/pages文件夹下创建userResult.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

<h2>Spring MVC Form Handling</h2>

<table>

    <tr>

        <td>Name</td>

        <td>${u.name}</td>

    </tr>

    <tr>

        <td>Last Name</td>

        <td>${u.lastname}</td>

    </tr>

    <tr>

        <td>Password</td>

        <td>${u.password}</td>

    </tr>

    <tr>

        <td>Detail</td>

        <td>${u.detail}</td>

    </tr>

    <tr>

        <td>Birth Date</td>

        <td>${u.birthDate}</td>

    </tr>

    <tr>

        <td>Gender</td>

        <td>${u.gender}</td>

    </tr>

    <tr>

        <td>Country</td>

        <td>${u.country}</td>

    </tr>

    <tr>

        <td>IsSmoking</td>

        <td>${u.smoking}</td>

    </tr>

</table>

</body>

</html>

部署服务器,访问http://localhost:8080/form ,输入信息后提交得到结果

使用MVC的JSP表单标签库处理表单

使用MVC的JSP表单标签库处理表单