首页 > 解决方案 > 更新前如何在jsp表单中填写camp

问题描述

我正在尝试显示我打算更新的用户数据。

在我的结构中,我输入了一个在查询列表时提取的 dni,然后以另一种形式显示用户的数据。发送此表格后,数据将永久更新。

我遇到的问题是我不知道如何显示我之前搜索过的用户数据。PE:我正在寻找一个名为 Jhon Doe 且 DNI 为 11111111E 的用户,在第二个表单中,我将有一个新的表单,其中包含姓名和姓氏 Jhon Doe,现在我在表单 Mike Doe 的这些字段中写入并按下提交。

问题:我不知道如何在我的表单中显示旧名称(我不知道如何显示 Jhon Doe)。

这是我的代码,现在我被困在那个部分,更新本身没问题,但没有显示名称)

我已经添加了我的 jsp 网页以查看它们是如何工作的,但无论如何我的问题是关于我设法显示将要更新的用户的日期的方式。

我的第一个 jsp 页面在这里(V2formModificarUsuario1.jsp)

 <%@page import="java.util.List"%>
<%@page import="Entidades.Usuario"%>
<%@page import="DAO.DAOUsuario"%>
<%@page import="Conexion.DBConnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="usuario" class="Entidades.Usuario" />
<jsp:setProperty name="usuario" property="*"/>

<%
String message = "";
List<Usuario> usuarios = null;
DAOUsuario dao = new DAOUsuario();

try {
    //usuarios = dao.selectUsuario("si","nombre");
    usuarios = dao.selectAllUsuarios();
} catch (Exception ex) {
    message = ex.toString();
}
%>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>V2formListarUsuariosAltaNombre</title>
    <link href="../css/estilos.css" rel="stylesheet" type="text/css"/>
</head>
<body>   
    <div class="titulo">
        <h1>BIBLIOTECA - LISTADOS USUARIOS</h1>
        <div id="menu">
            <div id="opciones">
                <h2><a href="../Menu.jsp">Menu inicial</a></h2>
                <h2><a href="V2HojaUsuarios.jsp">Atras</a></h2> 
            </div>
            <form id="formulario" action="V2formModificarUsuario2(en obras).jsp" method="POST">
                <label for="DNI">Intruduzca DNI del usuario</label><br> 
                DNI: <input type="text" name="DNI" required="">
                <input type="submit" value="buscar"> 
            </form>  
            <form>
                <%if (!message.isEmpty()) {%>
                <input name="status_initial" type="text" style="width:400px" value="<%=message%>"/>
                <%} else { %>

                <br/><br/>

                <table border="1">
                    <tr>
                        <th>DNI</th>
                        <th>nombre</th>
                        <th>apellidos</th>
                        <th>de alta</th>

                    </tr>
                    <%for (int i = 0; i < usuarios.size(); i++) {%>
                    <tr>

                        <td><%=usuarios.get(i).getDNI()%></td>
                        <td><%=usuarios.get(i).getNombre()%></td>
                        <td><%=usuarios.get(i).getApellidos()%></td>
                        <td><%=usuarios.get(i).getDeAlta()%></td>

                    </tr>                            
                    <%}%>
                </table>
                <%}%>
            </form>
        </div>
    </div>
</body>

我的第二个 jsp 页面在这里(V2formModificarUsuario2.jsp)

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="Entidades.Usuario"%>
<%@page import="DAO.DAOUsuario"%>
<%@page import="Conexion.DBConnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="usuario" class="Entidades.Usuario"></jsp:useBean> 
<jsp:setProperty name="usuario" property="*"/>

<%
String message = "";


// String nameToChange = "";
String surnameToChange = "";

try {
    if ((usuario.getDNI() != null) && (!usuario.getDNI().isEmpty())) {
        DAOUsuario dao = new DAOUsuario();   

       // nameToChange=dao.selectByDNI(request.getParameter("DNI")).getNombre(); comentado por ahora
        surnameToChange=dao.selectByDNI(request.getParameter("DNI")).getApellidos(); 
        Usuario usuarios = dao.selectByDNI(usuario.getDNI());

        if (usuarios != null) {
            if ((usuario.getNombre() != null) && (!usuario.getNombre().isEmpty())
                    && (usuario.getApellidos() != null) && (!usuario.getApellidos().isEmpty())) {

                dao.update(usuario.getDNI(), usuario.getNombre(), usuario.getApellidos());
                message = "User correctly updated.";
            } else {
                if (request.getParameter("updating") == "1") {
                    message = "Los campos name, surname and deAlta are required .";
                }
            }
        } else {
            message = "user do not exists.";
        }
    } else {
        message = "DNI must not be null.";
    }
} catch (Exception ex) {
    message = ex.getMessage();
}
%>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSPformModificarUsuario2(en obras)</title>
    <link href="css/estilos.css" rel="stylesheet" type="text/css"/>
</head>
<body>  
    <div class="titulo">
        <h1>BIBLIOTECA - USUARIO</h1>
        <div id="menu">
            <div id="opciones">
                <h2><a href="index.jsp">Inicio</a></h2> 
            </div>
            <form method="POST" action="V2formModificarUsuario2(en obras).jsp">
                <input name="updating" type="hidden" value="1"/>

                <%if (!message.isEmpty()) {%>
                <input name="message" type="text" style="width:400px" value="<%=message%>"/>
                <%} else { %>

                <br/><br/>

                DNI:
                <input name="DNI" type="text" style="width:200px" value="<jsp:getProperty property="DNI" name="usuario"/>" readonly=""/>

                <br/><br/>
                nombre:
                <input name="nombre" type="text" style="width:200px" placeholder="<%=apellidoActualizar%>" value="<jsp:getProperty property="nombre" name="usuario"/>"/>

                <br/><br/>
                apellidos:
                <input name="apellidos" type="text" style="width:200px" value="<jsp:getProperty property="apellidos" name="usuario"/>"/>

                <br/><br/>
                <input type="submit" value="Actualizar"/>

                <%}%>
            </form>
       </div>
    </div>
</body>

最后,这里是我的 DAO。

public Usuario update(String DNI, String nombre, String apellidos) throws Exception {
    if ((DNI == null) || (DNI.isEmpty())) {
        throw new Exception("DNI must not be null");
    }
    if ((nombre == null) || (nombre.isEmpty())) {
        throw new Exception("name must not be null");
    }
    if ((apellidos == null) || (apellidos.isEmpty())) {
        throw new Exception("surname must not be null");
    }

    Usuario usuario = selectByDNI(DNI);
    if (usuario == null) {
        throw new Exception("user do not exist");
    }
    try (Connection connection = DBConnection.get()) {
        if (connection == null) {
            throw new Exception("Connection is null");
        }

        String query = "UPDATE usuario SET nombre=?, apellidos=? WHERE DNI=?";

        PreparedStatement statement = connection.prepareStatement(query);
        statement.setString(1, nombre);
        statement.setString(2, apellidos);
        statement.setString(3, DNI);
        statement.execute();
    } catch (Exception ex) {
        throw ex;
    } finally {
        DBConnection.close();
    }
    usuario = selectByDNI(DNI);
    return usuario;
}

public Usuario selectByDNI(String DNI) throws Exception {
    if ((DNI == null) || (DNI.isEmpty())) {
        return null;
    }
    Usuario usuario = null;
    try (Connection connection = DBConnection.get()) {
        if (connection == null) {
            throw new Exception("Connection is null");
        }
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery("SELECT * FROM usuario WHERE DNI = '" + DNI + "'");

        if (rs.next()) {
            usuario = new Usuario(rs.getString("DNI"), rs.getString("nombre"),
                    rs.getString("apellidos"), rs.getString("deAlta"));
        }
    } catch (Exception ex) {
        throw ex;
    } finally {
        DBConnection.close();
    }
    return usuario;
}

标签: javasqlformsjspsubmit

解决方案


我已经解决了,响应被限定为重复,但我不同意,我的解决方案不同:

我创建了 3 个表单,第一个搜索 DNI,第二个创建用户对象并在表单中填充营地(每个表单都有一个占位符和一个值),最后一个是我进行更新的地方为用户。

代码在这里。

jsp页面中的第二种形式

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="Entidades.Usuario"%>
<%@page import="DAO.DAOUsuario"%>
<%@page import="Conexion.DBConnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="usuario" class="Entidades.Usuario"></jsp:useBean> 
<jsp:setProperty name="usuario" property="*"/>

<%
String message = "";
String nombreActualizar = "";
String apellidoActualizar = "";

try {
    if ((usuario.getDNI() != null) && (!usuario.getDNI().isEmpty())) {
        DAOUsuario dao = new DAOUsuario();
        nombreActualizar = dao.selectByDNI(request.getParameter("DNI")).getNombre();
        apellidoActualizar = dao.selectByDNI(request.getParameter("DNI")).getApellidos();
    } else {
        message = "El DNI  de actualización no puede ser nulo.";
    }
} catch (Exception ex) {
    message = ex.getMessage();
}
%>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSPformModificarUsuario2(en obras)</title>
    <link href="../css/estilos.css" rel="stylesheet" type="text/css"/>
</head>
<body>  
    <div class="titulo">
        <h1>BIBLIOTECA - USUARIO</h1>
        <div id="menu">
            <div id="opciones">
                <h2><a href="../Menu.jsp">Menu inicial</a></h2>
                <h2><a href="V2formModificarUsuario1(en obras).jsp">Atras</a></h2> 
            </div>
            <form method="POST" action="V2formModificarUsuario3(en obras).jsp">
                <input name="updating" type="hidden" value="1"/>

                <%if (!message.isEmpty()) {%>
                <input name="message" type="text" style="width:400px" value="<%=message%>"/>
                <%} else {%>

                <br/><br/>

                DNI:
                <input name="DNI" type="text" style="width:200px" value="<jsp:getProperty property="DNI" name="usuario"/>" readonly=""/>

                <br/><br/>
                nombre:
                <!-- <input name="nombre" type="text" style="width:200px" value="<<!--jsp:getProperty property="nombre" name="usuario"/>"/>-->
                <input name="nombre" type="text" style="width:400px" placeholder="<%=nombreActualizar%>" value="<%=nombreActualizar%>"/>
                <br/><br/>
                apellidos:
                <!--<input name="apellidos" type="text" style="width:200px" value="<<!--jsp:getProperty property="apellidos" name="usuario"/>"/>-->
                <input name="apellidos" type="text" style="width:400px" placeholder="<%=apellidoActualizar%>" value="<%=apellidoActualizar%>"/>
                <br/><br/>
                <input type="submit" value="Actualizar"/>

                <%}%>
            </form>
        </div>
    </div>
</body>

新建一个jsp页面中的第三种形式

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="Entidades.Usuario"%>
<%@page import="DAO.DAOUsuario"%>
<%@page import="Conexion.DBConnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="usuario" class="Entidades.Usuario"></jsp:useBean> 
<jsp:setProperty name="usuario" property="*"/>

<%
String message = "";
DAOUsuario dao = new DAOUsuario();
String DNIActualizado="";
String nombreActualizado ="";
String apellidoActualizado = "";

if (
        ((request.getParameter("DNI") == null) || (request.getParameter("DNI").isEmpty()))
     || ((request.getParameter("nombre") == null) || (request.getParameter("nombre").isEmpty()))
     || ((request.getParameter("apellidos") == null) ||(request.getParameter("apellidos").isEmpty()))
        ) {
    message = "Ningún campo del formulario debe estar vacio.";
}else{

DNIActualizado=request.getParameter("DNI");
nombreActualizado=request.getParameter("nombre");
apellidoActualizado=request.getParameter("apellidos");

usuario=dao.update(DNIActualizado, nombreActualizado, apellidoActualizado);
message = "usuario con DNI "+request.getParameter("DNI")+" actualizado";

}

%>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSPformModificarUsuario3(en obras)</title>
    <link href="../css/estilos.css" rel="stylesheet" type="text/css"/>
</head>
<body>  
    <div class="titulo">
        <h1>BIBLIOTECA - USUARIO</h1>
        <div id="menu">
            <div id="opciones">
                <h2><a href="../Menu.jsp">Menu inicial</a></h2>
                <h2><a href="V2formModificarUsuario2(en obras)">Atras</a></h2> 
            </div>
            <form method="POST" action="V2formModificarUsuario3(en obras).jsp">
                <input name="updating" type="hidden" value="1"/>

                <%if (!message.isEmpty()) {%>
                <input name="message" type="text" style="width:400px" value="<%=message%>"/>
                <%} else {%>

                <br/><br/>

                DNI:
                <input name="DNI" type="text" style="width:200px" value="<jsp:getProperty property="DNI" name="usuario"/>" readonly=""/>



                <%}%>
            </form>
       </div>
    </div>
</body>


推荐阅读