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工程,实现一个让用户可选择自己所善长的技术(多选)的功能.并按 ...
随机推荐
- [转] C/C++中printf和C++中cout的输出格式
原文地址 一. Printf 输出格式 C中格式字符串的一般形式为: %[标志][输出最小宽度][.精度][长度]类型,其中方括号[]中的项为可选项.各项的意义介绍如下:1.类型类型字符用以表示输出数 ...
- 使用webpack构建本地服务器
想不想让你的浏览器监听你的代码的修改,并自动刷新显示修改后的结果,其实Webpack提供一个可选的本地开发服务器,这个本地服务器基于node.js构建,可以实现你想要的这些功能,不过它是一个单独的组件 ...
- web开发者的博客
一. Jerry Qu https://imququ.com/post/sth-about-switch-to-https-2.html 点评:博客做的比较好,样式不错. http知识比较深.
- C# 代码生成器 (存储过程生成方法)
最近研究C#,发现写代码很麻烦,特别是创建实体.模型.接口.工厂.存储过程之类,网上有些开源的,但是不完整,也不能完整的按需求生成,于是,花了三天的时候,结合前辈的一些经验,整合了一些代码生成器,以下 ...
- Json杂谈系列------(一)初始json
1. JSON 是什么 JSON,全称是 JavaScript Object Notation,即 JavaScript 对象标记法.这是一种轻量级(Light-Weight).基于文本的(Text- ...
- Windows右击无新建文本文档怎么办
右击无新建文本文档2008-07-26 16:51 刚在网上找的,在运行项输入notepad,把下面的复制进去,然后保存为123.reg,双击导入. REGEDIT4 [HKEY_CLASSES_RO ...
- web 前端 常见操作 将时间戳转成日期格式 字符串截取 使用mui制作选项卡
1.将时间戳转成日期格式: //第一种 function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString( ...
- C语言循环中降低推断——————【Badboy】
为了让编译器更好地优化循环,应该尽量让循环中降低推断,方法之中的一个是将推断语句整合进表达式.还是这个样例: for (int i = 0; i < 1000*10; i++) { sum += ...
- css样式布局中position的那些事儿
哎,页面布局及设计开发.对于一个一直从事后台开发来说屌丝来说,确实是件非常费时.费力,非常艰难的一件事. 今晚是想实现把多张重叠在一起.或是标记一张图片中不同的位置然后赋以超链接.花了一晚上的时间,才 ...
- HTML5事件-pageshow 和 pagehide
<!doctype html> <html> <head> <title>html5事件</title> <meta charset= ...