SpringBoot动态修改form标签的提交方式

SpringBoot中动态修改form标签的method提交方式:

      请先阅读下面的带有thymeleaf的jsp代码,提交有Restful风格,该页面是将员工添加和修改页面都合在一起用(工作中不建议这样用,耦合度高):

<!--需要区分是员工修改还是添加;-->
<form th:action="@{/emp}" method="post">
	<!--发送put请求修改员工数据-->
	<!--
	1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自动配置好的)
	2、页面创建一个post表单
	3、创建一个input项,name="_method";值就是我们指定的请求方式
	-->
	<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
	<input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">
	<div class="form-group">
		<label>LastName</label>
		<input name="lastName" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${emp.lastName}">
	</div>
	<div class="form-group">
		<label>Email</label>
		<input name="email" type="email" class="form-control" placeholder="zhangsan@atguigu.com" th:value="${emp!=null}?${emp.email}">
	</div>
	<div class="form-group">
		<label>Gender</label><br/>
		<div class="form-check form-check-inline">
			<input class="form-check-input" type="radio" name="gender" value="1" th:checked="${emp!=null}?${emp.gender==1}">
			<label class="form-check-label"></label>
		</div>
		<div class="form-check form-check-inline">
			<input class="form-check-input" type="radio" name="gender" value="0" th:checked="${emp!=null}?${emp.gender==0}">
			<label class="form-check-label"></label>
		</div>
	</div>
	<div class="form-group">
		<label>department</label>
		<!--提交的是部门的id-->
		<select class="form-control" name="department.id">
			<option th:selected="${emp!=null}?${dept.id == emp.department.id}" th:value="${dept.id}" th:each="dept:${depts}" th:text="${dept.departmentName}">1</option>
		</select>
	</div>
	<div class="form-group">
		<label>Birth</label>
		<input name="birth" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}">
	</div>
	<button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button>
</form>
问题

      如上

<button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button>

emp!=null即为修改页面否则为添加页面,由Restful API风格知添加功能的页面时应该使用Post请求,修改功能的页面时使用Put请求,而此处又是添加、修改重用一个页面需要动态修改<from> 标签的method提交方式。
;

答案

在这里插入图片描述
在这里插入图片描述
     SpringMVC中配置HiddenHttpMethodFilter类(SpringBoot自动配置好的),里面定义了 public static final String DEFAULT_METHOD_PARAM = "_method"大概意思是将提交method方式改为参数为_method内标签值。

如最上面的代码原本的

	<form th:action="@{/emp}" method="post">
	<!--发送put请求修改员工数据-->
	<!--
	1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自动配置好的)
	2、页面创建一个post表单
	3、创建一个input项,name="_method";值就是我们指定的请求方式
	-->
	<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
	
	<!-- 此处省略中间代码 -->
	
	<button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button>

</form>

原本的<form>的method是post提交方式,而当emp!=null时带有name="_method" value="put"的input标签<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>会并被SpringBoot自动配置所检查到,将<form>的method的提交方式改为value="put"的对应值“put”,即此时<form>的method是put提交方式。

### 回答1: Spring Boot可以很方便地处理表单提交。以下是一些基本的步骤: 1. 在Controller中定义一个处理表单提交方法,使用@PostMapping注解。 2. 在方法中使用@ModelAttribute注解来绑定表单数据到一个对象中。 3. 在方法中处理表单数据,可以使用@RequestParam注解来获取单个表单字段的值,或者使用@RequestBody注解来获取整个表单数据的JSON格式。 4. 在方法中返回一个视图或者重定向到另一个页面。 5. 在HTML页面中使用<form>标签来定义表单,使用<input>标签来定义表单字段。 6. 在HTML页面中使用Thymeleaf等模板引擎来渲染表单和处理表单提交后的结果。 以上是Spring Boot处理表单提交的基本步骤,具体实现可以根据具体需求进行调整和扩展。 ### 回答2: Spring Boot是Spring框架的顶层封装,它对于项目的构建和部署有着非常大的作用。在实际的开发中,我们经常需要处理form表单提交的请求,本文将介绍如何使用Spring Boot来进行form表单提交的处理。 1. 添加依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <!--如果使用thymeleaf--> <!--如果使用其他模板引擎,则换成对应的依赖即可--> </dependency> ``` 2. 编写Controller 在Spring Boot中,可以通过@Controller注解来定义Controller类,其中的各种方法可以用来处理不同的请求。对于form表单提交,我们可以编写类似如下的代码: ```java @Controller public class FormController { @GetMapping("/form") public String showForm(Model model) { model.addAttribute("form", new Form()); return "form"; } @PostMapping("/submitForm") public String submitForm(@ModelAttribute("form") Form form) { //处理form表单提交后的逻辑 return "result"; } } ``` 在上面的代码中,showForm方法用来处理GET请求,并将一个空的Form对象添加到模型中。submitForm方法用来处理POST请求,它将form表单提交后的数据封装到Form对象中,并返回result视图。 3. 编写视图 在Spring Boot中,可以通过模板引擎来渲染视图。这里以Thymeleaf为例,编写一个form.html视图: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Form</title> </head> <body> <form th:object="${form}" th:action="@{/submitForm}" method="post"> <label> Name: <input type="text" th:field="*{name}"/> <span th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</span> </label> <label> Age: <input type="number" th:field="*{age}"/> <span th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Age Error</span> </label> <button type="submit">Submit</button> </form> </body> </html> ``` 在视图中,我们使用了Thymeleaf的语法来渲染一个form表单,并使用th:field属性将表单项绑定到Form对象的属性上。在input标签中,我们使用了*{name}和*{age}的写法来引用Form对象的属性,这样一来,表单项的值就会自动绑定到Form对象的属性上。同时,我们还使用了Thymeleaf的表单验证功能,当表单项的值不符合要求时,会自动提示错误信息。 4. 运行项目 完成以上步骤后,我们可以启动Spring Boot项目,并访问/form路径,即可看到一个form表单。在表单中填写数据并点击提交按钮后,就会触发submitForm方法,并跳转到result视图。 Spring Boot的form表单提交处理,需要注意以下几个方面: - POST请求需要使用@PostMapping注解来处理; - 表单项需要使用th:field属性来绑定到Form对象的属性上; - 表单验证可以使用Thymeleaf的语法来实现。 ### 回答3: Spring Boot是一款集成了许多方便特性的JavaWeb框架,其中包括了处理表单提交的功能。Spring Boot表单提交采用了注解的方式,使得表单提交非常方便,而且可以快速实现表单周期。 Spring Boot表单的核心是使用@Controller注解来标记处理请求的方法。在这些方法中,可以使用@RequestParam注解来接收表单中的参数。例如: ```java @Controller public class MyController { @RequestMapping("/submitForm") public String handleSubmitForm(@RequestParam("name") String name, @RequestParam("email") String email) { // 处理表单数据 return "resultPage"; } } ``` 在上述代码中,我们使用@RequestMapping注解将方法映射到处理表单提交的URL。它包括了两个@RequestParam注解,表示需要获取表单中的'name'和'email'参数。 接收表单提交后,我们可以使用这些参数来执行表单操作。最后,我们返回一个视图,显示处理的结果。 除了@RequestParam注解外,Spring Boot中还提供了其他注解来处理表单提交,如@ModelAttribute注解,该注解可以将表单数据映射到一个Java对象中。例如: ```java @Controller public class MyController { @RequestMapping("/submitForm") public String handleSubmitForm(@ModelAttribute("user") User user) { // 处理表单数据 return "resultPage"; } } ``` 在上述代码中,我们使用@ModelAttribute注解来将表单数据映射到User对象中。这就意味着,我们不需要再手动获取表单所有参数,而是可以直接使用User对象来访问所有参数。 除了以上的方式,Spring Boot还可以使用@Valid注解来验证表单数据的有效性,防止表单数据不完整或格式错误。我们只需要在User对象上加上@Valid注解,然后在处理表单数据的方法中加上BindingResult参数即可。 总的来说,Spring Boot提供了非常便捷易用的方式来处理表单提交,使得我们可以快速实现Web应用中的表单操作。同时,通过注解的方式,我们也可以避免繁琐的表单参数处理操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值