【问题标题】:Passing value from JSP to spring mvc controller error将值从 JSP 传递到 spring mvc 控制器错误
【发布时间】:2017-08-01 14:17:42
【问题描述】:

我在将变量从 JSP 站点传递到 mvc 控制器时遇到问题。下面你可以看到我的 JSP 页面和对应的 mvc 控制器。我设法获取数据,但我的目的是将用户在站点上输入的数据插入数据库并在其他站点上显示。我设法获得了变量的值,但是第一次打开站点时,它们被设置为 null,这会使我的 sql 查询崩溃。当我点击提交按钮时,有没有办法为变量设置值?此外,当我尝试使用 if 排除空值时(如下面的代码所示),我收到以下错误:

错误

There was an unexpected error (type=Internal Server Error, status=500).
No message available

addCustomer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add Customer</title>
</head>
<body>
    <body>
    <form:form method="GET" action="addCustomer" commandName="Residents">
        <div align="center">
            <h2> Add Customer </h2>
            <table>
                <tr>
                    <td>First Name</td>
                    <td><input type="text" name="name" /></td>

                </tr>
                <tr>
                    <td>Surame</td>
                    <td><input type="text" name="surname" /></td>
                </tr>
                <tr>
                    <td>Check in Date</td>
                    <td><input type="date" name="checkInDate"</td>
                </tr>
                <tr>
                    <td>Check out Date</td>
                    <td><input type="date" name="checkOutDate"</td>
                </tr>

                <tr>
                    <td>Email</td>
                    <td><input type="email" name="email"></td>
                </tr>

                <tr>
                    <td>Id</td>
                    <td><input type="number" name="residentId"></td>
                </tr>

                <tr>
                    <td>Room ID</td>
                    <td><input type="number" name="roomId"</td>


                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="send" /></td>
                    <td/>
                    <td><input type="submit" value="Cancel"/></td>
                </tr>

            </table>
            <div style="color: red">${error}</div>
        </div>
    </form:form>
</body>
</html>

Residents.java

package com.stapor.bartlomiej.Model;

import java.sql.Date;

/**
 * Created by Bartek on 2017-07-30.
 */
public class Residents {
    private String name;
    private String surname;
    private Date checkInDate;
    private Date checkOutDate;
    private String email;
    private int id;
    private int roomId;

    public Residents(int id, String name, String surname, Date checkInDate, Date checkOutDate, String email, int roomId) {
        this.name = name;
        this.surname = surname;
        this.checkInDate = checkInDate;
        this.checkOutDate = checkOutDate;
        this.email = email;
        this.id = id;
        this.roomId = roomId;
    }

    public Residents(String name, String surname, Date checkInDate, Date checkOutDate, String email, int roomId) {
        this.name = name;
        this.surname = surname;
        this.checkInDate = checkInDate;
        this.checkOutDate = checkOutDate;
        this.email = email;
        this.roomId = roomId;
    }

    public Residents() {
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Date getCheckInDate() {
        return checkInDate;
    }

    public void setCheckInDate(Date checkInDate) {
        this.checkInDate = checkInDate;
    }

    public Date getCheckOutDate() {
        return checkOutDate;
    }

    public void setCheckOutDate(Date checkOutDate) {
        this.checkOutDate = checkOutDate;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getRoomId() {
        return roomId;
    }

    public void setRoomId(int roomId) {
        this.roomId = roomId;
    }

    @Override
    public String toString() {
        return String.format(
                "Id: %d<br>Name: %s<br>Surname: %s<br>Check In Date: %s<br>Check Out Date: %s<br>E-mail: %s<br>Room ID: %d<br>",
                id, name, surname, checkInDate, checkOutDate, email, roomId);


    }
}

MVC 控制器

@RequestMapping(value = "/addCustomer", method = RequestMethod.GET)
    public String addCustomer(@ModelAttribute("Residents") Residents residents, HttpServletRequest request)
    {

        //System.out.println(name + "\n" + surname + "\n" + checkInDate + "\n" + checkOutDate + "\n" + email + "\n" + residentId + "\n" + roomId);

            //System.out.println(request.getParameter("checkInDate"));
            String[] params = {request.getParameter("checkInDate"),
                    request.getParameter("checkOutDate"),
                    request.getParameter("name"),
                    request.getParameter("surname"),
                    request.getParameter("residentId"),
                    request.getParameter("email"),
                    request.getParameter("roomId")
            };
            for(String k : params)
            {
                System.out.println(k);
            }

            if(params[0].isEmpty())
            {
                System.out.println("shiet");
            }
            else
            {
                String sql = "insert into residents(Check_in_Date, Check_out_Date, Name, Surname, ResidentId, email, roomId) values " +
                        "('" + params[0] + "', '" + params[1] +
                        "', '" + params[2] + "', '" + params[3] + "', '" +
                        params[4] + "', '"  + params[5] + "', " + params[5] + ");";
                jdbcTemplate.batchUpdate(sql);
            }

        //System.out.println("" + request.getParameter("name"));
      //  map.put("residents", residents);
       // map.put("name", request.getParameter("name"));

        return "addCustomer";
    }

从 GET 更改为 POST 并不能解决问题。错误代码如下:

There was an unexpected error (type=Internal Server Error, status=500).
StatementCallback; SQL [insert into residents(Check_in_Date, Check_out_Date, Name, Surname, ResidentId, email, roomId) values ('null', 'null', 'null', 'null', 'null', 'null', null);]; Cannot parse "DATE" constant "null"; SQL statement: insert into residents(Check_in_Date, Check_out_Date, Name, Surname, ResidentId, email, roomId) values ('null', 'null', 'null', 'null', 'null', 'null', null) -- ('null', 'null', 'null', 'null', 'null', 'null', NULL) [22007-191]; nested exception is org.h2.jdbc.JdbcBatchUpdateException: Cannot parse "DATE" constant "null"; SQL statement: insert into residents(Check_in_Date, Check_out_Date, Name, Surname, ResidentId, email, roomId) values ('null', 'null', 'null', 'null', 'null', 'null', null) -- ('null', 'null', 'null', 'null', 'null', 'null', NULL) [22007-191]

【问题讨论】:

  • 对于表单提交,使用post方法而不是get方法
  • 试过你的方法,但还是不行。

标签: java spring jsp spring-mvc


【解决方案1】:

像这样在addCustomer.jsp 页面中使用 Post 方法

<form:form method="POST" action="addCustomer" commandName="Residents">

然后在您的 Controller 类中,请注意 @ModelAttribute 将自动将您的 addCustomer.jsp 表单元素属性值与相应的 Residents 类属性绑定。所以你不必做所有这些request.getParameter("checkOutDate")。 Spring 已经做到了这一点,只需将 @ModelAttribute("Residents") Residents residents 作为参数传递给 addCustomer 方法,就像你做的那样。

所以请继续检索这样的值

@RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
public String addCustomer(@ModelAttribute("Residents") Residents residents){

     //Just some printouts for confirmation
     System.out.println(residents.getName());
     System.out.println(residents.getSurname());

     //Persist data

}

【讨论】:

    【解决方案2】:

    需要在JSP形式的所有输入标签中添加&lt;form:input path="" /&gt;

    【讨论】:

      猜你喜欢
      • 2012-12-08
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多