• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Spring如何处理表单提交

java 搞代码 4年前 (2022-01-09) 25次浏览 已收录 0个评论

今天我们来讲一个最简单的表单提交处理的例子,通过提交一个表单给朋友打一声招呼!

看这边文章之前,你至少应该了解基于Spring的Web开发的基础知识,当然,你还是应该准备好开发环境:

  • IDE+Java环境(JDK 1.7或以上版本)
  • Maven 3.0+(Eclipse和Idea IntelliJ内置,如果使用IDE并且不使用命令行工具可以不安装)

准备POM文件

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.tianmaying</groupId>
 <artifactId>springboot-form-submission-demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>springboot-form-submission-demo</name>
 <description>Springboot form submission demo</description>

 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.2.5.RELEASE</version>
 <relativePath/>
 </parent>

 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <java.version>1.8</java.version>
 </properties>

 <dependen<strong>本文来源gaodai#ma#com搞@@代~&码网</strong>cies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>

 <build>
 <plugins>
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 </plugins>
 </build>

</project>

创建Controller

我们已经知道可以通过Controller来进行URL路由,Spring WebMvc框架会将Servlet容器里收到的HTTP请求根据路径分发给对应的@Controller类进行处理、而 @RequestMapping注解表明该方法处理那些URL对应的HTTP请求。

我们的SayHelloController的代码如下:

package com.tianmaying.springboot.formsubmission;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class SayHelloController {

 @RequestMapping(value="/sayhello", method=RequestMethod.GET)
 public String sayHelloForm(Model model) {
 model.addAttribute("helloMessage", new HelloMessage());
 return "sayhello";
 }

 @RequestMapping(value="/sayhello", method=RequestMethod.POST)
 public String sayHello(@ModelAttribute HelloMessage helloMessage, Model model) {
 model.addAttribute("helloMessage", helloMessage);
 return "message";
 }

}
  • 针对/sayhelloGET请求,我们返回提交表单的页面,即sayHello.html
  • 针对/sayhelloPOST请求,我们进行表单的处理,然后将打招呼的信息渲染到message.html页面返回。

表单处理也无外乎这两件事情:显示表单,处理表单提交。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring如何处理表单提交

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址