Spring MVC文本框
以下示例显示如何使用Spring Web MVC框架在表单中使用文本框。首先使用Eclipse IDE来创建一个Web工程,按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:
- 创建一个名称为 TextBox 的项目。
- 在
com.yiibai.springmvc
包下创建Java类Student
,StudentController
。 - 在
jsp
子文件夹下创建一个视图文件student.jsp
,result.jsp
。 - 最后一步是创建所有源和配置文件的内容并运行应用程序,如下所述。
完整的工程结构如下图所示 -
Student.java 的代码如下所示 -
package com.yiibai.springmvc;
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 的代码如下所示 -
package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
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;
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 "result";
}
}
这里的第一个服务方法student()
,我们已经在ModelAndView
对象中传递了一个名称为“command
”的空对象,因为如果要在JSP中使用<form:form>
标签,spring框架需要一个名称为“command
”的对象文件。当调用student()
方法时,它返回student.jsp
视图。
第二个服务方法addStudent()
将在URL HelloWeb/addStudent
上的POST方法调用。将根据提交的信息准备模型对象。 最后,将从服务方法返回“result
”视图,这将渲染result.jsp
页面。
student.jsp的代码如下所示 -
<%@ 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="/TextBox/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>
这里使用<form:input />
标签来渲染一个HTML文本框。示例 -
<form:input path="name" />
它将生成以下HTML
内容。
<input id="name" name="name" type="text" value=""/>
result.jsp的代码如下所示 -
<%@ 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>
</table>
</body>
</html>
完成创建源和配置文件后,发布到Tomcat服务器并运行应用程序。
现在启动Tomcat服务器,并尝试URL => http://localhost:8080/TextBox/student, 如果Spring Web应用程序的没有问题,应该看到以下结果:
提交所需信息后,点击提交按钮提交表单。 如果Spring Web应用程序没有问题,应该看到以下结果:
Spring MVC文本框的更多相关文章
- spring mvc:文本框
采用:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> ...
- Spring MVC文本域
以下示例显示如何在使用Spring Web MVC框架的表单中使用文本域(TextArea).首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Framewo ...
- spring mvc: 密码框
以user为例,包含username, password字段. user.java public class User { private String username; private Strin ...
- spring mvc:常用标签库(文本框,密码框,文本域,复选框,单选按钮,下拉框隐藏于,上传文件等)
在jsp页面需要引入:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form&q ...
- Spring MVC-表单(Form)标签-文本框(Text Box)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_textbox.htm 说明:示例基于Spring MVC 4.1.6. 以下示例 ...
- 用MVC的辅助方法自定义了两个控件:“可编辑的下拉框控件”和“文本框日历控件”
接触MVC也没多长时间,一开始学的时候绝得MVC结构比较清晰.后来入了门具体操作下来感觉MVC控件怎么这么少还不可以像ASP.net form那样拖拽.这样设计界面来,想我种以前没学过JS,Jquer ...
- MVC动态添加文本框,后台使用FormCollection接收
在"MVC批量添加,增加一条记录的同时添加N条集合属性所对应的个体"中,对于前台传来的多个TextBox值,在控制器方法中通过强类型来接收.使用FormCollection也可以接 ...
- spring mvc:复选框(多选)
以user为例,user下有 username用户,password密码, address地址, receivePaper是否订阅, favotireFramework兴趣爱好, user.java ...
- Spring MVC列表多选框
以下示例显示如何在使用Spring Web MVC框架的表单中使用列表框(Listbox).首先使用Eclipse IDE来创建一个WEB工程,实现一个让用户可选择自己所善长的技术(多选)的功能.并按 ...
随机推荐
- Chrome好用的扩展插件
LastPassChrome上最好用的密码管理软件,支持密码自动填充.生成密码
- CSS3:3D转换
几个突破口:(为了更简洁理解,先忽略兼容) 1.认识3D的坐标系 rotateX()-----------元素绕X轴旋转 rotateY() -----------元素绕Y轴旋转 rotateZ() ...
- npm的简单应用
1.安装node 2.旧版node升级 (1)linux系统 sudo npm install npm -g (2)window系统 npm install npm -g 3.安装淘宝镜像cnpm(以 ...
- 11、Pickle序列化
概念: 常用语法:DUMP:把现在内存中的对象状态装到硬盘文件上 常用语法:LOAD:把磁盘文件中的对象导入到内存中 小练习: 字典中存账号信息,用pickle dump到文件中,并load进行修 ...
- win8 下脚本安装IIS
@echo off echo 正在添加IIS8.0 功能,依据不同的网络速率,全程大约需要5分钟时间... start /w pkgmgr /iu:IIS-WebServerRol ...
- django admin富文本编辑kindeditor
最近在做django项目,需要在后台管理系统加入富文本编辑 其实加入富文本编辑很简单,就是导入几个编辑器的js脚本到admin页面内,下面说说怎么做 第一步,下载想要的富文本编辑器如kindedito ...
- cinemachine unity
scrips extending timeline Extending Timeline with your own playables Data Audio ----playable beh ...
- 怎样在caffe中添加layer以及caffe中triplet loss layer的实现
关于triplet loss的原理.目标函数和梯度推导在上一篇博客中已经讲过了.详细见:triplet loss原理以及梯度推导.这篇博文主要是讲caffe下实现triplet loss.编程菜鸟.假 ...
- ubuntu中wifi显示被硬件禁用的解决方法
本人使用的电脑是华硕X550C,安装了ubuntu16.04版本. 联网的时候显示“wifi已经通过硬件开关禁用”.按Fn+F2无法开启wifi.通过rfkill命令无法也无法开启wifi. 经过了解 ...
- Oracle Database Link 的创建和使用小见
假设:需要从数据库db_a通过db_link连接到db_b查询数据库b的部分相关信息 前提条件: 数据库a账户需要有创建dblink的权限,如果没有可以使用dba账户赋权限 grant CREATE ...