2014-06-29 130 views
-1

我有一个DisplayMessage.jsp页面,它打印存储在表中的数据。我需要更新数量变量的值并要求传递更改字段的值。 我想通过使用隐藏的输入字段来实现这一点,但我总是在servlet中获得一个空值。 我的JSP页面:下面隐藏输入字段总是为空

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
    <%@ page import="package1.GetConnection,java.sql.*" %> 
    <!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=ISO-8859-1"> 
    <title>Insert title here</title> 
    <script type="text/javascript"> 
    function modify(button_id) 
    { 
    var text_val=document.getElementById('textbox'+button_id).value; 
    var dep_id=document.getElementById("dep"+button_id).value; 
    var machine_code=document.getElementById("machine"+button_id).value; 
    var mtlcode=document.getElementById("material"+button_id).value; 
    //alert("Text box value is "+text_val+"Department is "+dep_id+"Machine is"+machine_code+"Material code is "+mtlcode); 
    //$.post("UpdateValue.jsp",{'button_id':button_id}); 

} 



    </script> 


    </head> 

    <body> 

    <form id="displayEstimate" name="displayEstimate" action="UpdateData" method="post" enctype="multipart/form-data"> 
    <input id="button_id" type="hidden" value="Hello"></input> 
    <% 
    GetConnection gc=new GetConnection(); 
    Connection con=gc.getConnection(); 
    PreparedStatement ps=con.prepareStatement("select dep,machine_code,mtlcode,qty,value,to_char(date_of_entry,'DD-MON-YYYY') from tbl_material_estimation"); 
    ResultSet rs=ps.executeQuery(); 


    int i=0; 
    out.print("<table style=\"border-style:solid\"; >"); 
    out.print("<tr><td width=\"50\">Modify</td><td width=\"50\">Delete</td><td width=\"70\">Department</td><td width=\"100\">Machine Code</td><td width=\"100\">Material Code</td><td width=\"50\">Quantity</td><td width=\"50\">Value</td><td width=\"100\">Date of Entry</td></tr>"); 
while(rs.next()) 
    { 
    out.print("<tr><td><button id=\""+i+"\" onclick=\"modify(id)\">Mod</button></td>"); 
    out.print("<td><button id=\""+i+"\">Del</button></td>"); 
    out.print("<td width=\"50\" align=\"center\">"+rs.getString(1)+"<input id=\"dep"+i+"\" type=\"hidden\" value=\""+rs.getString(1)+"\"></input></td>"); 
    out.print("<td width=\"70\" align=\"center\">"+rs.getString(2)+"<input id=\"machine"+i+"\" type=\"hidden\" value=\""+rs.getString(2)+"\"></input></td>"); 
    out.print("<td width=\"100\">"+rs.getString(3)+"<input id=\"material"+i+"\" type=\"hidden\" value=\""+rs.getString(3)+"\"></td>"); 
    out.print("<td width=\"50\" align=\"center\"><input type=\"text\" id=\"textbox"+i+"\" value=\""+rs.getString(4)+"\"><input type=\"hidden\" id=\"hidden"+i+"\" value=\""+rs.getString(4)+"\"></td>"); 
    out.print("<td width=\"50\" align=\"center\">"+rs.getString(5)+"</td>"); 
    out.print("<td width=\"100\">"+rs.getString(6)+"</td></tr>"); 
i++; 
    } 

    out.print("</table>"); 
    %> 
    </form> 
</div> 

</body> 
</html> 

我的servlet代码给出:

import java.io.IOException; 
    import javax.servlet.ServletException; 
    import javax.servlet.annotation.WebServlet; 
    import javax.servlet.http.HttpServlet; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 

    public class UpdateData extends HttpServlet { 


    public UpdateData() { 
    super(); 
    // TODO Auto-generated constructor stub 
    } 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
} 

/** 
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
*/ 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    String id=request.getParameter("button_id"); 
    System.out.println("Id is "+id); 


} 

}

我想打印一个简单的隐藏字段的值,但给了我太多空。有人能指出哪里出了问题吗?

+2

Je sus christ,请在控制器中进行数据库调用,并将*传递给视图。 – christopher

回答

1

如果有很多的代码但发布,你的情况的问题这将是难以针点确切的问题是

<input id="button_id"

你应该使用

<input id="button_id" name="button_id"

HTML表单当提交表格时需要name,而不是id