我有一个包含数据的表。我想添加一条新记录。但是在某些时候,不可能考虑我引入的内容 控制台 显示 null。读取数据生成空值帖子.jsp <h3 align="left"> <label id="name_form_genre">Добавить должность</label> </h3> id Должности: <input type="text" id="post_id" style="width: 270px;" maxlength="15" value=""/> Название: <input type="text" id="post_name" style="width: 270px;" maxlength="15" value=""/> <div align="left"> <button onclick="add_post()" id="button_form_post">Добавить</button></div>示例.js:function add_post() {var post_id = document.getElementById("post_id");var post_name = document.getElementById("post_name");if(post_id.value.toString().trim()=="" || post_name.value.toString().trim()==""){ alert("Поля не должны быть пустые!")}else { var xhr = new XMLHttpRequest(); var body = 'action=add' + '&id=' + post_id.value + '&name=' + post_name.value; xhr.open("POST", "post", false); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); xhr.send(body); if (xhr.status != 200) alert(xhr.status); window.location = "/post";}}邮递.java:@WebServlet("/post")public class PostServlet extends HttpServlet{private PostService postService = new PostService();public PostServlet() throws SQLException, ClassNotFoundException { }protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("post.jsp").forward(request, response);}有人可以帮助确定问题吗?提前致谢。
1 回答
白衣染霜花
TA贡献1796条经验 获得超10个赞
您正在文件中设置请求参数,但获取请求参数和 java。你能在你的请求中验证它吗?错误消息显示 该和 为 null。id
name
.js
post_id
post_name
post_id
post_name
使用 和 代替。id
name
postEntity.setPost_id(Integer.parseInt(request.getParameter("id"))); postEntity.setPost_name(request.getParameter("name"));
添加回答
举报
0/150
提交
取消