以下示例显示如何在使用spring web mvc框架的表单中使用隐藏字段(hidden)。首先使用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
”的空student
对象,因为如果在jsp文件中使用<form:form>
标签,spring框架需要一个名称为“command
”的对象。 所以当调用student()
方法时,它返回student.jsp
视图。
第二个服务方法addstudent()
将根据url => hiddenfield/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="/hiddenfield/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></td>
<td><form:hidden path="id" value="1000" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
这里使用<form:hidden />
标签来呈现html隐藏字段域。 例如 -
<form:hidden path="id" value="1000"/>
它将呈现以下html内容。
<input id="id" name="id" type="hidden" value="1000"/>
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/hiddenfield/student ,如果spring web应用程序没有问题,应该看到以下结果:
提交所需信息后,点击提交按钮提交表单。 如果spring web应用程序没有问题,应该看到以下结果: