gpt4 book ai didi

java - 在jsp中将编辑按钮保存在相同的表单上

转载 作者:行者123 更新时间:2023-11-30 23:04:44 25 4
gpt4 key购买 nike

我有简单的表格,我想保存数据并通过以相同的形式给出第一个值来编辑数据。我能够保存数据但要编辑我有问题,我想我遵循正确的方法但我是找不到问题,我没有收到任何错误,我放弃了我的全部代码,请解决这个问题

索引.jsp:

  <body>
<%String status;
status=(String)session.getAttribute("edit");
if(status=="fail")
{%>
<h6>there are no records with this no</h6>
<% }
%>
<form action="indexaction.jsp" method="post">
Id <input type="text" name="id" id="id2"><br>
<%
if(status=="success")
{%>
<script type="text/javascript">
document.getElementsById("id2").value='<%=request.getAttribute("id1")%>';
</script>
<%} %>
Name <input type="text" name="name" id="name2"><br>
<%
if(status=="success")
{%>
<script type="text/javascript">
document.getElementsById("name2").value='<%=request.getAttribute("name1")%>';
</script>
<%} %>
sex <input type="text" name="sex" id="sex2"><br>
<%
if(status=="success")
{%>
<script type="text/javascript">
document.getElementsById("sex2").value='<%=request.getAttribute("sex1")%>';
</script>
<%} %>
<input type="submit" name="action" value="save">
<input type="submit" name="action" value="edit">
</form>
</body>

indexaction.jsp:

<body>

<%!

PreparedStatement preparedStatement;
int id1;
String name1,sex1;
%>

<%
Connection connection=DBCreation.getConnection();
String action=request.getParameter("action");
int i=Integer.parseInt(request.getParameter("id"));
String name=request.getParameter("name");
String sex=request.getParameter("sex");

if(action.equals("save")){

preparedStatement=connection.prepareStatement("insert into sample values(?,?,?)");
preparedStatement.setInt(1, i);
preparedStatement.setString(2, name);
preparedStatement.setString(3, sex);
int j=preparedStatement.executeUpdate();
if(j>0)
{
out.println("<h3>inserted success</h3>");
response.sendRedirect("index.jsp");
}
}
else if(action.equals("edit")){
int x=0;
System.out.println("edit called");
preparedStatement=connection.prepareStatement("select * from sample where id=?");
preparedStatement.setInt(1, i);
ResultSet resultSet=preparedStatement.executeQuery();

while(resultSet.next())
{
x++;
request.getSession().setAttribute("edit", "success");
System.out.println("while called"+x);
id1=resultSet.getInt(1);
name1=resultSet.getString(2);
sex1=resultSet.getString(3);

request.setAttribute("id1", id1);
request.setAttribute("name1",name1);
request.setAttribute("sex1", sex1);
}


if(x<=0)
{
request.getSession().setAttribute("edit", "fail");

}

response.sendRedirect("index.jsp");
}
%>
</body>
</html>

使用此代码,如果我输入错误的 ID,我会收到错误消息“没有包含此编号的记录”,这意味着它工作正常,因为设置了状态“失败”。如果我输入正确的 ID,问题就来了。谢谢

最佳答案

试试这个

<body>
<%String status;
status=(String)session.getAttribute("edit");
if(status.equals("fail")) //compare string with .equals not ==
{%>
<h6>there are no records with this no</h6>
<% }
%>
<form action="indexaction.jsp" method="post">
Id <input type="text" name="id" id="id2"><br>
<%
if(status=="success")
{%>
<script type="text/javascript">
document.getElementsById("id2").value='<%=request.getAttribute("id1")%>';
</script>
<%} %>
Name <input type="text" name="name" id="name2"><br>
<%
if(status=="success")
{%>
<script type="text/javascript">
document.getElementsById("name2").value='<%=request.getAttribute("name1")%>';
</script>
<%} %>
sex <input type="text" name="sex" id="sex2"><br>
<%
if(status=="success")
{%>
<script type="text/javascript">
document.getElementsById("sex2").value='<%=request.getAttribute("sex1")%>';
</script>
<%} %>
<input type="submit" name="action" value="save">
<input type="submit" name="action" value="edit">
</form>
</body>

关于java - 在jsp中将编辑按钮保存在相同的表单上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22369857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com