如何将表单数据发送到控制器

时间:2015-02-13 13:14:55

标签: spring forms spring-mvc

我有一个简单的J2ee应用程序。 我想创建一个简单的注册形式,然后在控制器上发送数据来存储mysql数据库中的数据。 所以我有这个jsp页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
        <meta charset="utf-8">
        <title>Registrazione</title>
    </head>
<body>

    <h2>${msg}</h2>
    <form action="/sendDati" method="POST">
        <label>Nome <span class="color-red">*</span></label>
        <input type="text" class="form-control margin-bottom-20" name="nome" required></input>      

        <label>Email <span class="color-red">*</span></label>
        <input type="email" class="form-control margin-bottom-20" name="email" required></input>

        <label>Password <span class="color-red">*</span></label>
        <input type="password" class="form-control margin-bottom-20" name="password" required></input>                          

        <label>Conferma Password <span class="color-red">*</span></label>
        <input type="password" class="form-control margin-bottom-20" name="password1" required></input>
        <button type="submit" class="btn btn-primary">REGISTRATI</button>
    </form>
</body>
</html>

这是控制器:

package com.springmvcapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RegistrazioneController {

    @RequestMapping("/registrazione")
    public ModelAndView helloWorld(){

        ModelAndView model = new ModelAndView("registrazione");
        model.addObject("msg", "hello world");
        return model;
    }

    @RequestMapping(value = "/sendDati")
    public String getDataFromForum(@RequestParam String nome){
        System.out.println("pippo");
        return nome;
    }
}

但是当我尝试点击Registrazione按钮时,我收到了此错误

HTTP Status 404 - /sendDati

如何解决此错误?

编辑:

springmvcapp-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.springmvcapp.controller" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
  <display-name>SpringMVCApp</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>springmvcapp</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvcapp</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

编辑2 我改变了这个。当我尝试打开我的registrazione.jsp页面时,我使用这种方法:

<form action="RegistrazioneController/registrazione.html">
                            <button class="btn btn-primary btn-lg" data-toggle="modal" onclick="submit">
                               Registrazione
                            </button>
                        </form>

我的Jsp表格是这样的:

<form action="RegistrazioneController/sendDati" method="POST"/>

我的RegistrazioneController类是这样的:

@Controller
@RequestMapping("/RegistrazioneController")
public class RegistrazioneController {

    @RequestMapping("/registrazione")
    public ModelAndView helloWorld(){

        ModelAndView model = new ModelAndView("registrazione");
        model.addObject("msg", "hello world");
        return model;
    }

    @RequestMapping(value = "/sendDati")
    public String getDataFromForum(@RequestParam String nome){
        System.out.println("pippo");
        return nome;
    }
}

3 个答案:

答案 0 :(得分:0)

这是一个404错误,这意味着Web应用服务器找不到该资源。 原因是,您没有给控制器命名。

您必须定义控制器的名称,如

 @Controller
 @RequestMapping("/controllerName")

并且您还需要修改表单中的操作值。

action="appName/controllerName/methodName"

答案 1 :(得分:0)

在表单操作上添加${pageContext.request.contextPath}/NameOfTheProject/sendDati

然后在

中执行你的代码
@RequestMapping(value = "/sendDati")
    public String getDataFromForum(@RequestParam String nome){
        System.out.println("pippo"); //execute code here
        return nome;
    }

答案 2 :(得分:0)

主题:
POST Html-Form数据朝向Spring-Boot @Controller作为一个DTO对象示例

以下说明:
  - SPRING BOOT申请
  - 带有2个字段的Html-Form。 Form向控制器单一方法POST数据   - 带有test()方法的TestController,它接收一个对象TestObj    包括2个字段。

希望它有所帮助..
Y.Lev

HTML

<form action="/test" method="post">
     f name:<input name="fname" value="yosi"/><br>
     L name:<input name="lname" value="lev"/><br>
     <input type="submit"/>
 </form>

申请人

@SpringBootApplication
public class ApplicationMain {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationMain.class, args);
    }
}

CONTROLLER

@Controller
public class TestController {
    @RequestMapping(value="/test", method=RequestMethod.POST)
    public @ResponseBody String test( TestObj testObj){
        return "HELLO TEST : " + testObj;
    }
}

DTO对象

public class TestObj {
    public String fname;
    public String lname;

    public String getFname() { return fname;}
    public void setFname(String fname) { this.fname = fname; }
    public String getLname() { return lname; }
    public void setLname(String lname) { this.lname = lname; }

    @Override
    public String toString() {
        return "TestObj [fname=" + fname + ", lname=" + lname + "]";
    }
}

享受..