SpringMVC的form表单处理

针对前端传送过来的一个表单中的数据如果后台对应的action方法中没有跟form表单组成的实体相同名称的入参;那么就会报出异常:

先上一段前端form表单对应的一个action方法,注意其中没有任何的入参;

@RequestMapping("/profile")
public String displayProfile() {
  return "profile/profilePage";
}

前端代码如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="layout/default">
<head lang="en">
    <title>Your profile</title>
</head>
<body>
<div class="row" layout:fragment="content">
    <h2 class="indigo-text center">Personal info</h2>
    <form th:action="@{/profile}" method="post" th:object="${profileForm}" class="col m8 s12 offset-m2">
        <div class="row">
            <div class="input-field col s6">
                <input  th:filed="${profileForm.twitterHandle}" id="twitterHandle" type="text"/>
                <label for="twitterHandle">Twitter handle</label>
            </div>
            <div class="input-field col s6">
                <input  th:field="${profileForm.email}" id="email" type="email"/>
                <label for="email">Email</label>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s6">
                <input th:field="${profileForm.birthDate}" id="birthDate" type="text"/>
                <label for="birthDate">Birth Date</label>
            </div>
        </div>
        <div class="row s12">
            <button class="btn waves-effect waves-light" type="submit" name="save">Submit
                <i class="mdi-content-send right"></i>
            </button>
        </div>
    </form>
</div>
</body>
</html>

运行并访问应用回展示如下的前端错误。

后端会报:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "profile", template might not exist or might not be accessible by any of the configured Template Resolvers

仔细查看了一下是由于自己没有在controler方法中加入请求参数,将action方法变为如下即可通过

@RequestMapping("/profile")
    public String displayProfile(ProfileForm profileForm) {        
        return "profile/profilePage";
    }

以上就是动力节点小编介绍的"SpringMVC的form表单处理",希望对大家有帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMVC中,表单的处理是非常常见的操作。而SpringMVC提供了form:标签库来简化表单的处理,使得表单的处理更加方便和简单。 使用form:标签库,需要在JSP页面中引入form标签库的命名空间,如下所示: ``` <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> ``` 引入命名空间后,就可以使用form标签库提供的标签来处理表单了。比如,使用form:form标签创建一个表单,如下所示: ``` <form:form modelAttribute="user" method="post" action="/user/save"> <table> <tr> <td><form:label path="username">Username:</form:label></td> <td><form:input path="username" /></td> </tr> <tr> <td><form:label path="password">Password:</form:label></td> <td><form:password path="password" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit" /></td> </tr> </table> </form:form> ``` 在上面的代码中,form:form标签用来创建一个表单,其中modelAttribute属性用来指定表单数据绑定到的对象,method属性用来指定表单提交时使用的HTTP方法,action属性用来指定表单提交的URL。form:label标签用来创建一个标签,path属性用来指定标签绑定到的对象属性,form:input标签用来创建一个文本框,path属性用来指定文本框绑定到的对象属性,form:password标签用来创建一个密码框,path属性用来指定密码框绑定到的对象属性。 使用form:标签库可以非常方便地处理表单,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值