当前位置: 首页 > 知识库问答 >
问题:

如何使用Spring MVC 3.1通过控制器传递表单数据 - Spring MVC的新功能

翟誉
2023-03-14

我是Spring的新手,也是Spring的MVC。我使用的是 3.1 版。另外,目前,我使用的是Tomcat 7和MySQL 5.5。我在html" target="_blank">数据库表中有数据,我可以在网页上显示。现在,我正在尝试从 Web 窗体向数据库添加数据,然后在网页上显示该数据。

以下是我尝试调用web表单时出现的运行时错误的一部分:

**根本原因是:不支持请求方法“GET”org . spring framework . web . httprequestmethodnotsupportedexception:org . spring framework . web . servlet . MVC . annotation . annotationmethodhandleradapter$servlethandlemethodresolver . resolve handler method(annotationmethodhandleradapter . Java:665)org . spring framework . web . servlet . MVC . annotation . annotationmethodhandhandler adapter method(annotationmethod handler

这是我的控制器的问题部分。注意,如果我只有@RequestMapping(“/newProfile”)行,而不是另一个@RequestMapping行,那么我会得到一个网页,没有显示我的数据,这是我所期望的,因为我还没有插入DAO层。

    @RequestMapping(value = "/newProfile", method = {RequestMethod.POST})
//@RequestMapping("/newProfile")
public String addNewProfile(@ModelAttribute("profile")Profile profile, ModelMap model) {
      model.addAttribute("firstName", profile.getFirstName());
      model.addAttribute("lastName", profile.getLastName());
      return "newProfileResult";
   }

这是我的输入web表单(目前使用JSP):

<%@ include file="/WEB-INF/jsp/includes.jsp" %>
<%@ include file="/WEB-INF/jsp/header.jsp" %>

<html>
<head>
    <title>Profile Test</title>
</head>
<body>

<h2>Information</h2>
<form:form method="POST" action="/newProfile">
   <table>
    <tr>
        <td><form:label path="firstName">First Name</form:label></td>
        <td><form:input path="firstName" /></td>
    </tr>
    <tr>
        <td><form:label path="lastName">Last Name</form:label></td>
        <td><form:input path="lastName" /></td>
    </tr>   
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>    
</table>  
</form:form>
</body>

共有1个答案

山乐生
2023-03-14

异常告诉Spring找不到GET请求的映射。我假设您只在浏览器中输入/newProfile。这将导致GET请求。提供GET请求的映射。您可以通过一个单独的映射来实现这一点,或者在编写时忽略该方法

 类似资料:
  • 我想把一个id作为用户的输入,传递给控制器,得到特定id的数据 当我在URL--http://localhost:8080/student/1中手动传递id时,它就起作用了 Whitelabel错误页面此应用程序没有/Error的显式映射,因此您将其视为一种后退。 Wed Jun 19 11:04:22 IST 2019出现意外错误(Type=内部服务器错误,Status=500)。模板分析过程中

  • 因此,在我的Javascript中,我使用的是表单序列化,输出是正确的 (issubmit=1) 当我试图将其传递给我的控制器时,我什么也得不到,。我做错了什么? Jquery: 控制器:

  • 我试图将数据传递给controller进行进一步处理,但在controller中得到null,然而js(ajax)的debug无论如何都会显示这个数字。可能是什么问题? Ajax: 控制器:

  • 假设我在jsp中有这个表单 和Person类 编辑:控制器实现 我没有问题调用其他方法,但测试。

  • 问题内容: 如何将ID从此Ajax调用传递给TestController getAjax()函数?当我打电话时,网址是testUrl?id = 1 TestController.php 问题答案: 最后,我只是将参数添加到Route :: get()以及ajax url调用中。我在getAjax()函数中将$ _POST [‘id’]更改为$ _GET [‘id’],这使我的回复 TestCont

  • 我的问题:有一个简单的超文本标记语言表单,操作="/myController"。我需要将表单数据发送到我的控制器,然后从那里我需要向外部控制器发送另一个POST。 我的Spring控制器看起来像这样: 我需要将表单数据传递给我的控制器,它应该将表单数据进一步发送到“urlPath”并接收响应。我需要操纵这个响应并返回一个结果。我的问题是,如何在不操纵请求的情况下进一步发送表单数据? 提前感谢。