Spring MVC-表单(Form)标签-隐藏字段(Hidden Field)示例(转载实践)

本文基于Spring MVC 4.1.6,介绍使用Spring Web MVC框架在窗体中使用隐藏字段的方法。详细说明了使用Eclipse IDE开发基于动态窗体的Web应用程序的步骤,包括创建项目、Java类、视图文件等,还给出了相关代码示例及运行结果展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明:示例基于Spring MVC 4.1.6。

以下示例显示如何使用Spring Web MVC框架在窗体中使用隐藏字段。首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态窗体的Web应用程序:

步骤 描述
1 创建一个名为HelloWeb的项目,在一个包com.tutorialspoint下,如Spring MVC - Hello World Example章节所述。
2 在com.tutorialspoint包下创建一个Java类Student,StudentController。
3 在jsp子文件夹下创建一个视图文件student.jsp,result.jsp。
4 最后一步是创建所有源和配置文件的内容并导出应用程序,如下所述。
Student.java

复制代码
package com.tutorialspoint;

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.tutorialspoint;

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对象中传递了一个空白的Student对象,名称为“command”,因为如果您在JSP中使用form:form标签,Spring框架会期望一个名为“command”的对象文件。所以当调用student()方法时,返回student.jsp视图。

将在HelloWeb/addStudent URL 上针对POST方法调用第二个服务方法addStudent()。您将根据提交的信息准备您的模型对象。最后,将从服务方法返回“result”视图,这将导致渲染result.jsp

student.jsp

复制代码
<%@taglib uri=“http://www.springframework.org/tags/form” prefix=“form”%>

Spring MVC Form Handling

Student Information

Name
Age
复制代码 这里我们使用标签来呈现HTML隐藏字段。例如

<form:hidden path=“id” value=“1”/>
它将呈现以下HTML内容。

result.jsp中

复制代码
<%@taglib uri=“http://www.springframework.org/tags/form” prefix=“form”%>

Spring MVC Form Handling

Submitted Student Information

Name${name}
Age${age}
ID${id}
复制代码 完成创建源文件和配置文件后,导出应用程序。右键单击应用程序并使用Export->WAR File选项,并将您的HelloWeb.war文件保存在Tomcat的webapps文件夹中。

现在启动您的Tomcat服务器,并确保您可以使用标准浏览器从webapps文件夹访问其他网页。现在尝试URL http://localhost:8080/HelloWeb/student,如果Spring Web应用程序的一切都很好,您应该看到以下结果:

提交所需信息后,点击提交按钮提交表单。如果您的Spring Web应用程序的一切都很好,您应该会看到以下结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值