采用:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>设置,来使用form文本框

我的项目名称是hello, 在src/main/java下面建了一个目录chapter2: src/main/java/chapter2/

以学习姓名,年龄,id号为例。

Student.java

public class Student {
private Integer age;
private String name;
private Integer id; public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
} public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
} public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
}

  

StudentController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap; @Controller
public class StudentController { @RequestMapping(value="/student", method=RequestMethod.GET)
public ModelAndView student()
{
return new ModelAndView("student", "command", new Student()); } @RequestMapping(value="/addStudent", method=RequestMethod.POST)
public String addStudent(@ModelAttribute("SpringWeb")Student student, ModelMap model)
{
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId()); return "student_result"; } }

  

这里的第一个服务方法student(),我们已经在ModelAndView对象中传递了一个名称为“command”的空对象,因为如果要在JSP中使用<form:form>标签,spring框架需要一个名称为“command”的对象文件。当调用student()方法时,它返回student.jsp视图。

第二个服务方法addStudent()将在URL HelloWeb/addStudent 上的POST方法调用。将根据提交的信息准备模型对象。 最后,将从服务方法返回“result”视图,这将渲染result.jsp 页面。

student.jsp的代码如下所示

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Spring MVC表单处理</title>
</head>
<body> <h2>Student Information</h2>
<form:form method="post" action="/hello/addStudent">
<table>
<tr>
<td><form:label path="name">姓名:</form:label></td>
<td><form:input 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 colspan="2">
<input type="submit" value="提交表单"/>
</td>
</tr>
</table>
</form:form> </body>
</html>

  student_result.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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>
</table> </body>
</html>

  

注意加入jstl标签库,否则不会解析的。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false" %>

  

spring mvc:文本框的更多相关文章

  1. Spring MVC文本框

    以下示例显示如何使用Spring Web MVC框架在表单中使用文本框.首先使用Eclipse IDE来创建一个Web工程,按照以下步骤使用Spring Web Framework开发基于动态表单的W ...

  2. Spring MVC文本域

    以下示例显示如何在使用Spring Web MVC框架的表单中使用文本域(TextArea).首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Framewo ...

  3. spring mvc: 密码框

    以user为例,包含username, password字段. user.java public class User { private String username; private Strin ...

  4. spring mvc:常用标签库(文本框,密码框,文本域,复选框,单选按钮,下拉框隐藏于,上传文件等)

    在jsp页面需要引入:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form&q ...

  5. Spring MVC-表单(Form)标签-文本框(Text Box)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_textbox.htm 说明:示例基于Spring MVC 4.1.6. 以下示例 ...

  6. 用MVC的辅助方法自定义了两个控件:“可编辑的下拉框控件”和“文本框日历控件”

    接触MVC也没多长时间,一开始学的时候绝得MVC结构比较清晰.后来入了门具体操作下来感觉MVC控件怎么这么少还不可以像ASP.net form那样拖拽.这样设计界面来,想我种以前没学过JS,Jquer ...

  7. MVC动态添加文本框,后台使用FormCollection接收

    在"MVC批量添加,增加一条记录的同时添加N条集合属性所对应的个体"中,对于前台传来的多个TextBox值,在控制器方法中通过强类型来接收.使用FormCollection也可以接 ...

  8. spring mvc:复选框(多选)

    以user为例,user下有 username用户,password密码, address地址, receivePaper是否订阅, favotireFramework兴趣爱好, user.java ...

  9. Spring MVC列表多选框

    以下示例显示如何在使用Spring Web MVC框架的表单中使用列表框(Listbox).首先使用Eclipse IDE来创建一个WEB工程,实现一个让用户可选择自己所善长的技术(多选)的功能.并按 ...

随机推荐

  1. qmake make install

    一般的qmake生成的Makefile是没有 make install的 方法: 在.pro中做文章 比如你要安装libEbookDataBase.so*到目录 /usr/local/lib .pro ...

  2. IO 流的操作基本规律

    想要知道开发时,使用哪个流对象, 只要通过四个明确即可. 明确源和目的(数据汇) 源: InputStream 或 Reader 目的: OutPutStream 或 Writer 明确数据是否是纯文 ...

  3. 剑指Offer——斐波那契数列

    题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.n<=39 分析: 递归解法肯定相当耗时. 因为当n=4时,程序是这样子递归运算的:Fibonacci( ...

  4. 转!!配置Tomcat时server.xml和content.xml自动还原问题

    原博文地址:http://www.cnblogs.com/zuosl/p/4342190.html 当我们在处理中文乱码或是配置数据源时,我们要修改Tomcat下的server.xml和content ...

  5. mysql 中sum (if()) 用法

    原表: id    fenlei     time 1      分类1      20130316 2      分类2      20130316 3      分类3      20130317 ...

  6. use html5 video tag with MSE for h264 live streaming

    本编博客记录桌面虚拟化移动端预研. 完整demo: https://github.com/MarkRepo/wfs.js 常见的直播方案有RTMP RTSP HLS 等等, 由于这些流都需要先传输到服 ...

  7. 简明python教程二-----对象

    物理行:是你在编写程序时所看见的. 逻辑行:是Python看见的单个语句. 默认的,python希望每行都只使用一个语句,这样使得代码更加易读. 如果你想要在一个物理行中使用多于一个逻辑行,用分号“: ...

  8. python识别一段由字母组成的字符串是拼音还是英文单词

    环境:win10 python3.6 先说一下算法思想: 首先建立本地拼音库(不带声调).使用贪婪算法将字符串从左向右扫描,将字符串与本地拼音库(这里提供给大家一个)进行匹配,当发现匹配成功时继续扫描 ...

  9. boost noncopyable类

    1. 当声明一个类时,编译器会自动为该类生成默认构造函数,复制构造函数,赋值操作符以及析构函数: 2.自动生成的各个函数和操作符都是public的: 3.当声明一个类不允许复制时,可以将一个类的复制构 ...

  10. url监控

    #!/usr/bin/env python #coding:utf-8 import MySQLdb,requests import time from datetime import datetim ...