[英]How to send form data to controller
I have a simple J2ee application.我有一个简单的 J2ee 应用程序。 I want to create a simple form of registration, then send the data at controller to storage the data in mysql database.我想创建一个简单的注册形式,然后将数据发送到控制器以将数据存储在 mysql 数据库中。 So I have this jsp page:所以我有这个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>
and this is the controller:这是控制器:
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;
}
}
But when I try to click on Registrazione button I received this error但是当我尝试点击 Registrazione 按钮时,我收到了这个错误
HTTP Status 404 - /sendDati
How can I fixed this error?我该如何修复这个错误?
EDIT:编辑:
springmvcapp-servlet.xml 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
<?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>
EDIT 2 I have change this.编辑 2 我已经改变了这一点。 When I try to open my registrazione.jsp page I use this method:当我尝试打开我的 registrazione.jsp 页面时,我使用以下方法:
<form action="RegistrazioneController/registrazione.html">
<button class="btn btn-primary btn-lg" data-toggle="modal" onclick="submit">
Registrazione
</button>
</form>
My Jsp form is this:我的 Jsp 表格是这样的:
<form action="RegistrazioneController/sendDati" method="POST"/>
My RegistrazioneController class is this:我的 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;
}
}
Subject:主题:
POST Html-Form data towards Spring-Boot @Controller as one DTO Object example POST Html-Form 数据到 Spring-Boot @Controller 作为一个 DTO 对象示例
The following demonstrates:下面演示:
- SPRING BOOT Application - 弹簧启动应用程序
- Html-Form with 2 fields. - 带有 2 个字段的 Html-Form。 The Form POSTs data towards the controller single method.表单向控制器单一方法发送数据。
- TestController with test() method that Receives an object TestObj that includes the 2 fields. - 带有 test() 方法的 TestController 接收包含 2 个字段的对象 TestObj。
Hope that it helps..希望它有帮助..
Y.Lev列夫
<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
public class TestController {
@RequestMapping(value="/test", method=RequestMethod.POST)
public @ResponseBody String test( TestObj testObj){
return "HELLO TEST : " + testObj;
}
}
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 + "]";
}
}
Enjoy..享受..
On your form action add ${pageContext.request.contextPath}/NameOfTheProject/sendDati
在您的表单操作上添加${pageContext.request.contextPath}/NameOfTheProject/sendDati
then execute your code inside然后在里面执行你的代码
@RequestMapping(value = "/sendDati")
public String getDataFromForum(@RequestParam String nome){
System.out.println("pippo"); //execute code here
return nome;
}
It is a 404 error which means the web app server didn't find the resource.这是一个 404 错误,这意味着 Web 应用服务器没有找到资源。 The reason is, you have not given your controller a name.原因是,您没有为控制器命名。
You have to define the name of the controller like您必须定义控制器的名称,例如
@Controller
@RequestMapping("/controllerName")
and you need to modify the action value in the form as well.并且您还需要修改表单中的操作值。
action="appName/controllerName/methodName"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.