以下示例显示如何使用spring web mvc框架在表单中使用文本框。首先使用eclipse ide来创建一个web工程,按照以下步骤使用spring web framework开发基于动态表单的web应用程序:
com.h3.springmvc
包下创建java类student
,studentcontroller
。jsp
子文件夹下创建一个视图文件student.jsp
,result.jsp
。完整的工程结构如下图所示 -
student.java 的代码如下所示 -
package com.h3.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.h3.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应用程序没有问题,应该看到以下结果: