采用:<%@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. Kafka — 高吞吐量的分布式发布订阅消息系统【转】

    1.Kafka独特设计在什么地方?2.Kafka如何搭建及创建topic.发送消息.消费消息?3.如何书写Kafka程序?4.数据传输的事务定义有哪三种?5.Kafka判断一个节点是否活着有哪两个条件 ...

  2. 《深入理解Linux网络技术内幕》阅读笔记 --- 路由查找

    概述 1.不论是入口还是出口流量,都是利用fib_lookup来查找路由表,fib_lookup是对每一个路由表所提供的查找函数的包裹函数,当不支持策略路由时,查找函数版本针对的是local表和mai ...

  3. Python(进程池与协程)

    1.进程池与线程池: 为什么要用“池”:池子使用来限制并发的任务数目,限制我们的计算机在一个自己可承受的范围内去并发地执行任务 池子内什么时候装进程:并发的任务属于计算密集型 池子内什么时候装线程:并 ...

  4. 我的Android进阶之旅------>Java全角半角的转换方法

    一中文全角和半角输入的区别 1全角指一个字符占用两个标准字符位置 2半角指一字符占用一个标准的字符位置 3全角与半角各在什么情况下使用 4全角和半角的区别 5关于全角和半角 6全角与半角比较 二转半角 ...

  5. x265探索与研究(六):main()函数

    x265探索与研究(六):main()函数 x265源代码的入口函数是main(),本文分析main()的主要功能. 首先给出main()函数的功能及其代码结构:其次给出main()函数源代码以及分析 ...

  6. Objective-C学习笔记(五)——数据类型与限定词

    如同其它不论什么的编程语言一样,想要更深入的学习.必需要了解该门语言的数据类型与限定词. OC的数据类型例如以下: (1)int:整型:   int a;      int b=2;     int ...

  7. Java Concurrent happens-before

    happens-before relation on memory operations such as reads and writes of shared variables. The resul ...

  8. Android--Apache HttpClient(2种实现)

    前言 上一篇文章介绍了使用HttpURLConnection来完成对于HTTP协议的支持,现在介绍一个新的方式来访问Web站点,那就是HttpClient. HttpClient是Apache开源组织 ...

  9. ajax请求真实服务器数据示例

    // http://study.163.com/webDev/couresByCategory.htm // http://study.163.com/webDev/couresByCategory. ...

  10. Nhibernate工具Profiler配置

    1.使用之前需要确认Framework的Version,如果是4.0那么使用如下程序集需要在 创建ISessionFactory的项目中引用NHProfiler安装目录下的 HibernatingRh ...